Example usage for io.netty.buffer ByteBuf getBytes

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

Introduction

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

Prototype

public abstract ByteBuf getBytes(int index, ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the specified absolute index until the destination's position reaches its limit.

Usage

From source file:ru.calypso.ogar.server.net.packet.c2s.PacketInMouseMove.java

License:Open Source License

@Override
public void readData(ByteBuf buf) {
    ByteBuffer bbuf = ByteBuffer.allocate(16);
    buf.getBytes(1, bbuf);
    x = getFloat64(bbuf.array(), 0);/*from   w w w  . j  av a 2s  .co m*/
    y = getFloat64(bbuf.array(), 8);
    //nodeId = buf.readInt();       
}

From source file:tp.MyJZLibDecoder.java

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    if (!in.isReadable()) {
        return;// w ww .j av  a  2  s.  co  m
    }

    try {
        // Configure input.
        int inputLength = in.readableBytes();
        //totalBytes += inputLength;
        z.avail_in = inputLength;
        if (in.hasArray()) {
            z.next_in = in.array();
            z.next_in_index = in.arrayOffset() + in.readerIndex();
        } else {
            byte[] array = new byte[inputLength];
            in.getBytes(in.readerIndex(), array);
            z.next_in = array;
            z.next_in_index = 0;
        }
        int oldNextInIndex = z.next_in_index;

        // Configure output.
        int maxOutputLength = inputLength << 1;
        ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength);

        try {
            loop: for (;;) {
                z.avail_out = maxOutputLength;
                decompressed.ensureWritable(maxOutputLength);
                z.next_out = decompressed.array();
                z.next_out_index = decompressed.arrayOffset() + decompressed.writerIndex();
                int oldNextOutIndex = z.next_out_index;

                // Decompress 'in' into 'out'
                int resultCode = z.inflate(JZlib.Z_SYNC_FLUSH);
                int outputLength = z.next_out_index - oldNextOutIndex;
                if (outputLength > 0) {
                    decompressed.writerIndex(decompressed.writerIndex() + outputLength);
                }

                switch (resultCode) {
                case JZlib.Z_NEED_DICT:
                    if (dictionary == null) {
                        ZlibUtil.fail(z, "decompression failure", resultCode);
                    } else {
                        resultCode = z.inflateSetDictionary(dictionary, dictionary.length);
                        if (resultCode != JZlib.Z_OK) {
                            ZlibUtil.fail(z, "failed to set the dictionary", resultCode);
                        }
                    }
                    break;
                case JZlib.Z_STREAM_END:
                    finished = true; // Do not decode anymore.
                    z.inflateEnd();
                    break loop;
                case JZlib.Z_OK:
                    break;
                case JZlib.Z_BUF_ERROR:
                    if (z.avail_in <= 0) {
                        //ZlibUtil.fail(z, "decompression failure [Z_BUF_ERROR] ", resultCode);
                        break loop;

                    }
                    break;
                default:
                    ZlibUtil.fail(z, "decompression failure", resultCode);
                }
            }
        } finally {
            in.skipBytes(z.next_in_index - oldNextInIndex);

            //System.err.println("[recived][numBytes] = "+inputLength);
            if (decompressed.isReadable()) {
                out.add(decompressed);
            } else {
                decompressed.release();
            }

        }
    } finally {
        // Deference the external references explicitly to tell the VM that
        // the allocated byte arrays are temporary so that the call stack
        // can be utilized.
        // I'm not sure if the modern VMs do this optimization though.
        z.next_in = null;
        z.next_out = null;
    }
}