/* * Copyright 2012 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package info.jerrinot.nettyloc; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.ClosedChannelException; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf { private final ByteBufAllocator alloc; private long memoryAddress; private ByteBuffer buffer; private ByteBuffer tmpNioBuf; private int capacity; private boolean doNotFree; /** * Creates a new direct buffer. * * @param capacity the capacity of the underlying direct buffer */ protected UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, int capacity) { if (alloc == null) { throw new NullPointerException("alloc"); } if (capacity < 0) { throw new IllegalArgumentException("capacity: " + capacity); } this.alloc = alloc; setByteBuffer(ByteBuffer.allocateDirect(capacity)); } private void setByteBuffer(ByteBuffer buffer) { ByteBuffer oldBuffer = this.buffer; if (oldBuffer != null) { if (doNotFree) { doNotFree = false; } else { PlatformDependent.freeDirectBuffer(oldBuffer); } } this.buffer = buffer; memoryAddress = PlatformDependent.directBufferAddress(buffer); tmpNioBuf = null; capacity = buffer.remaining(); } @Override public int capacity() { return capacity; } @Override public ByteBufAllocator alloc() { return alloc; } @Override public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { checkIndex(index, length); if (dst == null) { throw new NullPointerException("dst"); } if (dstIndex < 0 || dstIndex > dst.length - length) { throw new IndexOutOfBoundsException(String.format( "dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length)); } if (length != 0) { PlatformDependent.copyMemory(addr(index), dst, dstIndex, length); } return this; } @Override public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { checkIndex(index, length); if (length != 0) { PlatformDependent.copyMemory(src, srcIndex, addr(index), length); } return this; } @Override public ByteBuf setBytes(int index, ByteBuffer src) { ensureAccessible(); ByteBuffer tmpBuf = internalNioBuffer(); if (src == tmpBuf) { src = src.duplicate(); } tmpBuf.clear().position(index).limit(index + src.remaining()); tmpBuf.put(src); return this; } @Override public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { ensureAccessible(); if (length != 0) { byte[] tmp = new byte[length]; PlatformDependent.copyMemory(addr(index), tmp, 0, length); out.write(tmp); } return this; } @Override public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { return getBytes(index, out, length, false); } private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException { ensureAccessible(); if (length == 0) { return 0; } ByteBuffer tmpBuf; if (internal) { tmpBuf = internalNioBuffer(); } else { tmpBuf = buffer.duplicate(); } tmpBuf.clear().position(index).limit(index + length); return out.write(tmpBuf); } @Override public int setBytes(int index, InputStream in, int length) throws IOException { checkIndex(index, length); byte[] tmp = new byte[length]; int readBytes = in.read(tmp); if (readBytes > 0) { PlatformDependent.copyMemory(tmp, 0, addr(index), readBytes); } return readBytes; } @Override public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { ensureAccessible(); ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(index).limit(index + length); try { return in.read(tmpBuf); } catch (ClosedChannelException e) { return -1; } } @Override public int nioBufferCount() { return 1; } @Override public ByteBuffer[] nioBuffers(int index, int length) { return new ByteBuffer[] { nioBuffer(index, length) }; } private ByteBuffer internalNioBuffer() { ByteBuffer tmpNioBuf = this.tmpNioBuf; if (tmpNioBuf == null) { this.tmpNioBuf = tmpNioBuf = buffer.duplicate(); } return tmpNioBuf; } @Override public ByteBuffer nioBuffer(int index, int length) { return (ByteBuffer) buffer.duplicate().position(index).limit(index + length); } @Override protected void deallocate() { ByteBuffer buffer = this.buffer; if (buffer == null) { return; } this.buffer = null; if (!doNotFree) { PlatformDependent.freeDirectBuffer(buffer); } } @Override public ByteBuf unwrap() { return null; } long addr(int index) { return memoryAddress + index; } }