Example usage for io.netty.buffer ByteBuf readBytes

List of usage examples for io.netty.buffer ByteBuf readBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readBytes.

Prototype

public abstract ByteBuf readBytes(ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination's position reaches its limit, and increases the readerIndex by the number of the transferred bytes.

Usage

From source file:herddb.utils.ByteBufUtils.java

License:Apache License

public static final String readString(ByteBuf buffer) {
    final int len = readVInt(buffer);
    final byte[] array = new byte[len];
    buffer.readBytes(array);
    return new String(array, StandardCharsets.UTF_8);
}

From source file:herddb.utils.ByteBufUtils.java

License:Apache License

public static final RawString readRawString(ByteBuf buffer) {
    final int len = readVInt(buffer);
    final byte[] array = new byte[len];
    buffer.readBytes(array);
    return RawString.newPooledRawString(array, 0, len);
}

From source file:herddb.utils.ByteBufUtils.java

License:Apache License

public static final RawString readUnpooledRawString(ByteBuf buffer) {
    final int len = readVInt(buffer);
    final byte[] array = new byte[len];
    buffer.readBytes(array);
    return RawString.newUnpooledRawString(array, 0, len);
}

From source file:http.HTTPResponseHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
    Integer streamId = msg.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
    if (streamId == null) {
        log.error("HTTP2ResponseHandler unexpected message received: " + msg);
        return;//from  www.  ja  v  a2 s .  co  m
    }
    Entry<ChannelFuture, ChannelPromise> entry = streamIdPromiseMap.get(streamId);
    if (entry == null) {
        if (streamId == 1) {
            log.error("HTTP2 Upgrade request has received from  stream : " + streamId);
        }
    } else {
        ByteBuf content = msg.content();
        if (content.isReadable()) {
            int contentLength = content.readableBytes();
            byte[] arr = new byte[contentLength];
            content.readBytes(arr);
            streamIdResponseMap.put(streamId, new String(arr, 0, contentLength, CharsetUtil.UTF_8));
        }
        entry.getValue().setSuccess();
    }
}

From source file:http2.client.HttpResponseHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
    Integer streamId = msg.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
    if (streamId == null) {
        System.err.println("HttpResponseHandler unexpected message received: " + msg);
        return;//from   www .j  a  v  a2s.  c  om
    }

    Entry<ChannelFuture, ChannelPromise> entry = streamidPromiseMap.get(streamId);
    if (entry == null) {
        System.err.println("Message received for unknown stream id " + streamId);
    } else {
        // Do stuff with the message (for now just print it)
        ByteBuf content = msg.content();
        if (content.isReadable()) {
            int contentLength = content.readableBytes();
            byte[] arr = new byte[contentLength];
            content.readBytes(arr);
            System.out.println("finally channel read 0 is "
                    + new String(arr, 0, contentLength, CharsetUtil.UTF_8) + " streamId : " + streamId);
        }

        entry.getValue().setSuccess();
    }
}

From source file:hunternif.mc.atlas.network.AbstractMessage.java

License:Open Source License

public static NBTTagCompound readNBT(ByteBuf buffer) throws IOException {
    int length = buffer.readInt();
    if (length < 0) {
        return null;
    } else {//from  ww  w .  j a v a2  s  . c o m
        byte[] compressed = new byte[length];
        buffer.readBytes(compressed);
        return CompressedStreamTools.func_152457_a(compressed, new NBTSizeTracker(Integer.MAX_VALUE));
    }
}

From source file:impl.underdark.transport.bluetooth.discovery.ble.ManufacturerData.java

License:Open Source License

public static ManufacturerData parse(byte[] data) {
    if (data == null || data.length < (1 + 6 + 4))
        return null;

    ByteBuf buf = Unpooled.wrappedBuffer(data);
    byte version = buf.readByte();
    if (version != 1)
        return null;

    int appId = buf.readInt();
    byte[] address = new byte[deviceAddressLen];
    buf.readBytes(address);
    int channelsMask = buf.readInt();

    List<Integer> channels = new ArrayList<>();

    for (int channel = 0; channelsMask != 0; ++channel) {
        if (channelsMask % 2 != 0)
            channels.add(channel);/*w w w  . ja  v  a2s  . c  o  m*/

        channelsMask >>= 1;
    }

    return new ManufacturerData(appId, address, channels);
}

From source file:io.airlift.drift.transport.netty.codec.HeaderTransport.java

License:Apache License

/**
 * Decodes the ByteBuf into a HeaderFrame transferring the reference ownership.
 * @param buffer buffer to be decoded; reference count ownership is transferred to this method
 * @return the decoded frame; caller is responsible for releasing this object
 *//*from  w  ww . j a va 2  s .c om*/
public static ThriftFrame decodeFrame(ByteBuf buffer) {
    ByteBuf messageHeader = null;
    try {
        // frame header
        short magic = buffer.readShort();
        verify(magic == HEADER_MAGIC, "Invalid header magic");
        short flags = buffer.readShort();
        boolean outOfOrderResponse;
        switch (flags) {
        case FLAGS_NONE:
            outOfOrderResponse = false;
            break;
        case FLAG_SUPPORT_OUT_OF_ORDER:
            outOfOrderResponse = true;
            break;
        default:
            throw new IllegalArgumentException("Unsupported header flags: " + flags);
        }
        int frameSequenceId = buffer.readInt();
        int headerSize = buffer.readShort() << 2;
        messageHeader = buffer.readBytes(headerSize);

        // encoding info
        byte protocolId = messageHeader.readByte();
        Protocol protocol = Protocol.getProtocolByHeaderTransportId(protocolId);
        byte numberOfTransforms = messageHeader.readByte();
        if (numberOfTransforms > 0) {
            // currently there are only two transforms, a cryptographic extension which is deprecated, and gzip which is too expensive
            throw new IllegalArgumentException("Unsupported transform");
        }

        // headers
        // todo what about duplicate headers?
        ImmutableMap.Builder<String, String> allHeaders = ImmutableMap.builder();
        allHeaders.putAll(decodeHeaders(NORMAL_HEADERS, messageHeader));
        allHeaders.putAll(decodeHeaders(PERSISTENT_HEADERS, messageHeader));

        // message
        ByteBuf message = buffer.readBytes(buffer.readableBytes());

        // header frame wraps message byte buffer, so message should not be release yet
        return new ThriftFrame(frameSequenceId, message, allHeaders.build(), HEADER, protocol,
                outOfOrderResponse);
    } finally {
        // message header in an independent buffer and must be released
        if (messageHeader != null) {
            messageHeader.release();
        }

        // input buffer has been consumed and transformed into a HeaderFrame, so release it
        buffer.release();
    }
}

From source file:io.airlift.drift.transport.netty.codec.TestThriftFramedDecoder.java

License:Apache License

private static byte[] createTestFrame(ByteBufAllocator allocator, String methodName, int sequenceId,
        byte messageType) throws Exception {
    TChannelBufferOutputTransport transport = new TChannelBufferOutputTransport(allocator);
    try {// ww w  .j  ava 2s.  c  o m
        TProtocolWriter protocolWriter = BINARY.createProtocol(transport);
        protocolWriter.writeMessageBegin(new TMessage(methodName, messageType, sequenceId));

        // write the parameters
        ProtocolWriter writer = new ProtocolWriter(protocolWriter);
        writer.writeStructBegin(methodName + "_args");
        writer.writeStructEnd();

        protocolWriter.writeMessageEnd();
        ByteBuf buffer = transport.getBuffer();
        byte[] result = new byte[buffer.readableBytes()];
        buffer.readBytes(result);
        buffer.release();
        return result;
    } finally {
        transport.release();
    }
}

From source file:io.airlift.drift.transport.netty.HeaderMessageEncoding.java

License:Apache License

@Override
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
public Object readResponse(ByteBuf buffer, int sequenceId, MethodMetadata method) throws Exception {
    short magic = buffer.readShort();
    verify(magic == HEADER_MAGIC, "Unexpected response header magic");
    short flags = buffer.readShort();
    verify(flags == 1, "Unexpected response header flags");

    int frameSequenceId = buffer.readInt();
    if (frameSequenceId != sequenceId) {
        throw new TApplicationException(BAD_SEQUENCE_ID,
                method.getName() + " failed: out of sequence response");
    }// ww  w  . j a  va  2  s. c o  m
    int headerSize = buffer.readShort() << 2;

    ByteBuf messageHeader = buffer.readBytes(headerSize);
    int protocolId = messageHeader.readUnsignedByte();
    verify(protocolId == this.protocolId, "response protocol is different than request protocol");

    int numberOfTransforms = messageHeader.readUnsignedByte();
    verify(numberOfTransforms < 128, "Too many transforms for response");

    boolean gzipCompressed = false;
    for (int i = 0; i < numberOfTransforms; i++) {
        int transform = messageHeader.readUnsignedByte();
        verify(transform == 0x01, "Unsupported response transform");
        gzipCompressed = true;
    }

    // Currently we ignore response headers from the server because there is no API to fetch the headers
    Map<String, String> normalHeaders = decodeHeaders(NORMAL_HEADERS, messageHeader);
    Map<String, String> persistentHeaders = decodeHeaders(PERSISTENT_HEADERS, messageHeader);

    ByteBuf message = buffer.readBytes(buffer.readableBytes());
    if (gzipCompressed) {
        // todo decompress
        throw new TTransportException("gzip compression not implemented");
    }

    return MessageEncoding.decodeResponse(protocolFactory, message, sequenceId, method);
}