當前位置: 首頁>>代碼示例>>Java>>正文


Java UnpooledByteBufAllocator類代碼示例

本文整理匯總了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);
    }
  }
}
 
開發者ID:skhalifa,項目名稱:QDrill,代碼行數:18,代碼來源:CachedSingleFileSystem.java

示例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;
}
 
開發者ID:apache,項目名稱:incubator-pulsar,代碼行數:22,代碼來源:PersistentMessageFinderTest.java

示例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);
}
 
開發者ID:SecureSmartHome,項目名稱:SecureSmartHome,代碼行數:25,代碼來源:DeviceConnectInformation.java

示例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);

}
 
開發者ID:streamsets,項目名稱:datacollector,代碼行數:19,代碼來源:TestCollectdParser.java

示例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);

}
 
開發者ID:streamsets,項目名稱:datacollector,代碼行數:19,代碼來源:TestCollectdParser.java

示例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());
}
 
開發者ID:streamsets,項目名稱:datacollector,代碼行數:18,代碼來源:TestCollectdParser.java

示例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());
  }
}
 
開發者ID:streamsets,項目名稱:datacollector,代碼行數:21,代碼來源:TestSyslogParser.java

示例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());
}
 
開發者ID:xjdr,項目名稱:xio,代碼行數:25,代碼來源:Http1ServerCodecUnitTest.java

示例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());
}
 
開發者ID:xjdr,項目名稱:xio,代碼行數:26,代碼來源:Http1ServerCodecUnitTest.java

示例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());
}
 
開發者ID:xjdr,項目名稱:xio,代碼行數:22,代碼來源:Http1ClientCodecUnitTest.java

示例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());
}
 
開發者ID:xjdr,項目名稱:xio,代碼行數:24,代碼來源:Http1ClientCodecUnitTest.java

示例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();
}
 
開發者ID:lettuce-io,項目名稱:lettuce-core,代碼行數:19,代碼來源:CommandArgs.java

示例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());
}
 
開發者ID:fp7-netide,項目名稱:Engine,代碼行數:19,代碼來源:ShimRelay.java

示例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);
    }
}
 
開發者ID:fp7-netide,項目名稱:Engine,代碼行數:26,代碼來源:OF10StatsReplyMessageFactory.java

示例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);
    }
}
 
開發者ID:fp7-netide,項目名稱:Engine,代碼行數:22,代碼來源:MultipartReplyMessageFactory.java


注:本文中的io.netty.buffer.UnpooledByteBufAllocator類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。