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.channels.ClosedChannelException;
|
2016-02-05 04:02:40 +01:00
|
|
|
import java.nio.channels.FileChannel;
|
2013-01-10 10:27:16 +01:00
|
|
|
import java.nio.channels.GatheringByteChannel;
|
|
|
|
import java.nio.channels.ScatteringByteChannel;
|
|
|
|
|
|
|
|
final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
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) {
|
2015-10-15 21:41:49 +02:00
|
|
|
return UnsafeByteBufUtil.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) {
|
2015-10-15 21:41:49 +02:00
|
|
|
return UnsafeByteBufUtil.getShort(addr(index));
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected short _getShortLE(int index) {
|
|
|
|
return UnsafeByteBufUtil.getShortLE(addr(index));
|
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected int _getUnsignedMedium(int index) {
|
2015-10-15 21:41:49 +02:00
|
|
|
return UnsafeByteBufUtil.getUnsignedMedium(addr(index));
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected int _getUnsignedMediumLE(int index) {
|
|
|
|
return UnsafeByteBufUtil.getUnsignedMediumLE(addr(index));
|
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected int _getInt(int index) {
|
2015-10-15 21:41:49 +02:00
|
|
|
return UnsafeByteBufUtil.getInt(addr(index));
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected int _getIntLE(int index) {
|
|
|
|
return UnsafeByteBufUtil.getIntLE(addr(index));
|
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected long _getLong(int index) {
|
2015-10-15 21:41:49 +02:00
|
|
|
return UnsafeByteBufUtil.getLong(addr(index));
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected long _getLongLE(int index) {
|
|
|
|
return UnsafeByteBufUtil.getLongLE(addr(index));
|
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
@Override
|
|
|
|
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
2015-10-21 10:04:49 +02:00
|
|
|
UnsafeByteBufUtil.getBytes(this, addr(index), index, dst, dstIndex, length);
|
2013-01-10 10:27:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
2015-10-21 10:04:49 +02:00
|
|
|
UnsafeByteBufUtil.getBytes(this, addr(index), index, dst, dstIndex, length);
|
2013-01-10 10:27:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
2015-10-21 10:04:49 +02:00
|
|
|
UnsafeByteBufUtil.getBytes(this, addr(index), index, dst);
|
2015-10-19 06:59:26 +02:00
|
|
|
return this;
|
2013-09-20 16:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf readBytes(ByteBuffer dst) {
|
|
|
|
int length = dst.remaining();
|
|
|
|
checkReadableBytes(length);
|
2015-10-19 06:59:26 +02:00
|
|
|
getBytes(readerIndex, dst);
|
2013-09-20 16:24:46 +02:00
|
|
|
readerIndex += length;
|
2013-01-10 10:27:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
|
2015-10-21 10:04:49 +02:00
|
|
|
UnsafeByteBufUtil.getBytes(this, addr(index), index, out, length);
|
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);
|
|
|
|
}
|
|
|
|
|
2016-02-05 04:02:40 +01:00
|
|
|
@Override
|
|
|
|
public int getBytes(int index, FileChannel out, long position, int length) throws IOException {
|
|
|
|
return getBytes(index, out, position, length, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getBytes(int index, FileChannel out, long position, int length, boolean internal) throws IOException {
|
|
|
|
checkIndex(index, length);
|
|
|
|
if (length == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer tmpBuf = internal ? internalNioBuffer() : memory.duplicate();
|
|
|
|
index = idx(index);
|
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
|
|
|
return out.write(tmpBuf, position);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-02-05 04:02:40 +01:00
|
|
|
@Override
|
|
|
|
public int readBytes(FileChannel out, long position, int length)
|
|
|
|
throws IOException {
|
|
|
|
checkReadableBytes(length);
|
|
|
|
int readBytes = getBytes(readerIndex, out, position, 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) {
|
2015-10-15 21:41:49 +02:00
|
|
|
UnsafeByteBufUtil.setByte(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) {
|
2015-10-15 21:41:49 +02:00
|
|
|
UnsafeByteBufUtil.setShort(addr(index), value);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected void _setShortLE(int index, int value) {
|
|
|
|
UnsafeByteBufUtil.setShortLE(addr(index), value);
|
|
|
|
}
|
|
|
|
|
2013-03-06 02:32:29 +01:00
|
|
|
@Override
|
|
|
|
protected void _setMedium(int index, int value) {
|
2015-10-15 21:41:49 +02:00
|
|
|
UnsafeByteBufUtil.setMedium(addr(index), value);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected void _setMediumLE(int index, int value) {
|
|
|
|
UnsafeByteBufUtil.setMediumLE(addr(index), 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) {
|
2015-10-15 21:41:49 +02:00
|
|
|
UnsafeByteBufUtil.setInt(addr(index), value);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected void _setIntLE(int index, int value) {
|
|
|
|
UnsafeByteBufUtil.setIntLE(addr(index), 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) {
|
2015-10-15 21:41:49 +02:00
|
|
|
UnsafeByteBufUtil.setLong(addr(index), value);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected void _setLongLE(int index, long value) {
|
|
|
|
UnsafeByteBufUtil.setLongLE(addr(index), value);
|
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
@Override
|
|
|
|
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
|
2015-10-21 10:04:49 +02:00
|
|
|
UnsafeByteBufUtil.setBytes(this, addr(index), index, src, srcIndex, length);
|
2013-01-10 10:27:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
|
2015-10-21 10:04:49 +02:00
|
|
|
UnsafeByteBufUtil.setBytes(this, addr(index), index, src, srcIndex, length);
|
2013-01-10 10:27:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf setBytes(int index, ByteBuffer src) {
|
2015-10-21 10:04:49 +02:00
|
|
|
UnsafeByteBufUtil.setBytes(this, addr(index), index, src);
|
2013-01-10 10:27:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int setBytes(int index, InputStream in, int length) throws IOException {
|
2015-10-21 10:04:49 +02:00
|
|
|
return UnsafeByteBufUtil.setBytes(this, addr(index), index, in, length);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 04:02:40 +01:00
|
|
|
@Override
|
|
|
|
public int setBytes(int index, FileChannel in, long position, int length) throws IOException {
|
|
|
|
checkIndex(index, length);
|
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
|
|
|
index = idx(index);
|
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
|
|
|
try {
|
|
|
|
return in.read(tmpBuf, position);
|
|
|
|
} catch (ClosedChannelException ignored) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-10 10:27:16 +01:00
|
|
|
@Override
|
|
|
|
public ByteBuf copy(int index, int length) {
|
2015-10-21 10:04:49 +02:00
|
|
|
return UnsafeByteBufUtil.copy(this, addr(index), index, length);
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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() {
|
2015-10-15 21:41:49 +02:00
|
|
|
if (PlatformDependent.isUnaligned()) {
|
|
|
|
// Only use if unaligned access is supported otherwise there is no gain.
|
|
|
|
return new UnsafeDirectSwappedByteBuf(this);
|
|
|
|
}
|
|
|
|
return super.newSwappedByteBuf();
|
2014-06-04 07:04:19 +02:00
|
|
|
}
|
2016-05-23 14:53:31 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf setZero(int index, int length) {
|
2017-07-17 15:44:46 +02:00
|
|
|
checkIndex(index, length);
|
|
|
|
UnsafeByteBufUtil.setZero(addr(index), length);
|
2016-05-23 14:53:31 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf writeZero(int length) {
|
|
|
|
ensureWritable(length);
|
|
|
|
int wIndex = writerIndex;
|
2017-07-17 15:44:46 +02:00
|
|
|
UnsafeByteBufUtil.setZero(addr(wIndex), length);
|
2016-05-23 14:53:31 +02:00
|
|
|
writerIndex = wIndex + length;
|
|
|
|
return this;
|
|
|
|
}
|
2013-01-10 10:27:16 +01:00
|
|
|
}
|