Java Code Examples for io.netty.buffer.ByteBuf.nioBuffer()

The following are Jave code examples for showing how to use nioBuffer() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: sstable-adaptor   File: CBUtil.java   Source Code and License Vote up 6 votes
private static String readString(ByteBuf cb, int length)
{
    if (length == 0)
        return "";

    ByteBuffer buffer = cb.nioBuffer(cb.readerIndex(), length);
    try
    {
        String str = decodeString(buffer);
        cb.readerIndex(cb.readerIndex() + length);
        return str;
    }
    catch (IllegalStateException | CharacterCodingException e)
    {
        throw new ProtocolException("Cannot decode string as UTF8: '" + ByteBufferUtil.bytesToHex(buffer) + "'; " + e);
    }
}
 
Example 2
Project: simulacron   File: ByteBufCodec.java   Source Code and License Vote up 6 votes
@Override
public ByteBuffer readBytes(ByteBuf source) {
  int len = readInt(source);
  if (len < 0) {
    return null;
  }

  ByteBuf slice = source.readSlice(len);
  // if direct byte buffer, return underlying nioBuffer.
  if (slice.isDirect()) {
    return slice.nioBuffer();
  }
  // otherwise copy to a byte array and wrap it.
  final byte[] out = new byte[slice.readableBytes()];
  source.getBytes(source.readerIndex(), out, 0, len);
  return ByteBuffer.wrap(out);
}
 
Example 3
Project: reactive-playing   File: MessageEncoder.java   Source Code and License Vote up 6 votes
@SneakyThrows
public static Message mapToMessage(ByteBuf bb) {
    val buf = bb.nioBuffer();

    return new ObjectMapper().readValue(new InputStream() {

        public int read() throws IOException {
            if (!buf.hasRemaining()) {
                return -1;
            }
            return buf.get() & 0xFF;
        }

        public int read(byte[] bytes, int off, int len)
                throws IOException {
            if (!buf.hasRemaining()) {
                return -1;
            }

            len = Math.min(len, buf.remaining());
            buf.get(bytes, off, len);
            return len;
        }
    }, Message.class);
}
 
Example 4
Project: dremio-oss   File: ColumnChunkIncReadStore.java   Source Code and License Vote up 6 votes
private ByteBuffer uncompressPage(PageHeader pageHeader, boolean isDataPage) throws IOException {
  final ByteBuf src = allocator.buffer(pageHeader.compressed_page_size);
  try {
    ByteBuffer srcBuffer = src.nioBuffer(0, pageHeader.compressed_page_size);
    readFully(srcBuffer, pageHeader.compressed_page_size);

    ByteBuf dest = allocator.buffer(pageHeader.uncompressed_page_size);
    ByteBuffer destBuffer = dest.nioBuffer(0, pageHeader.uncompressed_page_size);
    decompressor.decompress(
        srcBuffer,
        pageHeader.compressed_page_size,
        destBuffer,
        pageHeader.uncompressed_page_size
    );
    if (isDataPage) {
      lastDataPageUncompressed = dest;
    } else {
      dictionaryPageUncompressed = dest;
    }
    return destBuffer;
  } finally {
    src.release(); // we don't need this anymore
  }
}
 
Example 5
Project: QDrill   File: ParquetDirectByteBufferAllocator.java   Source Code and License Vote up 5 votes
@Override
public ByteBuffer allocate(int sz) {
  ByteBuf bb = allocator.buffer(sz);
  ByteBuffer b = bb.nioBuffer(0, sz);
  final Key key = new Key(b);
  allocatedBuffers.put(key, bb);
  logger.debug("ParquetDirectByteBufferAllocator: Allocated {} bytes. Allocated ByteBuffer id: {}", sz, key.hash);
  return b;
}
 
Example 6
Project: spark_deep   File: MessageWithHeader.java   Source Code and License Vote up 5 votes
private int copyByteBuf(ByteBuf buf, WritableByteChannel target) throws IOException {
  ByteBuffer buffer = buf.nioBuffer();
  int written = (buffer.remaining() <= NIO_BUFFER_LIMIT) ?
    target.write(buffer) : writeNioBuffer(target, buffer);
  buf.skipBytes(written);
  return written;
}
 
Example 7
Project: dremio-oss   File: ParquetDirectByteBufferAllocator.java   Source Code and License Vote up 5 votes
@Override
  public ByteBuffer allocate(int sz) {
    ByteBuf bb = allocator.buffer(sz);
    ByteBuffer b = bb.nioBuffer(0, sz);
    final Key key = new Key(b);
    allocatedBuffers.put(key, bb);
//    logger.debug("ParquetDirectByteBufferAllocator: Allocated {} bytes. Allocated ByteBuffer id: {}", sz, key.hash);
    return b;
  }