本文整理汇总了Java中io.netty.buffer.UnpooledByteBufAllocator类的典型用法代码示例。如果您正苦于以下问题:Java UnpooledByteBufAllocator类的具体用法?Java UnpooledByteBufAllocator怎么用?Java UnpooledByteBufAllocator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UnpooledByteBufAllocator类属于io.netty.buffer包,在下文中一共展示了UnpooledByteBufAllocator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: CachedSingleFileSystem
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
public CachedSingleFileSystem(String path) throws IOException {
this.path = path;
File f = new File(path);
long length = f.length();
if (length > Integer.MAX_VALUE) {
throw new UnsupportedOperationException("Cached file system only supports files of less than 2GB.");
}
System.out.println(length);
try (InputStream is = new BufferedInputStream(new FileInputStream(path))) {
byte[] buffer = new byte[64*1024];
this.file = UnpooledByteBufAllocator.DEFAULT.directBuffer((int) length);
int read;
while ( (read = is.read(buffer)) > 0) {
file.writeBytes(buffer, 0, read);
}
}
}
示例2: createMessageWrittenToLedger
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
public static byte[] createMessageWrittenToLedger(String msg) throws Exception {
PulsarApi.MessageMetadata.Builder messageMetadataBuilder = PulsarApi.MessageMetadata.newBuilder();
messageMetadataBuilder.setPublishTime(System.currentTimeMillis());
messageMetadataBuilder.setProducerName("createMessageWrittenToLedger");
messageMetadataBuilder.setSequenceId(1);
PulsarApi.MessageMetadata messageMetadata = messageMetadataBuilder.build();
ByteBuf data = UnpooledByteBufAllocator.DEFAULT.heapBuffer().writeBytes(msg.getBytes());
int msgMetadataSize = messageMetadata.getSerializedSize();
int payloadSize = data.readableBytes();
int totalSize = 4 + msgMetadataSize + payloadSize;
ByteBuf headers = PooledByteBufAllocator.DEFAULT.heapBuffer(totalSize, totalSize);
ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
headers.writeInt(msgMetadataSize);
messageMetadata.writeTo(outStream);
ByteBuf headersAndPayload = DoubleByteBuf.get(headers, data);
byte[] byteMessage = headersAndPayload.nioBuffer().array();
headersAndPayload.release();
return byteMessage;
}
示例3: fromDataString
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
/**
* Read a DeviceConnectInformation from a Base64 encoded String, which was read from a QR Code.
*/
public static DeviceConnectInformation fromDataString(String data) throws IOException {
final ByteBuf base64 = UnpooledByteBufAllocator.DEFAULT.heapBuffer(data.length());
ByteBufUtil.writeAscii(base64, data);
final ByteBuf byteBuf = decode(base64);
if (byteBuf.readableBytes() != DATA_LENGTH) {
throw new IOException("too many bytes encoded");
}
final byte[] addressData = new byte[ADDRESS_LENGTH];
byteBuf.readBytes(addressData);
final InetAddress address = InetAddress.getByAddress(addressData);
final int port = byteBuf.readUnsignedShort();
final byte[] idData = new byte[DeviceID.ID_LENGTH];
byteBuf.readBytes(idData);
final DeviceID id = new DeviceID(idData);
final byte[] encodedToken = new byte[TOKEN_BASE64_LENGTH];
byteBuf.readBytes(encodedToken);
final byte[] token = decodeToken(new String(encodedToken));
return new DeviceConnectInformation(address, port, id, token);
}
示例4: testParser
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
@Test
public void testParser() throws Exception {
UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
CollectdParser parser = new CollectdParser(getContext(), false, null, false, null, CHARSET);
byte[] bytes = Files.readAllBytes(SINGLE_PACKET.toPath());
ByteBuf buf = allocator.buffer(bytes.length);
buf.writeBytes(bytes);
List<Record> records = parser.parse(buf, null, null);
Assert.assertEquals(23, records.size()); // 23 Value parts
Record record0 = records.get(0);
UDPTestUtil.verifyCollectdRecord(UDPTestUtil.expectedRecord0, record0);
Record record2 = records.get(2);
UDPTestUtil.verifyCollectdRecord(UDPTestUtil.expectedRecord2, record2);
}
示例5: testParserExcludeInterval
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
@Test
public void testParserExcludeInterval() throws Exception {
UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
CollectdParser parser = new CollectdParser(getContext(), false, null, true, null, CHARSET);
byte[] bytes = Files.readAllBytes(SINGLE_PACKET.toPath());
ByteBuf buf = allocator.buffer(bytes.length);
buf.writeBytes(bytes);
List<Record> records = parser.parse(buf, null, null);
Assert.assertEquals(23, records.size()); // 23 Value parts
Record record0 = records.get(0);
UDPTestUtil.verifyCollectdRecord(UDPTestUtil.expectedRecordNoInterval0, record0);
Record record2 = records.get(2);
UDPTestUtil.verifyCollectdRecord(UDPTestUtil.expectedRecordNoInterval2, record2);
}
示例6: testEncryptedRecord
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
@Test
public void testEncryptedRecord() throws Exception {
// If unlimited strength encryption is not available, we cant run this test.
Assume.assumeFalse(Cipher.getMaxAllowedKeyLength("AES") < 256);
UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
CollectdParser parser = new CollectdParser(getContext(), false, null, false, AUTH_FILE_PATH, CHARSET);
byte[] bytes = Files.readAllBytes(SINGLE_ENCRYPTED_PACKET.toPath());
ByteBuf buf = allocator.buffer(bytes.length);
buf.writeBytes(bytes);
List<Record> records = parser.parse(buf, null, null);
Assert.assertEquals(24, records.size()); // 24 value parts
Record record14 = records.get(14);
UDPTestUtil.verifyCollectdRecord(UDPTestUtil.encryptedRecord14, record14);
LOG.info("Num records: {}", records.size());
}
示例7: testParseFailure
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
@Test
public void testParseFailure() throws Exception {
SyslogParser parser = new SyslogParser(getContext(), StandardCharsets.UTF_8);
String msg = "<123> ";
byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
ByteBuf buffer = allocator.buffer(bytes.length);
buffer.writeBytes(bytes);
try {
parser.parse(
buffer,
InetSocketAddress.createUnresolved("localhost", 5000),
InetSocketAddress.createUnresolved("localhost", 50000)
);
Assert.fail("Expected OnRecordErrorException");
} catch (OnRecordErrorException ex) {
Record record = ex.getRecord();
Assert.assertEquals(msg, record.get().getValueAsString());
}
}
示例8: testFullRequestWithBody
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
@Test
public void testFullRequestWithBody() throws Exception {
outputReceived = new CountDownLatch(1);
String payload = "body";
ByteBuf body = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, payload);
FullHttpRequest requestIn = new DefaultFullHttpRequest(HTTP_1_1, GET, "/", body);
channel.writeInbound(requestIn);
channel.runPendingTasks(); // blocks
Uninterruptibles.awaitUninterruptibly(outputReceived);
Request requestOut = requests.remove(0);
assertTrue(requestOut != null);
assertTrue(requestOut instanceof FullRequest);
assertEquals("HTTP/1.1", requestOut.version());
assertEquals(HttpMethod.GET, requestOut.method());
assertEquals("/", requestOut.path());
assertTrue(requestOut.hasBody());
assertFalse(requestOut.body() == null);
assertEquals(body, requestOut.body());
}
示例9: testFullResponse
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
@Test
public void testFullResponse() throws Exception {
outputReceived = new CountDownLatch(2);
ByteBuf body = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, "response");
FullHttpRequest requestIn = new DefaultFullHttpRequest(HTTP_1_1, GET, "/");
FullResponse responseIn = ResponseBuilders.newOk().body(body).build();
channel.writeInbound(requestIn);
channel.runPendingTasks(); // blocks
channel.writeOutbound(responseIn);
channel.runPendingTasks(); // blocks
Uninterruptibles.awaitUninterruptibly(outputReceived);
HttpResponse responseOut = (HttpResponse) responses.remove(0);
assertTrue(responseOut != null);
assertTrue(responseOut instanceof FullHttpResponse);
assertEquals(HTTP_1_1, responseOut.protocolVersion());
assertEquals(OK, responseOut.status());
assertFalse(((FullHttpResponse) responseOut).content() == null);
assertEquals(body, ((FullHttpResponse) responseOut).content());
}
示例10: testFullRequestWithBody
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
@Test
public void testFullRequestWithBody() throws Exception {
outputReceived = new CountDownLatch(1);
ByteBuf body = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, "body");
FullRequest requestIn = RequestBuilders.newPost("/").body(body).build();
channel.writeOutbound(requestIn);
channel.runPendingTasks(); // blocks
Uninterruptibles.awaitUninterruptibly(outputReceived);
FullHttpRequest requestOut = (FullHttpRequest) requests.remove(0);
assertTrue(requestOut != null);
assertEquals(HTTP_1_1, requestOut.protocolVersion());
assertEquals(HttpMethod.POST, requestOut.method());
assertEquals("/", requestOut.uri());
assertFalse(requestOut.content() == null);
assertEquals(body, requestOut.content());
}
示例11: testFullResponse
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
@Test
public void testFullResponse() throws Exception {
outputReceived = new CountDownLatch(1);
ByteBuf body = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, "response");
FullHttpResponse responseIn = new DefaultFullHttpResponse(HTTP_1_1, OK, body);
channel.writeInbound(responseIn);
channel.runPendingTasks(); // blocks
Uninterruptibles.awaitUninterruptibly(outputReceived);
Response responseOut = responses.remove(0);
assertTrue(responseOut != null);
assertTrue(responseOut instanceof FullResponse);
assertEquals("HTTP/1.1", responseOut.version());
assertEquals(OK, responseOut.status());
assertTrue(responseOut.hasBody());
assertFalse(responseOut.body() == null);
assertEquals(body, responseOut.body());
}
示例12: toString
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer(singularArguments.size() * 10);
encode(buffer);
buffer.resetReaderIndex();
byte[] bytes = new byte[buffer.readableBytes()];
buffer.readBytes(bytes);
sb.append(" [buffer=").append(new String(bytes));
sb.append(']');
buffer.release();
return sb.toString();
}
示例13: sendOpenFlowMessageToCore
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
public void sendOpenFlowMessageToCore(ZeroMQBaseConnector coreConnector, DataObject msg, short ofVersion, long xId,
long datapathId, int moduleId) {
SerializationFactory factory = createSerializationFactory();
SerializerRegistry registry = new SerializerRegistryImpl();
registry.init();
ByteBuf output = UnpooledByteBufAllocator.DEFAULT.buffer();
factory.setSerializerTable(registry);
factory.messageToBuffer(ofVersion, output, msg);
byte[] bytes = new byte[output.readableBytes()];
output.readBytes(bytes);
Message message = new Message(NetIPUtils.StubHeaderFromPayload(bytes), bytes);
message.getHeader().setMessageType(MessageType.OPENFLOW);
message.getHeader().setDatapathId(datapathId);
message.getHeader().setModuleId(moduleId);
message.getHeader().setTransactionId((int) xId);
coreConnector.SendData(message.toByteRepresentation());
}
示例14: serializeFlowBody
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
private void serializeFlowBody(MultipartReplyBody body, ByteBuf outBuffer, MultipartReplyMessage message) {
MultipartReplyFlowCase flowCase = (MultipartReplyFlowCase) body;
MultipartReplyFlow flow = flowCase.getMultipartReplyFlow();
for (FlowStats flowStats : flow.getFlowStats()) {
ByteBuf flowStatsBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
flowStatsBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
flowStatsBuff.writeByte(new Long(flowStats.getTableId()).byteValue());
flowStatsBuff.writeZero(FLOW_STATS_PADDING_1);
OFSerializer<MatchV10> matchSerializer = registry
.getSerializer(new MessageTypeKey<>(message.getVersion(), MatchV10.class));
matchSerializer.serialize(flowStats.getMatchV10(), flowStatsBuff);
flowStatsBuff.writeInt(flowStats.getDurationSec().intValue());
flowStatsBuff.writeInt(flowStats.getDurationNsec().intValue());
flowStatsBuff.writeShort(flowStats.getPriority());
flowStatsBuff.writeShort(flowStats.getIdleTimeout());
flowStatsBuff.writeShort(flowStats.getHardTimeout());
flowStatsBuff.writeZero(FLOW_STATS_PADDING_2);
flowStatsBuff.writeLong(flowStats.getCookie().longValue());
flowStatsBuff.writeLong(flowStats.getPacketCount().longValue());
flowStatsBuff.writeLong(flowStats.getByteCount().longValue());
ListSerializer.serializeList(flowStats.getAction(), ACTION_KEY_MAKER, registry, flowStatsBuff);
flowStatsBuff.setShort(FLOW_STATS_LENGTH_INDEX, flowStatsBuff.readableBytes());
outBuffer.writeBytes(flowStatsBuff);
}
}
示例15: serializeMeterBody
import io.netty.buffer.UnpooledByteBufAllocator; //导入依赖的package包/类
private void serializeMeterBody(MultipartReplyBody body, ByteBuf outBuffer) {
MultipartReplyMeterCase meterCase = (MultipartReplyMeterCase) body;
MultipartReplyMeter meter = meterCase.getMultipartReplyMeter();
for (MeterStats meterStats : meter.getMeterStats()) {
ByteBuf meterStatsBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
meterStatsBuff.writeInt(meterStats.getMeterId().getValue().intValue());
meterStatsBuff.writeInt(EncodeConstants.EMPTY_LENGTH);
meterStatsBuff.writeZero(METER_PADDING);
meterStatsBuff.writeInt(meterStats.getFlowCount().intValue());
meterStatsBuff.writeLong(meterStats.getPacketInCount().longValue());
meterStatsBuff.writeLong(meterStats.getByteInCount().longValue());
meterStatsBuff.writeInt(meterStats.getDurationSec().intValue());
meterStatsBuff.writeInt(meterStats.getDurationNsec().intValue());
for (MeterBandStats meterBandStats : meterStats.getMeterBandStats()) {
meterStatsBuff.writeLong(meterBandStats.getPacketBandCount().longValue());
meterStatsBuff.writeLong(meterBandStats.getByteBandCount().longValue());
}
meterStatsBuff.setInt(METER_LENGTH_INDEX, meterStatsBuff.readableBytes());
outBuffer.writeBytes(meterStatsBuff);
}
}