Example usage for io.netty.buffer ByteBuf readerIndex

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

Introduction

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

Prototype

public abstract int readerIndex();

Source Link

Document

Returns the readerIndex of this buffer.

Usage

From source file:com.yahoo.pulsar.common.compression.CompressionCodecLZ4.java

License:Apache License

@Override
public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException {
    ByteBuf uncompressed = PooledByteBufAllocator.DEFAULT.buffer(uncompressedLength, uncompressedLength);
    ByteBuffer uncompressedNio = uncompressed.nioBuffer(0, uncompressedLength);

    ByteBuffer encodedNio = encoded.nioBuffer(encoded.readerIndex(), encoded.readableBytes());
    decompressor.decompress(encodedNio, encodedNio.position(), uncompressedNio, uncompressedNio.position(),
            uncompressedNio.remaining());

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}

From source file:com.yahoo.pulsar.common.compression.CompressionCodecZLib.java

License:Apache License

@Override
public ByteBuf encode(ByteBuf source) {
    byte[] array;
    int length = source.readableBytes();

    int sizeEstimate = (int) Math.ceil(source.readableBytes() * 1.001) + 14;
    ByteBuf compressed = PooledByteBufAllocator.DEFAULT.heapBuffer(sizeEstimate);

    int offset = 0;
    if (source.hasArray()) {
        array = source.array();/*from w w  w  .  ja  va2s .  co  m*/
        offset = source.arrayOffset() + source.readerIndex();
    } else {
        // If it's a direct buffer, we need to copy it
        array = new byte[length];
        source.getBytes(source.readerIndex(), array);
    }

    synchronized (deflater) {
        deflater.setInput(array, offset, length);
        while (!deflater.needsInput()) {
            deflate(compressed);
        }

        deflater.reset();
    }

    return compressed;
}

From source file:com.yahoo.pulsar.common.compression.CompressionCodecZLib.java

License:Apache License

@Override
public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException {
    ByteBuf uncompressed = PooledByteBufAllocator.DEFAULT.heapBuffer(uncompressedLength, uncompressedLength);

    int len = encoded.readableBytes();

    byte[] array;
    int offset;//w  w  w .  j a  v a  2s  .  com
    if (encoded.hasArray()) {
        array = encoded.array();
        offset = encoded.arrayOffset() + encoded.readerIndex();
    } else {
        array = new byte[len];
        encoded.getBytes(encoded.readerIndex(), array);
        offset = 0;
    }

    int resultLength;
    synchronized (inflater) {
        inflater.setInput(array, offset, len);
        try {
            resultLength = inflater.inflate(uncompressed.array(), uncompressed.arrayOffset(),
                    uncompressedLength);
        } catch (DataFormatException e) {
            throw new IOException(e);
        }
        inflater.reset();
    }

    checkArgument(resultLength == uncompressedLength);

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}

From source file:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32cHardware() {
    if (HARDWARE_CRC32C_HASH == null) {
        return;// w w w  .  ja  va 2  s.  c  o  m
    }

    ByteBuf payload = Unpooled.wrappedBuffer(inputBytes);

    // compute checksum using sse4.2 hw instruction
    int hw = HARDWARE_CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(),
            payload.readableBytes());
    assertEquals(hw, expectedChecksum);
}

From source file:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32cSoftware() {
    ByteBuf payload = Unpooled.wrappedBuffer(inputBytes);

    // compute checksum using sw algo
    int sw = SOFTWARE_CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(),
            payload.readableBytes());/*from  ww w  . ja  v a2s  .  c o m*/
    assertEquals(sw, expectedChecksum);
}

From source file:com.yahoo.pulsar.common.util.XXHashChecksum.java

License:Apache License

public static long computeChecksum(ByteBuf payload) {
    if (payload.hasArray()) {
        return checksum.hash(payload.array(), payload.arrayOffset() + payload.readerIndex(),
                payload.readableBytes(), 0L);
    } else {//from w w w. ja  v  a2s . c om
        ByteBuffer payloadNio = payload.nioBuffer(payload.readerIndex(), payload.readableBytes());
        return checksum.hash(payloadNio, 0, payload.readableBytes(), 0L);
    }
}

From source file:com.zxcc.socket.protobuf.ProtobufVarint32LengthFieldPrepender.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
    int bodyLen = msg.readableBytes();
    //        int headerLen = CodedOutputStream.computeRawVarint32Size(bodyLen);
    out.ensureWritable(bodyLen + 4);/* ww  w  .  jav  a 2  s.  c om*/

    //        CodedOutputStream headerOut =
    //                CodedOutputStream.newInstance(new ByteBufOutputStream(out), headerLen);
    ////        headerOut.writeInt32NoTag(bodyLen);
    //        headerOut.writeFixed32NoTag(bodyLen);
    ////        headerOut.writeRawVarint32(bodyLen);
    //        headerOut.flush();
    out.writeInt(bodyLen);
    out.writeBytes(msg, msg.readerIndex(), bodyLen);
}

From source file:com.zz.learning.netty5.chap12.codec.MarshallingDecoder.java

License:Apache License

/**
 * ??/* w  w  w .  j  av a2 s  .c  om*/
 * @param in
 * @return
 * @throws Exception
 */
protected Object decode(ByteBuf in) throws Exception {
    int objectSize = in.readInt();
    /*
     * ?ByteBuf??
     */
    ByteBuf buf = in.slice(in.readerIndex(), objectSize);
    ByteInput input = new ChannelBufferByteInput(buf);
    try {
        unmarshaller.start(input);
        Object obj = unmarshaller.readObject();
        unmarshaller.finish();
        in.readerIndex(in.readerIndex() + objectSize); //
        return obj;
    } finally {
        unmarshaller.close();
    }
}

From source file:dan200.qcraft.shared.QCraftPacket.java

License:Open Source License

@Override
public void fromBytes(ByteBuf buffer) {
    packetType = buffer.readByte();//  w  ww .  ja v  a  2s .  c  om
    byte nString = buffer.readByte();
    byte nInt = buffer.readByte();
    int nByte = buffer.readInt();
    if (nString == 0) {
        dataString = null;
    } else {
        dataString = new String[nString];
        for (int k = 0; k < nString; k++) {
            if (buffer.readBoolean()) {
                int len = buffer.readInt();
                byte[] b = new byte[len];
                buffer.readBytes(b);
                try {
                    dataString[k] = new String(b, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    dataString[k] = null;
                }
            }
        }
    }
    if (nInt == 0) {
        dataInt = null;
    } else {
        dataInt = new int[nInt];
        for (int k = 0; k < nInt; k++) {
            dataInt[k] = buffer.readInt();
        }
    }
    if (nByte == 0) {
        dataByte = null;
    } else {
        dataByte = new byte[nByte][];
        for (int k = 0; k < nByte; k++) {
            int length = buffer.readInt();
            if (length > 0) {
                dataByte[k] = new byte[length];
                buffer.getBytes(buffer.readerIndex(), dataByte[k]);
            }
        }
    }
}

From source file:de.dfki.kiara.http.HttpHandler.java

License:Open Source License

@Override
protected void channelRead0(final ChannelHandlerContext ctx, Object msg) throws Exception {
    logger.debug("Handler: {} / Channel: {}", this, ctx.channel());
    if (mode == Mode.SERVER) {
        if (msg instanceof FullHttpRequest) {
            final FullHttpRequest request = (FullHttpRequest) msg;

            HttpRequestMessage transportMessage = new HttpRequestMessage(this, request);
            transportMessage.setPayload(request.content().nioBuffer());

            if (logger.isDebugEnabled()) {
                logger.debug("RECEIVED REQUEST WITH CONTENT {}",
                        Util.bufferToString(transportMessage.getPayload()));
            }// www  .j  a v a 2s  .c om

            synchronized (listeners) {
                if (!listeners.isEmpty()) {
                    for (TransportMessageListener listener : listeners) {
                        listener.onMessage(transportMessage);
                    }
                }
            }

            boolean keepAlive = HttpHeaders.isKeepAlive(request);
        }
    } else {
        // CLIENT
        if (msg instanceof HttpResponse) {
            HttpResponse response = (HttpResponse) msg;
            headers = response.headers();
            //if (!response.headers().isEmpty()) {
            //    contentType = response.headers().get("Content-Type");
            //}
        }
        if (msg instanceof HttpContent) {
            HttpContent content = (HttpContent) msg;
            ByteBuf buf = content.content();
            if (buf.isReadable()) {
                if (buf.hasArray()) {
                    bout.write(buf.array(), buf.readerIndex(), buf.readableBytes());
                } else {
                    byte[] bytes = new byte[buf.readableBytes()];
                    buf.getBytes(buf.readerIndex(), bytes);
                    bout.write(bytes);
                }
            }
            if (content instanceof LastHttpContent) {
                //ctx.close();
                bout.flush();
                HttpResponseMessage response = new HttpResponseMessage(this, headers);
                response.setPayload(ByteBuffer.wrap(bout.toByteArray(), 0, bout.size()));
                onResponse(response);
                bout.reset();
            }
        }
    }
}