2013-01-10 10:27:16 +01:00
|
|
|
/*
|
2013-01-10 10:27:16 +01:00
|
|
|
* Copyright 2013 The Netty Project
|
2013-01-10 10:27:16 +01:00
|
|
|
*
|
|
|
|
* 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 io.netty.buffer;
|
|
|
|
|
2013-06-10 12:52:56 +02:00
|
|
|
import io.netty.util.Recycler;
|
2013-01-11 07:45:22 +01:00
|
|
|
import io.netty.util.internal.PlatformDependent;
|
2013-01-10 10:27:16 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|
|
|
|
|
|
|
private static final boolean NATIVE_ORDER = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
2013-06-10 12:52:56 +02:00
|
|
|
|
|
|
|
private static final Recycler<PooledUnsafeDirectByteBuf> RECYCLER = new Recycler<PooledUnsafeDirectByteBuf>() {
|
|
|
|
@Override
|
2013-12-18 15:05:37 +01:00
|
|
|
protected PooledUnsafeDirectByteBuf newObject(Handle<PooledUnsafeDirectByteBuf> handle) {
|
2013-06-11 21:18:40 +02:00
|
|
|
return new PooledUnsafeDirectByteBuf(handle, 0);
|
2013-06-10 12:52:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PooledUnsafeDirectByteBuf newInstance(int maxCapacity) {
|
2013-06-11 21:18:40 +02:00
|
|
|
PooledUnsafeDirectByteBuf buf = RECYCLER.get();
|
2015-09-23 11:25:20 +02:00
|
|
|
buf.reuse(maxCapacity);
|
2013-06-11 21:18:40 +02:00
|
|
|
return buf;
|
2013-06-10 12:52:56 +02:00
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
private long memoryAddress;
|
|
|
|
|
2013-12-18 15:05:37 +01:00
|
|
|
private PooledUnsafeDirectByteBuf(Recycler.Handle<PooledUnsafeDirectByteBuf> recyclerHandle, int maxCapacity) {
|
2013-06-10 12:52:56 +02:00
|
|
|
super(recyclerHandle, maxCapacity);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-05-19 15:03:29 +02:00
|
|
|
void init(PoolChunk<ByteBuffer> chunk, long handle, int offset, int length, int maxLength,
|
|
|
|
PoolThreadCache cache) {
|
|
|
|
super.init(chunk, handle, offset, length, maxLength, cache);
|
2013-01-10 10:27:16 +01:00
|
|
|
initMemoryAddress();
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
void initUnpooled(PoolChunk<ByteBuffer> chunk, int length) {
|
|
|
|
super.initUnpooled(chunk, length);
|
2013-01-10 10:27:16 +01:00
|
|
|
initMemoryAddress();
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
private void initMemoryAddress() {
|
|
|
|
memoryAddress = PlatformDependent.directBufferAddress(memory) + offset;
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ByteBuffer newInternalNioBuffer(ByteBuffer memory) {
|
2013-06-10 17:57:23 +02:00
|
|
|
return memory.duplicate();
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isDirect() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected byte _getByte(int index) {
|
2013-01-10 10:27:16 +01:00
|
|
|
return PlatformDependent.getByte(addr(index));
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected short _getShort(int index) {
|
2013-01-10 10:27:16 +01:00
|
|
|
short v = PlatformDependent.getShort(addr(index));
|
|
|
|
return NATIVE_ORDER? v : Short.reverseBytes(v);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected int _getUnsignedMedium(int index) {
|
2013-01-10 10:27:16 +01:00
|
|
|
long addr = addr(index);
|
2013-01-10 10:27:16 +01:00
|
|
|
return (PlatformDependent.getByte(addr) & 0xff) << 16 |
|
|
|
|
(PlatformDependent.getByte(addr + 1) & 0xff) << 8 |
|
|
|
|
PlatformDependent.getByte(addr + 2) & 0xff;
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected int _getInt(int index) {
|
2013-01-10 10:27:16 +01:00
|
|
|
int v = PlatformDependent.getInt(addr(index));
|
|
|
|
return NATIVE_ORDER? v : Integer.reverseBytes(v);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected long _getLong(int index) {
|
2013-01-10 10:27:16 +01:00
|
|
|
long v = PlatformDependent.getLong(addr(index));
|
|
|
|
return NATIVE_ORDER? v : Long.reverseBytes(v);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
|
|
|
checkIndex(index, length);
|
2013-03-06 10:22:16 +01:00
|
|
|
if (dst == null) {
|
|
|
|
throw new NullPointerException("dst");
|
|
|
|
}
|
|
|
|
if (dstIndex < 0 || dstIndex > dst.capacity() - length) {
|
|
|
|
throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
|
|
|
|
}
|
|
|
|
|
2013-02-08 18:09:37 +01:00
|
|
|
if (length != 0) {
|
2013-03-06 10:22:16 +01:00
|
|
|
if (dst.hasMemoryAddress()) {
|
|
|
|
PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length);
|
2013-02-08 18:09:37 +01:00
|
|
|
} else if (dst.hasArray()) {
|
|
|
|
PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
|
|
|
|
} else {
|
|
|
|
dst.setBytes(dstIndex, this, index, length);
|
|
|
|
}
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
|
|
|
checkIndex(index, length);
|
2013-03-06 10:22:16 +01:00
|
|
|
if (dst == null) {
|
|
|
|
throw new NullPointerException("dst");
|
|
|
|
}
|
|
|
|
if (dstIndex < 0 || dstIndex > dst.length - length) {
|
|
|
|
throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
|
|
|
|
}
|
2013-02-08 18:09:37 +01:00
|
|
|
if (length != 0) {
|
|
|
|
PlatformDependent.copyMemory(addr(index), dst, dstIndex, length);
|
|
|
|
}
|
2013-01-10 10:27:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
2013-09-20 16:24:46 +02:00
|
|
|
getBytes(index, dst, false);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getBytes(int index, ByteBuffer dst, boolean internal) {
|
2013-01-10 10:27:16 +01:00
|
|
|
checkIndex(index);
|
|
|
|
int bytesToCopy = Math.min(capacity() - index, dst.remaining());
|
2013-09-20 16:24:46 +02:00
|
|
|
ByteBuffer tmpBuf;
|
|
|
|
if (internal) {
|
|
|
|
tmpBuf = internalNioBuffer();
|
|
|
|
} else {
|
|
|
|
tmpBuf = memory.duplicate();
|
|
|
|
}
|
2013-01-10 10:27:16 +01:00
|
|
|
index = idx(index);
|
|
|
|
tmpBuf.clear().position(index).limit(index + bytesToCopy);
|
|
|
|
dst.put(tmpBuf);
|
2013-09-20 16:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf readBytes(ByteBuffer dst) {
|
|
|
|
int length = dst.remaining();
|
|
|
|
checkReadableBytes(length);
|
|
|
|
getBytes(readerIndex, dst, true);
|
|
|
|
readerIndex += length;
|
2013-01-10 10:27:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
|
|
|
|
checkIndex(index, length);
|
2013-02-08 18:09:37 +01:00
|
|
|
if (length != 0) {
|
|
|
|
byte[] tmp = new byte[length];
|
|
|
|
PlatformDependent.copyMemory(addr(index), tmp, 0, length);
|
|
|
|
out.write(tmp);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
|
2013-09-20 16:24:46 +02:00
|
|
|
return getBytes(index, out, length, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
|
2013-01-10 10:27:16 +01:00
|
|
|
checkIndex(index, length);
|
|
|
|
if (length == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-20 16:24:46 +02:00
|
|
|
ByteBuffer tmpBuf;
|
|
|
|
if (internal) {
|
|
|
|
tmpBuf = internalNioBuffer();
|
|
|
|
} else {
|
|
|
|
tmpBuf = memory.duplicate();
|
|
|
|
}
|
2013-01-10 10:27:16 +01:00
|
|
|
index = idx(index);
|
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
|
|
|
return out.write(tmpBuf);
|
|
|
|
}
|
|
|
|
|
2013-09-20 16:24:46 +02:00
|
|
|
@Override
|
|
|
|
public int readBytes(GatheringByteChannel out, int length)
|
|
|
|
throws IOException {
|
|
|
|
checkReadableBytes(length);
|
|
|
|
int readBytes = getBytes(readerIndex, out, length, true);
|
|
|
|
readerIndex += readBytes;
|
|
|
|
return readBytes;
|
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected void _setByte(int index, int value) {
|
2013-01-10 10:27:16 +01:00
|
|
|
PlatformDependent.putByte(addr(index), (byte) value);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected void _setShort(int index, int value) {
|
|
|
|
PlatformDependent.putShort(addr(index), NATIVE_ORDER ? (short) value : Short.reverseBytes((short) value));
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2013-03-06 02:32:29 +01:00
|
|
|
@Override
|
|
|
|
protected void _setMedium(int index, int value) {
|
2013-01-10 10:27:16 +01:00
|
|
|
long addr = addr(index);
|
2013-01-10 10:27:16 +01:00
|
|
|
PlatformDependent.putByte(addr, (byte) (value >>> 16));
|
|
|
|
PlatformDependent.putByte(addr + 1, (byte) (value >>> 8));
|
|
|
|
PlatformDependent.putByte(addr + 2, (byte) value);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected void _setInt(int index, int value) {
|
|
|
|
PlatformDependent.putInt(addr(index), NATIVE_ORDER ? value : Integer.reverseBytes(value));
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected void _setLong(int index, long value) {
|
|
|
|
PlatformDependent.putLong(addr(index), NATIVE_ORDER ? value : Long.reverseBytes(value));
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
|
|
|
|
checkIndex(index, length);
|
2013-03-06 10:22:16 +01:00
|
|
|
if (src == null) {
|
|
|
|
throw new NullPointerException("src");
|
|
|
|
}
|
|
|
|
if (srcIndex < 0 || srcIndex > src.capacity() - length) {
|
|
|
|
throw new IndexOutOfBoundsException("srcIndex: " + srcIndex);
|
|
|
|
}
|
|
|
|
|
2013-02-08 18:09:37 +01:00
|
|
|
if (length != 0) {
|
2013-03-06 10:22:16 +01:00
|
|
|
if (src.hasMemoryAddress()) {
|
|
|
|
PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, addr(index), length);
|
2013-02-08 18:09:37 +01:00
|
|
|
} else if (src.hasArray()) {
|
|
|
|
PlatformDependent.copyMemory(src.array(), src.arrayOffset() + srcIndex, addr(index), length);
|
|
|
|
} else {
|
|
|
|
src.getBytes(srcIndex, this, index, length);
|
|
|
|
}
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
|
|
|
|
checkIndex(index, length);
|
2013-02-08 18:09:37 +01:00
|
|
|
if (length != 0) {
|
|
|
|
PlatformDependent.copyMemory(src, srcIndex, addr(index), length);
|
|
|
|
}
|
2013-01-10 10:27:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf setBytes(int index, ByteBuffer src) {
|
2014-05-09 22:32:43 +02:00
|
|
|
checkIndex(index, src.remaining());
|
2013-01-10 10:27:16 +01:00
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
|
|
|
if (src == tmpBuf) {
|
|
|
|
src = src.duplicate();
|
|
|
|
}
|
|
|
|
|
|
|
|
index = idx(index);
|
|
|
|
tmpBuf.clear().position(index).limit(index + src.remaining());
|
|
|
|
tmpBuf.put(src);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2013-02-08 18:09:37 +01:00
|
|
|
if (readBytes > 0) {
|
|
|
|
PlatformDependent.copyMemory(tmp, 0, addr(index), readBytes);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
return readBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
|
|
|
|
checkIndex(index, length);
|
2013-09-20 16:24:46 +02:00
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
2013-01-10 10:27:16 +01:00
|
|
|
index = idx(index);
|
2013-09-20 16:24:46 +02:00
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
2013-01-10 10:27:16 +01:00
|
|
|
try {
|
2013-09-20 16:24:46 +02:00
|
|
|
return in.read(tmpBuf);
|
2014-07-02 12:04:11 +02:00
|
|
|
} catch (ClosedChannelException ignored) {
|
2013-01-10 10:27:16 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf copy(int index, int length) {
|
|
|
|
checkIndex(index, length);
|
2013-12-04 11:03:32 +01:00
|
|
|
ByteBuf copy = alloc().directBuffer(length, maxCapacity());
|
2013-02-08 18:09:37 +01:00
|
|
|
if (length != 0) {
|
2013-12-04 11:03:32 +01:00
|
|
|
if (copy.hasMemoryAddress()) {
|
|
|
|
PlatformDependent.copyMemory(addr(index), copy.memoryAddress(), length);
|
|
|
|
copy.setIndex(0, length);
|
|
|
|
} else {
|
|
|
|
copy.writeBytes(this, index, length);
|
|
|
|
}
|
2013-02-08 18:09:37 +01:00
|
|
|
}
|
2013-01-10 10:27:16 +01:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int nioBufferCount() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-06-13 05:40:26 +02:00
|
|
|
public ByteBuffer[] nioBuffers(int index, int length) {
|
|
|
|
return new ByteBuffer[] { nioBuffer(index, length) };
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2013-08-30 09:51:12 +02:00
|
|
|
@Override
|
|
|
|
public ByteBuffer nioBuffer(int index, int length) {
|
|
|
|
checkIndex(index, length);
|
|
|
|
index = idx(index);
|
2013-10-16 10:34:33 +02:00
|
|
|
return ((ByteBuffer) memory.duplicate().position(index).limit(index + length)).slice();
|
2013-08-30 09:51:12 +02:00
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
@Override
|
2013-06-13 05:40:26 +02:00
|
|
|
public ByteBuffer internalNioBuffer(int index, int length) {
|
|
|
|
checkIndex(index, length);
|
|
|
|
index = idx(index);
|
|
|
|
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasArray() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public byte[] array() {
|
|
|
|
throw new UnsupportedOperationException("direct buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int arrayOffset() {
|
|
|
|
throw new UnsupportedOperationException("direct buffer");
|
|
|
|
}
|
|
|
|
|
2013-03-06 10:22:16 +01:00
|
|
|
@Override
|
|
|
|
public boolean hasMemoryAddress() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long memoryAddress() {
|
2014-11-05 14:38:56 +01:00
|
|
|
ensureAccessible();
|
2013-03-06 10:22:16 +01:00
|
|
|
return memoryAddress;
|
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
private long addr(int index) {
|
|
|
|
return memoryAddress + index;
|
|
|
|
}
|
2014-06-04 07:04:19 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected SwappedByteBuf newSwappedByteBuf() {
|
2014-06-20 18:19:45 +02:00
|
|
|
return new UnsafeDirectSwappedByteBuf(this);
|
2014-06-04 07:04:19 +02:00
|
|
|
}
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|