2013-03-06 08:01:46 +01:00
|
|
|
/*
|
|
|
|
* 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 io.netty.buffer;
|
|
|
|
|
|
|
|
import io.netty.util.internal.PlatformDependent;
|
|
|
|
|
|
|
|
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;
|
2016-02-05 04:02:40 +01:00
|
|
|
import java.nio.channels.FileChannel;
|
2013-03-06 08:01:46 +01:00
|
|
|
import java.nio.channels.GatheringByteChannel;
|
|
|
|
import java.nio.channels.ScatteringByteChannel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A NIO {@link ByteBuffer} based buffer. It is recommended to use {@link Unpooled#directBuffer(int)}
|
|
|
|
* and {@link Unpooled#wrappedBuffer(ByteBuffer)} instead of calling the
|
|
|
|
* constructor explicitly.
|
|
|
|
*/
|
2013-04-04 10:26:14 +02:00
|
|
|
public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf {
|
2013-03-06 08:01:46 +01:00
|
|
|
|
|
|
|
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 initialCapacity the initial capacity of the underlying direct buffer
|
|
|
|
* @param maxCapacity the maximum capacity of the underlying direct buffer
|
|
|
|
*/
|
2013-04-04 10:26:14 +02:00
|
|
|
protected UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
|
2013-03-06 08:01:46 +01:00
|
|
|
super(maxCapacity);
|
|
|
|
if (alloc == null) {
|
|
|
|
throw new NullPointerException("alloc");
|
|
|
|
}
|
|
|
|
if (initialCapacity < 0) {
|
|
|
|
throw new IllegalArgumentException("initialCapacity: " + initialCapacity);
|
|
|
|
}
|
|
|
|
if (maxCapacity < 0) {
|
|
|
|
throw new IllegalArgumentException("maxCapacity: " + maxCapacity);
|
|
|
|
}
|
|
|
|
if (initialCapacity > maxCapacity) {
|
|
|
|
throw new IllegalArgumentException(String.format(
|
|
|
|
"initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.alloc = alloc;
|
2013-11-12 12:13:38 +01:00
|
|
|
setByteBuffer(allocateDirect(initialCapacity));
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new direct buffer by wrapping the specified initial buffer.
|
|
|
|
*
|
|
|
|
* @param maxCapacity the maximum capacity of the underlying direct buffer
|
|
|
|
*/
|
2013-04-04 10:26:14 +02:00
|
|
|
protected UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity) {
|
2013-03-06 08:01:46 +01:00
|
|
|
super(maxCapacity);
|
|
|
|
if (alloc == null) {
|
|
|
|
throw new NullPointerException("alloc");
|
|
|
|
}
|
|
|
|
if (initialBuffer == null) {
|
|
|
|
throw new NullPointerException("initialBuffer");
|
|
|
|
}
|
|
|
|
if (!initialBuffer.isDirect()) {
|
|
|
|
throw new IllegalArgumentException("initialBuffer is not a direct buffer.");
|
|
|
|
}
|
|
|
|
if (initialBuffer.isReadOnly()) {
|
|
|
|
throw new IllegalArgumentException("initialBuffer is a read-only buffer.");
|
|
|
|
}
|
|
|
|
|
|
|
|
int initialCapacity = initialBuffer.remaining();
|
|
|
|
if (initialCapacity > maxCapacity) {
|
|
|
|
throw new IllegalArgumentException(String.format(
|
|
|
|
"initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.alloc = alloc;
|
|
|
|
doNotFree = true;
|
|
|
|
setByteBuffer(initialBuffer.slice().order(ByteOrder.BIG_ENDIAN));
|
|
|
|
writerIndex(initialCapacity);
|
|
|
|
}
|
|
|
|
|
2013-11-12 12:13:38 +01:00
|
|
|
/**
|
|
|
|
* Allocate a new direct {@link ByteBuffer} with the given initialCapacity.
|
|
|
|
*/
|
|
|
|
protected ByteBuffer allocateDirect(int initialCapacity) {
|
|
|
|
return ByteBuffer.allocateDirect(initialCapacity);
|
|
|
|
}
|
|
|
|
|
2013-11-12 12:40:41 +01:00
|
|
|
/**
|
|
|
|
* Free a direct {@link ByteBuffer}
|
|
|
|
*/
|
|
|
|
protected void freeDirect(ByteBuffer buffer) {
|
|
|
|
PlatformDependent.freeDirectBuffer(buffer);
|
|
|
|
}
|
|
|
|
|
2013-03-06 08:01:46 +01:00
|
|
|
private void setByteBuffer(ByteBuffer buffer) {
|
|
|
|
ByteBuffer oldBuffer = this.buffer;
|
|
|
|
if (oldBuffer != null) {
|
|
|
|
if (doNotFree) {
|
|
|
|
doNotFree = false;
|
|
|
|
} else {
|
2013-11-12 12:40:41 +01:00
|
|
|
freeDirect(oldBuffer);
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.buffer = buffer;
|
|
|
|
memoryAddress = PlatformDependent.directBufferAddress(buffer);
|
|
|
|
tmpNioBuf = null;
|
|
|
|
capacity = buffer.remaining();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isDirect() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int capacity() {
|
|
|
|
return capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf capacity(int newCapacity) {
|
|
|
|
ensureAccessible();
|
|
|
|
if (newCapacity < 0 || newCapacity > maxCapacity()) {
|
|
|
|
throw new IllegalArgumentException("newCapacity: " + newCapacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
int readerIndex = readerIndex();
|
|
|
|
int writerIndex = writerIndex();
|
|
|
|
|
|
|
|
int oldCapacity = capacity;
|
|
|
|
if (newCapacity > oldCapacity) {
|
|
|
|
ByteBuffer oldBuffer = buffer;
|
2013-11-12 12:13:38 +01:00
|
|
|
ByteBuffer newBuffer = allocateDirect(newCapacity);
|
2013-11-04 15:18:21 +01:00
|
|
|
oldBuffer.position(0).limit(oldBuffer.capacity());
|
|
|
|
newBuffer.position(0).limit(oldBuffer.capacity());
|
2013-03-06 08:01:46 +01:00
|
|
|
newBuffer.put(oldBuffer);
|
|
|
|
newBuffer.clear();
|
|
|
|
setByteBuffer(newBuffer);
|
|
|
|
} else if (newCapacity < oldCapacity) {
|
|
|
|
ByteBuffer oldBuffer = buffer;
|
2013-11-12 12:13:38 +01:00
|
|
|
ByteBuffer newBuffer = allocateDirect(newCapacity);
|
2013-03-06 08:01:46 +01:00
|
|
|
if (readerIndex < newCapacity) {
|
|
|
|
if (writerIndex > newCapacity) {
|
|
|
|
writerIndex(writerIndex = newCapacity);
|
|
|
|
}
|
|
|
|
oldBuffer.position(readerIndex).limit(writerIndex);
|
|
|
|
newBuffer.position(readerIndex).limit(writerIndex);
|
|
|
|
newBuffer.put(oldBuffer);
|
|
|
|
newBuffer.clear();
|
|
|
|
} else {
|
|
|
|
setIndex(newCapacity, newCapacity);
|
|
|
|
}
|
|
|
|
setByteBuffer(newBuffer);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBufAllocator alloc() {
|
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteOrder order() {
|
|
|
|
return ByteOrder.BIG_ENDIAN;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-03-06 08:01:46 +01:00
|
|
|
@Override
|
|
|
|
protected byte _getByte(int index) {
|
2015-10-15 21:41:49 +02:00
|
|
|
return UnsafeByteBufUtil.getByte(addr(index));
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected short _getShort(int index) {
|
2015-10-15 21:41:49 +02:00
|
|
|
return UnsafeByteBufUtil.getShort(addr(index));
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected short _getShortLE(int index) {
|
|
|
|
return UnsafeByteBufUtil.getShortLE(addr(index));
|
|
|
|
}
|
|
|
|
|
2013-03-06 08:01:46 +01:00
|
|
|
@Override
|
|
|
|
protected int _getUnsignedMedium(int index) {
|
2015-10-15 21:41:49 +02:00
|
|
|
return UnsafeByteBufUtil.getUnsignedMedium(addr(index));
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected int _getUnsignedMediumLE(int index) {
|
|
|
|
return UnsafeByteBufUtil.getUnsignedMediumLE(addr(index));
|
|
|
|
}
|
|
|
|
|
2013-03-06 08:01:46 +01:00
|
|
|
@Override
|
|
|
|
protected int _getInt(int index) {
|
2015-10-15 21:41:49 +02:00
|
|
|
return UnsafeByteBufUtil.getInt(addr(index));
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected int _getIntLE(int index) {
|
|
|
|
return UnsafeByteBufUtil.getIntLE(addr(index));
|
|
|
|
}
|
|
|
|
|
2013-03-06 08:01:46 +01:00
|
|
|
@Override
|
|
|
|
protected long _getLong(int index) {
|
2015-10-15 21:41:49 +02:00
|
|
|
return UnsafeByteBufUtil.getLong(addr(index));
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected long _getLongLE(int index) {
|
|
|
|
return UnsafeByteBufUtil.getLongLE(addr(index));
|
|
|
|
}
|
|
|
|
|
2013-03-06 08:01:46 +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-03-06 08:01:46 +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-03-06 08:01:46 +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-03-06 08:01:46 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void _setByte(int index, int value) {
|
2015-10-15 21:41:49 +02:00
|
|
|
UnsafeByteBufUtil.setByte(addr(index), value);
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void _setShort(int index, int value) {
|
2015-10-15 21:41:49 +02:00
|
|
|
UnsafeByteBufUtil.setShort(addr(index), value);
|
2013-03-06 08:01:46 +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 08:01:46 +01:00
|
|
|
@Override
|
|
|
|
protected void _setMedium(int index, int value) {
|
2015-10-15 21:41:49 +02:00
|
|
|
UnsafeByteBufUtil.setMedium(addr(index), value);
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected void _setMediumLE(int index, int value) {
|
|
|
|
UnsafeByteBufUtil.setMediumLE(addr(index), value);
|
|
|
|
}
|
|
|
|
|
2013-03-06 08:01:46 +01:00
|
|
|
@Override
|
|
|
|
protected void _setInt(int index, int value) {
|
2015-10-15 21:41:49 +02:00
|
|
|
UnsafeByteBufUtil.setInt(addr(index), value);
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected void _setIntLE(int index, int value) {
|
|
|
|
UnsafeByteBufUtil.setIntLE(addr(index), value);
|
|
|
|
}
|
|
|
|
|
2013-03-06 08:01:46 +01:00
|
|
|
@Override
|
|
|
|
protected void _setLong(int index, long value) {
|
2015-10-15 21:41:49 +02:00
|
|
|
UnsafeByteBufUtil.setLong(addr(index), value);
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 22:20:16 +01:00
|
|
|
@Override
|
|
|
|
protected void _setLongLE(int index, long value) {
|
|
|
|
UnsafeByteBufUtil.setLongLE(addr(index), value);
|
|
|
|
}
|
|
|
|
|
2013-03-06 08:01:46 +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-03-06 08:01:46 +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-03-06 08:01:46 +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-03-06 08:01:46 +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-03-06 08:01:46 +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-03-06 08:01:46 +01:00
|
|
|
ensureAccessible();
|
|
|
|
if (length == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-20 16:24:46 +02:00
|
|
|
ByteBuffer tmpBuf;
|
|
|
|
if (internal) {
|
|
|
|
tmpBuf = internalNioBuffer();
|
|
|
|
} else {
|
|
|
|
tmpBuf = buffer.duplicate();
|
|
|
|
}
|
2013-03-06 08:01:46 +01:00
|
|
|
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 {
|
|
|
|
ensureAccessible();
|
|
|
|
if (length == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer tmpBuf = internal ? internalNioBuffer() : buffer.duplicate();
|
|
|
|
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-03-06 08:01:46 +01:00
|
|
|
@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-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
|
|
|
|
ensureAccessible();
|
2013-09-20 16:24:46 +02:00
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
2013-03-06 08:01:46 +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-03-06 08:01:46 +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 {
|
|
|
|
ensureAccessible();
|
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
|
|
|
try {
|
|
|
|
return in.read(tmpBuf, position);
|
|
|
|
} catch (ClosedChannelException ignored) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 08:01:46 +01:00
|
|
|
@Override
|
|
|
|
public int nioBufferCount() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuffer[] nioBuffers(int index, int length) {
|
|
|
|
return new ByteBuffer[] { nioBuffer(index, length) };
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
|
2013-06-13 05:40:26 +02:00
|
|
|
@Override
|
|
|
|
public ByteBuffer internalNioBuffer(int index, int length) {
|
2014-06-30 07:10:12 +02:00
|
|
|
checkIndex(index, length);
|
2013-06-13 05:40:26 +02:00
|
|
|
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
|
|
|
|
}
|
|
|
|
|
2013-03-06 08:01:46 +01:00
|
|
|
private ByteBuffer internalNioBuffer() {
|
|
|
|
ByteBuffer tmpNioBuf = this.tmpNioBuf;
|
|
|
|
if (tmpNioBuf == null) {
|
|
|
|
this.tmpNioBuf = tmpNioBuf = buffer.duplicate();
|
|
|
|
}
|
|
|
|
return tmpNioBuf;
|
|
|
|
}
|
|
|
|
|
2013-08-30 09:51:12 +02:00
|
|
|
@Override
|
|
|
|
public ByteBuffer nioBuffer(int index, int length) {
|
2014-06-30 07:10:12 +02:00
|
|
|
checkIndex(index, length);
|
2013-10-16 10:34:33 +02:00
|
|
|
return ((ByteBuffer) buffer.duplicate().position(index).limit(index + length)).slice();
|
2013-08-30 09:51:12 +02:00
|
|
|
}
|
|
|
|
|
2013-03-06 08:01:46 +01:00
|
|
|
@Override
|
|
|
|
protected void deallocate() {
|
|
|
|
ByteBuffer buffer = this.buffer;
|
|
|
|
if (buffer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.buffer = null;
|
|
|
|
|
|
|
|
if (!doNotFree) {
|
2013-11-12 12:40:41 +01:00
|
|
|
freeDirect(buffer);
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf unwrap() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-03-21 15:49:14 +01:00
|
|
|
long addr(int index) {
|
2013-03-06 08:01:46 +01:00
|
|
|
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) {
|
|
|
|
UnsafeByteBufUtil.setZero(this, addr(index), index, length);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf writeZero(int length) {
|
|
|
|
ensureWritable(length);
|
|
|
|
int wIndex = writerIndex;
|
|
|
|
setZero(wIndex, length);
|
|
|
|
writerIndex = wIndex + length;
|
|
|
|
return this;
|
|
|
|
}
|
2013-03-06 08:01:46 +01:00
|
|
|
}
|