Returns an off-heap copy of the specified {@link ByteBuf}, and releases the original one.
Note that this method does not create an off-heap copy if the allocation / deallocation cost is too high,
but just returns the original {@link ByteBuf}..
422916
protected final ByteBuf newDirectBuffer(ByteBuf buf) {
        final int readableBytes = buf.readableBytes();
        if (readableBytes == 0) {
            ReferenceCountUtil.safeRelease(buf);
            return Unpooled.EMPTY_BUFFER;
        }

        final ByteBufAllocator alloc = alloc();
        if (alloc.isDirectBufferPooled()) {
            ByteBuf directBuf = alloc.directBuffer(readableBytes);
            directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
            ReferenceCountUtil.safeRelease(buf);
            return directBuf;
        }

        final ByteBuf directBuf = ByteBufUtil.threadLocalDirectBuffer();
        if (directBuf != null) {
            directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
            ReferenceCountUtil.safeRelease(buf);
            return directBuf;
        }

        // Allocating and deallocating an unpooled direct buffer is very expensive; give up.
        return buf;
    }
1003346
@NotNull
	public ByteBuf take() {
		assert hasRemaining();
		ByteBuf buf = bufs[first];
		first = next(first);
		return buf;
	}
90985
public static ByteArrayInputStream toStream(ByteBuffer byteBuffer) {
        if (byteBuffer == null) {
            return new ByteArrayInputStream(new byte[0]);
        }
        return new ByteArrayInputStream(copyBytesFrom(byteBuffer));
    }
616197
@SuppressWarnings("UnusedParameters")
    protected ByteBuf extractObject(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) {
        return buffer.retainedSlice(index, length);
    }
551288
public static ByteBuf wrappedBuffer(ByteBuffer buffer) {
        if (!buffer.hasRemaining()) {
            return EMPTY_BUFFER;
        }
        if (!buffer.isDirect() && buffer.hasArray()) {
            return wrappedBuffer(
                    buffer.array(),
                    buffer.arrayOffset() + buffer.position(),
                    buffer.remaining()).order(buffer.order());
        } else if (PlatformDependent.hasUnsafe()) {
            if (buffer.isReadOnly()) {
                if (buffer.isDirect()) {
                    return new ReadOnlyUnsafeDirectByteBuf(ALLOC, buffer);
                } else {
                    return new ReadOnlyByteBufferBuf(ALLOC, buffer);
                }
            } else {
                return new UnpooledUnsafeDirectByteBuf(ALLOC, buffer, buffer.remaining());
            }
        } else {
            if (buffer.isReadOnly()) {
                return new ReadOnlyByteBufferBuf(ALLOC, buffer);
            }  else {
                return new UnpooledDirectByteBuf(ALLOC, buffer, buffer.remaining());
            }
        }
    }
458945
public byte[] getBytes() {
        byte[] retval = new byte[buffer.b().position()];
        int position = buffer.b().position();
        buffer.b().rewind();
        buffer.b().get(retval);
        assert position == buffer.b().position();
        return retval;
    }
379366
public static IoBuffer copy(ByteBuffer src) {
        IoBuffer copy = IoBuffer.allocate(src.remaining());
        copy.put(src);
        copy.flip();
        return copy;
    }
1128613
protected ByteBuffer getByteBuffer(int bufferLength) {
        if(_buffer == null) {
            _buffer = ByteBuffer.wrap(new byte[bufferLength]);
            _log.info("ByteBuffer allocated for buffering");
        }
        
        return _buffer;
    }
16267
public static byte[] copyByteBufToByteArray(ByteBuf input) {
        byte[] copy;
        int length = input.readableBytes();
        if (input.hasArray()) {
            byte[] inputBytes = input.array();
            int offset = input.arrayOffset() + input.readerIndex();
            copy = Arrays.copyOfRange(inputBytes, offset, offset + length);
            return copy;
        } else {
            copy = new byte[length];
            input.getBytes(input.readerIndex(), copy);
        }
        return copy;
    }
9489
@Override
    public void releaseBuffer ( byte[] buf ) {
        // better safe than sorry: prevent leaks if there is some out of bound access
        Arrays.fill(buf, (byte) 0);
        synchronized ( this.cache ) {
            if ( this.freeBuffers < this.cache.length ) {
                for ( int i = 0; i < this.cache.length; i++ ) {
                    if ( this.cache[ i ] == null ) {
                        this.cache[ i ] = buf;
                        this.freeBuffers++;
                        return;
                    }
                }
            }
        }
    }
533965
private ByteBuf allocateOutNetBuf(ChannelHandlerContext ctx, int pendingBytes, int numComponents) {
        return allocate(ctx, engineType.calculateWrapBufferCapacity(this, pendingBytes, numComponents));
    }
729651
public static byte[] copyOf(byte[] byteArray) {

    	byte[] copy = new byte[byteArray.length];
        System.arraycopy(byteArray, 0, copy, 0, byteArray.length);
        return copy;
    }
673436
public int getBytes(byte[] buf, int offset, int length)
    {
        if (length>unconsumedData())
            length=unconsumedData();
        System.arraycopy(_buf,_pos,buf,offset,length);
        _pos+=length;
        return length;
    }
650356
private ByteBuf tryCompress(ByteBuf buf) {
        final int uncompressedLength = buf.readableBytes();
        final byte[] compressed = new byte[Snappy.maxCompressedLength(uncompressedLength)];
        final int compressedLength;

        if (buf.hasArray()) {
            // Save a memory copy by reading directly from the backing array.
            compressedLength = Snappy.compress(
                    buf.array(), buf.arrayOffset() + buf.readerIndex(), uncompressedLength, compressed, 0);
        } else {
            final byte[] data = new byte[buf.readableBytes()];
            buf.getBytes(buf.readerIndex(), data);
            compressedLength = Snappy.compress(data, 0, data.length, compressed, 0);
        }

        final double ratio = (double) compressedLength / uncompressedLength;
        if (ratio > minCompressionRatio) {
            // Compression doesn't make enough of a difference.
            return null;
        }

        // Save a memory copy by deferring trimming until we know the result will be used.
        final byte[] trimmedCompressed = Arrays.copyOf(compressed, compressedLength);
        return Unpooled.wrappedBuffer(trimmedCompressed);
    }
709875
public synchronized void returnBuf(byte[] buf) {
        if (buf == null || buf.length > mSizeLimit) {
            return;
        }
        mBuffersByLastUse.add(buf);
        int pos = Collections.binarySearch(mBuffersBySize, buf, BUF_COMPARATOR);
        if (pos < 0) {
            pos = -pos - 1;
        }
        mBuffersBySize.add(pos, buf);
        mCurrentSize += buf.length;
        trim();
    }
696183
public synchronized byte[] getBuffer() {
        byte[] dest = new byte[buf.length];
        System.arraycopy(buf, 0, dest, 0, dest.length);
        return dest;
    }
1184118
public ByteBuffer getByteBuffer() {
		final ByteBuffer bb = ByteBuffer.wrap(buf);
		bb.limit(bufLimit);
		bb.position(bufPosition);
		return bb;
	}