List of usage examples for io.netty.buffer ByteBuf nioBufferCount
public abstract int nioBufferCount();
From source file:alluxio.network.protocol.databuffer.DataNettyBuffer.java
License:Apache License
/** * Constructor for creating a DataNettyBuffer, by passing a Netty ByteBuf. * This way we avoid one copy from ByteBuf to another ByteBuffer, * and making sure the buffer would not be recycled. * IMPORTANT: {@link #release()} must be called after * reading is finished. Otherwise the memory space for the ByteBuf might never be reclaimed. * * @param bytebuf The ByteBuf having the data * @param length The length of the underlying ByteBuffer data */// w w w . jav a2 s. c o m public DataNettyBuffer(ByteBuf bytebuf, long length) { // throws exception if there are multiple nioBuffers, or reference count is not 1 Preconditions.checkArgument(bytebuf.nioBufferCount() == 1, "Number of nioBuffers of this bytebuf is %s (1 expected).", bytebuf.nioBufferCount()); Preconditions.checkArgument(bytebuf.refCnt() == 1, "Reference count of this bytebuf is %s (1 expected).", bytebuf.refCnt()); // increase the bytebuf reference count so it would not be recycled by Netty bytebuf.retain(); mNettyBuf = bytebuf; mBuffer = bytebuf.nioBuffer(); mLength = 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:divconq.http.multipart.AbstractDiskHttpData.java
License:Apache License
@Override public void addContent(ByteBuf buffer, boolean last) throws IOException { if (buffer != null) { try {//from w w w . j a v a2 s .co m int localsize = buffer.readableBytes(); checkSize(size + localsize); if (definedSize > 0 && definedSize < size + localsize) { throw new IOException("Out of size: " + (size + localsize) + " > " + definedSize); } ByteBuffer byteBuffer = buffer.nioBufferCount() == 1 ? buffer.nioBuffer() : buffer.copy().nioBuffer(); int written = 0; if (file == null) { file = tempFile(); } if (fileChannel == null) { FileOutputStream outputStream = new FileOutputStream(file); fileChannel = outputStream.getChannel(); } while (written < localsize) { written += fileChannel.write(byteBuffer); } size += localsize; buffer.readerIndex(buffer.readerIndex() + written); } finally { // Release the buffer as it was retained before and we not need a reference to it at all // See https://github.com/netty/netty/issues/1516 buffer.release(); } } if (last) { if (file == null) { file = tempFile(); } if (fileChannel == null) { FileOutputStream outputStream = new FileOutputStream(file); fileChannel = outputStream.getChannel(); } fileChannel.force(false); fileChannel.close(); fileChannel = null; setCompleted(); } else { if (buffer == null) { throw new NullPointerException("buffer"); } } }
From source file:io.advantageous.conekt.file.impl.AsyncFileImpl.java
License:Open Source License
private synchronized AsyncFile doWrite(Buffer buffer, long position, Handler<AsyncResult<Void>> handler) { Objects.requireNonNull(buffer, "buffer"); Arguments.require(position >= 0, "position must be >= 0"); check();/*w w w. j a v a2s . c o m*/ Handler<AsyncResult<Void>> wrapped = ar -> { if (ar.succeeded()) { checkContext(); checkDrained(); if (writesOutstanding == 0 && closedDeferred != null) { closedDeferred.run(); } if (handler != null) { handler.handle(ar); } } else { if (handler != null) { handler.handle(ar); } else { handleException(ar.cause()); } } }; ByteBuf buf = buffer.getByteBuf(); if (buf.nioBufferCount() > 1) { doWrite(buf.nioBuffers(), position, wrapped); } else { ByteBuffer bb = buf.nioBuffer(); doWrite(bb, position, bb.limit(), wrapped); } return this; }
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 ww.ja v a 2 s .c o m // 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);//from w ww. j a v a 2s . co m if (buf.nioBufferCount() == 1) { decodeSingleNioBuffer(buf.internalNioBuffer(buf.readerIndex(), length).duplicate()); } else { decode(buf.nioBuffers()); } charBuffer.flip(); }
From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoder.java
License:Apache License
private void inspectByteBufs(ByteBuf[] bufs) { for (ByteBuf buf : bufs) { if (!buf.hasArray()) { withoutArray = true;// www. jav a2s .co m break; } totalSize += buf.readableBytes(); totalNioBuffers += buf.nioBufferCount(); } }
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.ja v a 2s. c o m 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); }