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


Java ByteBufAllocator類代碼示例

本文整理匯總了Java中io.netty.buffer.ByteBufAllocator的典型用法代碼示例。如果您正苦於以下問題:Java ByteBufAllocator類的具體用法?Java ByteBufAllocator怎麽用?Java ByteBufAllocator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ByteBufAllocator類屬於io.netty.buffer包,在下文中一共展示了ByteBufAllocator類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: addValueOverloads

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Test
public void addValueOverloads() {
  byte[] value1 = new byte[64];
  Arrays.fill(value1, (byte) 'b');

  ByteBuf value2Buf = ByteBufAllocator.DEFAULT.buffer(14);
  byte[] value2 = "This is a test".getBytes();
  value2Buf.writeBytes(value2);

  RtMessage value3Msg = RtMessage.builder().add(RtTag.PAD, new byte[12]).build();
  ByteBuf value3Buf = RtWire.toWire(value3Msg);
  byte[] value3 = new byte[value3Buf.readableBytes()];
  value3Buf.readBytes(value3);

  RtMessage msg = RtMessage.builder()
      .add(RtTag.INDX, value1)
      .add(RtTag.MAXT, value2Buf)
      .add(RtTag.NONC, value3Msg)
      .build();

  assertArrayEquals(msg.get(RtTag.INDX), value1);
  assertArrayEquals(msg.get(RtTag.MAXT), value2);
  assertArrayEquals(msg.get(RtTag.NONC), value3);
}
 
開發者ID:int08h,項目名稱:nearenough,代碼行數:25,代碼來源:RtMessageBuilderTest.java

示例2: writeInContext

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
protected void writeInContext() {
  CompositeByteBuf cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
  for (;;) {
    ByteBuf buf = writeQueue.poll();
    if (buf == null) {
      break;
    }

    writeQueueSize.decrementAndGet();
    cbb.addComponent(true, buf);

    if (cbb.numComponents() == cbb.maxNumComponents()) {
      netSocket.write(Buffer.buffer(cbb));
      cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
    }
  }
  if (cbb.isReadable()) {
    netSocket.write(Buffer.buffer(cbb));
  }
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:21,代碼來源:TcpConnection.java

示例3: encode

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Override
public List<Object> encode(ByteBufAllocator alloc) {
    ByteBuf meta = metadata(alloc);

    ByteBuf head = alloc.buffer(FastdfsConstants.FDFS_HEAD_LEN);
    head.writeLong(meta.readableBytes() + size);
    head.writeByte(cmd());
    head.writeByte(FastdfsConstants.ERRNO_OK);

    CompositeByteBuf cbb = alloc.compositeBuffer();
    cbb.addComponents(head, meta);
    cbb.writerIndex(head.readableBytes() + meta.readableBytes());

    List<Object> requests = new LinkedList<>();
    requests.add(cbb);
    requests.add(content);
    return requests;
}
 
開發者ID:rodbate,項目名稱:fastdfs-spring-boot,代碼行數:19,代碼來源:FileOperationEncoder.java

示例4: encode

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, Proto proto, List<Object> list) throws Exception {
    ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
    if (proto.getBody() != null) {
        byteBuf.writeInt(Proto.HEADER_LENGTH + proto.getBody().length);
        byteBuf.writeShort(Proto.HEADER_LENGTH);
        byteBuf.writeShort(Proto.VERSION);
        byteBuf.writeInt(proto.getOperation());
        byteBuf.writeInt(proto.getSeqId());
        byteBuf.writeBytes(proto.getBody());
    } else {
        byteBuf.writeInt(Proto.HEADER_LENGTH);
        byteBuf.writeShort(Proto.HEADER_LENGTH);
        byteBuf.writeShort(Proto.VERSION);
        byteBuf.writeInt(proto.getOperation());
        byteBuf.writeInt(proto.getSeqId());
    }
    list.add(byteBuf);
    logger.debug("encode: {}", proto);
}
 
開發者ID:onsoul,項目名稱:os,代碼行數:21,代碼來源:TcpProtoCodec.java

示例5: encode

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Override
protected void encode(ChannelHandlerContext ctx, Proto proto, List<Object> list) throws Exception {
    ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
    if (proto.getBody() != null) {
        byteBuf.writeInt(Proto.HEADER_LENGTH + proto.getBody().length);
        byteBuf.writeShort(Proto.HEADER_LENGTH);
        byteBuf.writeShort(Proto.VERSION);
        byteBuf.writeInt(proto.getOperation());
        byteBuf.writeInt(proto.getSeqId());
        byteBuf.writeBytes(proto.getBody());
    } else {
        byteBuf.writeInt(Proto.HEADER_LENGTH);
        byteBuf.writeShort(Proto.HEADER_LENGTH);
        byteBuf.writeShort(Proto.VERSION);
        byteBuf.writeInt(proto.getOperation());
        byteBuf.writeInt(proto.getSeqId());
    }

    list.add(new BinaryWebSocketFrame(byteBuf));

    logger.debug("encode: {}", proto);
}
 
開發者ID:onsoul,項目名稱:os,代碼行數:23,代碼來源:WebSocketProtoCodec.java

示例6: hashToBase64

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
public static String hashToBase64(ByteBuf objectState) {
    ByteBuffer bf = objectState.internalNioBuffer(objectState.readerIndex(), objectState.readableBytes());
    long h1 = LongHashFunction.farmUo().hashBytes(bf);
    long h2 = LongHashFunction.xx().hashBytes(bf);

    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE);
    try {
        buf.writeLong(h1).writeLong(h2);
        ByteBuf b = Base64.encode(buf);
        try {
            String s = b.toString(CharsetUtil.UTF_8);
            return s.substring(0, s.length() - 2);
        } finally {
            b.release();
        }
    } finally {
        buf.release();
    }
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:20,代碼來源:Hash.java

示例7: decode

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
    int decompressSize = buf.readInt();
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize);
    try {
        LZ4SafeDecompressor decompressor = factory.safeDecompressor();
        ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
        int pos = outBuffer.position();
        decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer);
        int compressedLength = outBuffer.position() - pos;
        out.writerIndex(compressedLength);
        return innerCodec.getValueDecoder().decode(out, state);
    } finally {
        out.release();
    }
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:17,代碼來源:LZ4Codec.java

示例8: encode

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Override
public ByteBuf encode(Object in) throws IOException {
    Kryo kryo = null;
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
    try {
        ByteBufOutputStream baos = new ByteBufOutputStream(out);
        Output output = new Output(baos);
        kryo = kryoPool.get();
        kryo.writeClassAndObject(output, in);
        output.close();
        return baos.buffer();
    } catch (Exception e) {
        out.release();
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new RedissonKryoCodecException(e);
    } finally {
        if (kryo != null) {
            kryoPool.yield(kryo);
        }
    }
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:24,代碼來源:KryoCodec.java

示例9: encodeRequest

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
static ByteBuf encodeRequest(TProtocolFactory protocolFactory, ByteBufAllocator allocator, int sequenceId, MethodMetadata method, List<Object> parameters)
        throws Exception
{
    TChannelBufferOutputTransport transport = new TChannelBufferOutputTransport(allocator.buffer(1024));
    TProtocolWriter protocol = protocolFactory.getProtocol(transport);

    // Note that though setting message type to ONEWAY can be helpful when looking at packet
    // captures, some clients always send CALL and so servers are forced to rely on the "oneway"
    // attribute on thrift method in the interface definition, rather than checking the message
    // type.
    protocol.writeMessageBegin(new TMessage(method.getName(), method.isOneway() ? ONEWAY : CALL, sequenceId));

    // write the parameters
    ProtocolWriter writer = new ProtocolWriter(protocol);
    writer.writeStructBegin(method.getName() + "_args");
    for (int i = 0; i < parameters.size(); i++) {
        Object value = parameters.get(i);
        ParameterMetadata parameter = method.getParameters().get(i);
        writer.writeField(parameter.getName(), parameter.getId(), parameter.getCodec(), value);
    }
    writer.writeStructEnd();

    protocol.writeMessageEnd();
    return transport.getOutputBuffer();
}
 
開發者ID:airlift,項目名稱:drift,代碼行數:26,代碼來源:MessageEncoding.java

示例10: writeMessage

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
private void writeMessage(Packet packet) {
    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(64);

    Codecs codecs = connectivityManager.getCodecRegistrar().getCodecs();

    buf.writeByte(codecs.getId(packet));

    if (packet instanceof SubClientPacket) {
        SubClientPacket subClientPacket = (SubClientPacket) packet;
        buf.writeByte(subClientPacket.getSenderSubClientId());
        buf.writeByte(subClientPacket.getTargetSubClientId());
    }

    Codec<? extends Packet> codec = connectivityManager.getCodecRegistrar().getCodecs().getCodec(packet.getClass());
    codec.encode(packet, buf);

    ByteBufUtils.writeUnsignedVarInt(out, buf.readableBytes());
    out.writeBytes(buf);
}
 
開發者ID:JungleTree,項目名稱:JungleTree,代碼行數:20,代碼來源:BatchedMessageWriter.java

示例11: encode

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Test
public void encode() {
    // Given
    PlayStatePacket expected = createDummyPlayStatePacket();

    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
    buf.markReaderIndex();

    // When
    subject.encode(expected, buf);

    // Then
    PlayStatePacket actual = subject.decode(buf);
    assertThat(actual).isEqualTo(expected);
    Truth.assertThat(actual.getPlayState()).isEqualTo(expected.getPlayState());
}
 
開發者ID:JungleTree,項目名稱:JungleTree,代碼行數:17,代碼來源:PlayStateCodecTest.java

示例12: encode

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Test
public void encode() {
    // Given
    LoginPacket expected = createDummyLoginPacket();

    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();

    // When
    subject.encode(expected, buf);

    // Then
    LoginPacket actual = subject.decode(buf);
    assertThat(actual).isEqualTo(expected);
    assertThat(actual.getClientNetworkVersion()).isEqualTo(expected.getClientNetworkVersion());
    assertThat(actual.getConnectionInfo()).isEqualTo(expected.getConnectionInfo());
}
 
開發者ID:JungleTree,項目名稱:JungleTree,代碼行數:17,代碼來源:LoginCodecTest.java

示例13: preEncode

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
static ByteBuf preEncode(Object msg, Codec<Object> codec, ByteBufAllocator allocator) throws CodecException {
    ByteBuf buf = null;

    try {
        buf = allocator.buffer();

        ByteBufDataWriter writer = new ByteBufDataWriter(buf);

        doEncode(msg, writer, codec);

        return buf;
    } catch (CodecException e) {
        if (buf != null) {
            buf.release();
        }

        throw e;
    }
}
 
開發者ID:hekate-io,項目名稱:hekate,代碼行數:20,代碼來源:NetworkProtocolCodec.java

示例14: getEncodedTargetAddress

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
private ByteBuf getEncodedTargetAddress(ByteBufAllocator allocator, boolean resolve) throws ProxyConnectException {
    InetSocketAddress remoteAddress = destinationAddress();
    SocksAddressType remoteAddressType;
    String remoteHost;
    if (!resolve || remoteAddress.isUnresolved()) {
        remoteAddressType = SocksAddressType.DOMAIN;
        remoteHost = remoteAddress.getHostString();
    } else {
        remoteHost = remoteAddress.getAddress().getHostAddress();
        if (NetUtil.isValidIpV4Address(remoteHost)) {
            remoteAddressType = SocksAddressType.IPv4;
        } else if (NetUtil.isValidIpV6Address(remoteHost)) {
            remoteAddressType = SocksAddressType.IPv6;
        } else {
            throw new ProxyConnectException("unknown address type: " + StringUtil.simpleClassName(remoteHost));
        }
    }
    int remotePort = remoteAddress.getPort();
    SocksCmdRequest request = new SocksCmdRequest(SocksCmdType.UNKNOWN, remoteAddressType, remoteHost, remotePort);
    return SSocksAddressEncoder.INSTANCE.encode(allocator, request);
}
 
開發者ID:tridays,項目名稱:netty-socks,代碼行數:22,代碼來源:SSocksConnectHandler.java

示例15: encode

import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
public ByteBuf encode(ByteBufAllocator allocator, SocksCmdRequest msg) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("encode target address");
    }

    ByteBuf buf = allocator.directBuffer();
    msg.encodeAsByteBuf(buf);
    buf.skipBytes(3);

    if (LOG.isTraceEnabled()) {
        byte[] bytes = new byte[buf.readableBytes()];
        buf.getBytes(buf.readerIndex(), bytes);
    }

    return buf;
}
 
開發者ID:tridays,項目名稱:netty-socks,代碼行數:17,代碼來源:SSocksAddressEncoder.java


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