List of usage examples for io.netty.buffer ByteBuf internalNioBuffer
public abstract ByteBuffer internalNioBuffer(int index, int length);
From source file:com.datastax.driver.core.FrameCompressor.java
License:Apache License
protected static ByteBuffer inputNioBuffer(ByteBuf buf) { // Using internalNioBuffer(...) as we only hold the reference in this method and so can // reduce Object allocations. int index = buf.readerIndex(); int len = buf.readableBytes(); return buf.nioBufferCount() == 1 ? buf.internalNioBuffer(index, len) : buf.nioBuffer(index, len); }
From source file:com.datastax.driver.core.FrameCompressor.java
License:Apache License
protected static ByteBuffer outputNioBuffer(ByteBuf buf) { int index = buf.writerIndex(); int len = buf.writableBytes(); return buf.nioBufferCount() == 1 ? buf.internalNioBuffer(index, len) : buf.nioBuffer(index, len); }
From source file:io.gatling.netty.util.ahc.ByteBufUtils.java
License:Apache License
private static CharBuffer decode(ByteBuf src, Charset charset) { int readerIndex = src.readerIndex(); int len = src.readableBytes(); final CharsetDecoder decoder = CharsetUtil.decoder(charset); CharBuffer dst = pooledCharBuffer(len, decoder); if (src.nioBufferCount() == 1) { // Use internalNioBuffer(...) to reduce object creation. // BEWARE: NOT THREAD-SAFE decode(decoder, src.internalNioBuffer(readerIndex, len), dst); } else {//from w w w . jav a2s. com // We use a heap buffer as CharsetDecoder is most likely able to use a fast-path if src and dst buffers // are both backed by a byte array. ByteBuf buffer = src.alloc().heapBuffer(len); try { buffer.writeBytes(src, readerIndex, len); // Use internalNioBuffer(...) to reduce object creation. decode(decoder, buffer.internalNioBuffer(buffer.readerIndex(), len), dst); } finally { // Release the temporary buffer again. buffer.release(); } } dst.flip(); return dst; }
From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoder.java
License:Apache License
private void decodeHeap0(ByteBuf buf) { int length = buf.readableBytes(); ensureCapacity(length);/* ww w .j a v a2 s . c o m*/ if (buf.nioBufferCount() == 1) { decodeSingleNioBuffer(buf.internalNioBuffer(buf.readerIndex(), length).duplicate()); } else { decode(buf.nioBuffers()); } charBuffer.flip(); }
From source file:io.grpc.alts.internal.AltsChannelCrypter.java
License:Apache License
@Override public void encrypt(ByteBuf outBuf, List<ByteBuf> plainBufs) throws GeneralSecurityException { checkArgument(outBuf.nioBufferCount() == 1); // Copy plaintext buffers into outBuf for in-place encryption on single direct buffer. ByteBuf plainBuf = outBuf.slice(outBuf.writerIndex(), outBuf.writableBytes()); plainBuf.writerIndex(0);//from w w w.j a v a 2s . com for (ByteBuf inBuf : plainBufs) { plainBuf.writeBytes(inBuf); } verify(outBuf.writableBytes() == plainBuf.readableBytes() + TAG_LENGTH); ByteBuffer out = outBuf.internalNioBuffer(outBuf.writerIndex(), outBuf.writableBytes()); ByteBuffer plain = out.duplicate(); plain.limit(out.limit() - TAG_LENGTH); byte[] counter = incrementOutCounter(); int outPosition = out.position(); aeadCrypter.encrypt(out, plain, counter); int bytesWritten = out.position() - outPosition; outBuf.writerIndex(outBuf.writerIndex() + bytesWritten); verify(!outBuf.isWritable()); }
From source file:io.grpc.alts.internal.AltsChannelCrypter.java
License:Apache License
@Override public void decrypt(ByteBuf out, ByteBuf ciphertextAndTag) throws GeneralSecurityException { int bytesRead = ciphertextAndTag.readableBytes(); checkArgument(bytesRead == out.writableBytes()); checkArgument(out.nioBufferCount() == 1); ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes()); checkArgument(ciphertextAndTag.nioBufferCount() == 1); ByteBuffer ciphertextAndTagBuffer = ciphertextAndTag.nioBuffer(ciphertextAndTag.readerIndex(), bytesRead); byte[] counter = incrementInCounter(); int outPosition = outBuffer.position(); aeadCrypter.decrypt(outBuffer, ciphertextAndTagBuffer, counter); int bytesWritten = outBuffer.position() - outPosition; out.writerIndex(out.writerIndex() + bytesWritten); ciphertextAndTag.readerIndex(out.readerIndex() + bytesRead); verify(out.writableBytes() == TAG_LENGTH); }
From source file:io.grpc.alts.internal.BufUnwrapper.java
License:Apache License
/** * Optimized accessor for obtaining the underlying NIO buffers for a Netty {@link ByteBuf}. Based * on code from Netty's {@code SslHandler}. This method returns NIO buffers that span the readable * region of the {@link ByteBuf}.// ww w . j a va 2s . c om */ private static ByteBuffer[] nioBuffers(ByteBuf buf, ByteBuffer[] singleBuffer) { // As CompositeByteBuf.nioBufferCount() can be expensive (as it needs to check all composed // ByteBuf to calculate the count) we will just assume a CompositeByteBuf contains more than 1 // ByteBuf. The worst that can happen is that we allocate an extra ByteBuffer[] in // CompositeByteBuf.nioBuffers() which is better than walking the composed ByteBuf in most // cases. if (!(buf instanceof CompositeByteBuf) && buf.nioBufferCount() == 1) { // We know its only backed by 1 ByteBuffer so use internalNioBuffer to keep object // allocation to a minimum. singleBuffer[0] = buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()); return singleBuffer; } return buf.nioBuffers(); }
From source file:org.asynchttpclient.netty.util.Utf8ByteBufCharsetDecoder.java
License:Open Source License
public String decode(ByteBuf buf) throws CharacterCodingException { if (buf.isDirect()) { return buf.toString(UTF_8); }/*from ww w. j av a 2 s . co m*/ int length = buf.readableBytes(); ensureCapacity(length); if (buf.nioBufferCount() == 1) { decodeSingleNioBuffer(buf.internalNioBuffer(buf.readerIndex(), length).duplicate(), length); } else { decode(buf.nioBuffers(), buf.readableBytes()); } return charBuffer.flip().toString(); }