Example usage for io.netty.buffer ByteBuf arrayOffset

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

Introduction

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

Prototype

public abstract int arrayOffset();

Source Link

Document

Returns the offset of the first byte within the backing byte array of this buffer.

Usage

From source file:uk.co.thinkofdeath.prismarine.network.CipherCodec.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    byte[] data;//from   w  ww. j a  v  a 2s  .co m
    int offset = 0;
    int dataSize = in.readableBytes();
    if (!in.isDirect()) {
        data = in.array();
        offset = in.arrayOffset();
        in.skipBytes(in.readableBytes());
    } else {
        if (deDataBuffer.length < dataSize) {
            deDataBuffer = new byte[dataSize];
        }
        in.readBytes(deDataBuffer, 0, dataSize);
        data = deDataBuffer;
    }

    int size = cipherDecrypt.getOutputSize(dataSize);
    ByteBuf buf = ctx.alloc().heapBuffer(size);
    buf.writerIndex(cipherDecrypt.update(data, offset, dataSize, buf.array(), buf.arrayOffset()));
    out.add(buf);
}

From source file:uk.co.thinkofdeath.prismarine.network.CompressionCodec.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
    MCByteBuf buf = new MCByteBuf(out);
    if (msg.readableBytes() < threshold) {
        buf.writeVarInt(0);/*from w w w.  j a v a2  s .  c o m*/
        buf.writeBytes(msg);
    } else {
        CompressionInfo ci = info.get();

        byte[] data;
        int offset = 0;
        int dataSize;
        // Handle both direct and heap buffers
        // heap buffers are faster in this case as they
        // do not require a copy
        if (!msg.isDirect()) {
            data = msg.array();
            offset = msg.arrayOffset();
            dataSize = msg.readableBytes();
            msg.skipBytes(msg.readableBytes());
        } else {
            dataSize = msg.readableBytes();
            if (ci.dataBuffer.length < dataSize) {
                ci.dataBuffer = new byte[dataSize];
            }
            msg.readBytes(ci.dataBuffer, 0, dataSize);
            data = ci.dataBuffer;
        }

        buf.writeVarInt(dataSize);

        ci.deflater.setInput(data, offset, dataSize);
        ci.deflater.finish();
        while (!ci.deflater.finished()) {
            int count = ci.deflater.deflate(ci.compBuffer);
            out.writeBytes(ci.compBuffer, 0, count);
        }
        ci.deflater.reset();
    }
}

From source file:uk.co.thinkofdeath.prismarine.network.CompressionCodec.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    MCByteBuf buf = new MCByteBuf(in);
    int size = buf.readVarInt();
    if (size == 0) {
        out.add(buf.readBytes(buf.readableBytes()));
    } else {/*from w  w  w  .  java2  s .  c om*/
        CompressionInfo ci = info.get();
        if (ci.decompBuffer.length < in.readableBytes()) {
            ci.decompBuffer = new byte[in.readableBytes()];
        }
        int count = in.readableBytes();
        in.readBytes(ci.decompBuffer, 0, count);
        ci.inflater.setInput(ci.decompBuffer, 0, count);

        // Use heap buffers so we can just access the internal array
        ByteBuf oBuf = ctx.alloc().heapBuffer(size);
        oBuf.writerIndex(ci.inflater.inflate(oBuf.array(), oBuf.arrayOffset(), size));
        out.add(oBuf);
        ci.inflater.reset();
    }
}