Example usage for io.netty.buffer ByteBuf alloc

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

Introduction

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

Prototype

public abstract ByteBufAllocator alloc();

Source Link

Document

Returns the ByteBufAllocator which created this buffer.

Usage

From source file:at.yawk.dbus.protocol.object.ArrayObject.java

/**
 * Allocate a write buffer that will be written to the given target. Inherits endianness.
 *///from w ww . ja  v  a  2 s.c om
static ByteBuf allocateBufferForWrite(ByteBuf writeTarget) {
    return writeTarget.alloc().buffer().order(writeTarget.order());
}

From source file:com.datastax.driver.core.LZ4Compressor.java

License:Apache License

private ByteBuf compressDirect(ByteBuf input) throws IOException {
    int maxCompressedLength = compressor.maxCompressedLength(input.readableBytes());
    // If the input is direct we will allocate a direct output buffer as well as this will allow us to use
    // LZ4Compressor.compress and so eliminate memory copies.
    ByteBuf output = input.alloc().directBuffer(INTEGER_BYTES + maxCompressedLength);
    try {/*from   ww  w.  ja  v a 2s.com*/
        ByteBuffer in = inputNioBuffer(input);
        // Increase reader index.
        input.readerIndex(input.writerIndex());

        output.writeInt(in.remaining());

        ByteBuffer out = outputNioBuffer(output);
        int written = compressor.compress(in, in.position(), in.remaining(), out, out.position(),
                out.remaining());
        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + written);
    } catch (Exception e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw new IOException(e);
    }
    return output;
}

From source file:com.datastax.driver.core.LZ4Compressor.java

License:Apache License

private ByteBuf compressHeap(ByteBuf input) throws IOException {
    int maxCompressedLength = compressor.maxCompressedLength(input.readableBytes());

    // Not a direct buffer so use byte arrays...
    int inOffset = input.arrayOffset() + input.readerIndex();
    byte[] in = input.array();
    int len = input.readableBytes();
    // Increase reader index.
    input.readerIndex(input.writerIndex());

    // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so
    // can eliminate the overhead of allocate a new byte[].
    ByteBuf output = input.alloc().heapBuffer(INTEGER_BYTES + maxCompressedLength);
    try {/*  www.  ja v  a2  s .  c  om*/
        output.writeInt(len);
        // calculate the correct offset.
        int offset = output.arrayOffset() + output.writerIndex();
        byte[] out = output.array();
        int written = compressor.compress(in, inOffset, len, out, offset);

        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + written);
    } catch (Exception e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw new IOException(e);
    }
    return output;
}

From source file:com.datastax.driver.core.LZ4Compressor.java

License:Apache License

private ByteBuf decompressDirect(ByteBuf input) throws IOException {
    // If the input is direct we will allocate a direct output buffer as well as this will allow us to use
    // LZ4Compressor.decompress and so eliminate memory copies.
    int readable = input.readableBytes();
    int uncompressedLength = input.readInt();
    ByteBuffer in = inputNioBuffer(input);
    // Increase reader index.
    input.readerIndex(input.writerIndex());
    ByteBuf output = input.alloc().directBuffer(uncompressedLength);
    try {//from  w w  w.j a  v  a  2  s . c  o m
        ByteBuffer out = outputNioBuffer(output);
        int read = decompressor.decompress(in, in.position(), out, out.position(), out.remaining());
        if (read != readable - INTEGER_BYTES)
            throw new IOException("Compressed lengths mismatch");

        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + uncompressedLength);
    } catch (Exception e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw new IOException(e);
    }
    return output;
}

From source file:com.datastax.driver.core.LZ4Compressor.java

License:Apache License

private ByteBuf decompressHeap(ByteBuf input) throws IOException {
    // Not a direct buffer so use byte arrays...
    byte[] in = input.array();
    int len = input.readableBytes();
    int uncompressedLength = input.readInt();
    int inOffset = input.arrayOffset() + input.readerIndex();
    // Increase reader index.
    input.readerIndex(input.writerIndex());

    // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so
    // can eliminate the overhead of allocate a new byte[].
    ByteBuf output = input.alloc().heapBuffer(uncompressedLength);
    try {//  w w  w.  j ava2 s.  c  o  m
        int offset = output.arrayOffset() + output.writerIndex();
        byte out[] = output.array();
        int read = decompressor.decompress(in, inOffset, out, offset, uncompressedLength);
        if (read != len - INTEGER_BYTES)
            throw new IOException("Compressed lengths mismatch");

        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + uncompressedLength);
    } catch (Exception e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw new IOException(e);
    }
    return output;
}

From source file:com.datastax.driver.core.SnappyCompressor.java

License:Apache License

private ByteBuf compressDirect(ByteBuf input) throws IOException {
    int maxCompressedLength = Snappy.maxCompressedLength(input.readableBytes());
    // If the input is direct we will allocate a direct output buffer as well as this will allow us to use
    // Snappy.compress(ByteBuffer, ByteBuffer) and so eliminate memory copies.
    ByteBuf output = input.alloc().directBuffer(maxCompressedLength);
    try {/*w  w w.  j a v a2  s  .  com*/
        ByteBuffer in = inputNioBuffer(input);
        // Increase reader index.
        input.readerIndex(input.writerIndex());

        ByteBuffer out = outputNioBuffer(output);
        int written = Snappy.compress(in, out);
        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + written);
    } catch (IOException e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw e;
    }
    return output;
}

From source file:com.datastax.driver.core.SnappyCompressor.java

License:Apache License

private ByteBuf compressHeap(ByteBuf input) throws IOException {
    int maxCompressedLength = Snappy.maxCompressedLength(input.readableBytes());
    int inOffset = input.arrayOffset() + input.readerIndex();
    byte[] in = input.array();
    int len = input.readableBytes();
    // Increase reader index.
    input.readerIndex(input.writerIndex());

    // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so
    // can eliminate the overhead of allocate a new byte[].
    ByteBuf output = input.alloc().heapBuffer(maxCompressedLength);
    try {/*from w  w w .j  a v  a 2  s . co m*/
        // Calculate the correct offset.
        int offset = output.arrayOffset() + output.writerIndex();
        byte[] out = output.array();
        int written = Snappy.compress(in, inOffset, len, out, offset);

        // Increase the writerIndex with the written bytes.
        output.writerIndex(output.writerIndex() + written);
    } catch (IOException e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw e;
    }
    return output;
}

From source file:com.datastax.driver.core.SnappyCompressor.java

License:Apache License

private ByteBuf decompressDirect(ByteBuf input) throws IOException {
    ByteBuffer in = inputNioBuffer(input);
    // Increase reader index.
    input.readerIndex(input.writerIndex());

    if (!Snappy.isValidCompressedBuffer(in))
        throw new DriverInternalError("Provided frame does not appear to be Snappy compressed");

    // If the input is direct we will allocate a direct output buffer as well as this will allow us to use
    // Snappy.compress(ByteBuffer, ByteBuffer) and so eliminate memory copies.
    ByteBuf output = input.alloc().directBuffer(Snappy.uncompressedLength(in));
    try {// w ww.j  a  va  2 s. c  o  m
        ByteBuffer out = outputNioBuffer(output);

        int size = Snappy.uncompress(in, out);
        // Set the writer index so the amount of written bytes is reflected
        output.writerIndex(output.writerIndex() + size);
    } catch (IOException e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw e;
    }
    return output;
}

From source file:com.datastax.driver.core.SnappyCompressor.java

License:Apache License

private ByteBuf decompressHeap(ByteBuf input) throws IOException {
    // Not a direct buffer so use byte arrays...
    int inOffset = input.arrayOffset() + input.readerIndex();
    byte[] in = input.array();
    int len = input.readableBytes();
    // Increase reader index.
    input.readerIndex(input.writerIndex());

    if (!Snappy.isValidCompressedBuffer(in, inOffset, len))
        throw new DriverInternalError("Provided frame does not appear to be Snappy compressed");

    // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so
    // can eliminate the overhead of allocate a new byte[].
    ByteBuf output = input.alloc().heapBuffer(Snappy.uncompressedLength(in, inOffset, len));
    try {//  ww w. j a v a  2  s  .c o  m
        // Calculate the correct offset.
        int offset = output.arrayOffset() + output.writerIndex();
        byte[] out = output.array();
        int written = Snappy.uncompress(in, inOffset, len, out, offset);

        // Increase the writerIndex with the written bytes.
        output.writerIndex(output.writerIndex() + written);
    } catch (IOException e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw e;
    }
    return output;
}

From source file:com.ogarproject.ogar.server.net.packet.Packet.java

License:Open Source License

public static String readUTF8(ByteBuf in) {
    ByteBuf buffer = in.alloc().buffer();
    byte b;/*from   ww w . j  ava 2 s  .c  o  m*/
    while (in.readableBytes() > 0 && (b = in.readByte()) != 0) {
        buffer.writeByte(b);
    }

    return buffer.toString(Charsets.UTF_8);
}