2012-07-19 13:23:55 +02: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;
|
|
|
|
|
2012-12-05 11:28:56 +01:00
|
|
|
import io.netty.buffer.ByteBuf.Unsafe;
|
2012-11-09 09:26:11 +01:00
|
|
|
import sun.misc.Cleaner;
|
|
|
|
|
2012-07-19 13:23:55 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
import java.nio.channels.ClosedChannelException;
|
|
|
|
import java.nio.channels.GatheringByteChannel;
|
|
|
|
import java.nio.channels.ScatteringByteChannel;
|
2012-12-02 13:50:33 +01:00
|
|
|
import java.util.ArrayDeque;
|
|
|
|
import java.util.Queue;
|
2012-07-19 13:23:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("restriction")
|
2012-12-05 11:28:56 +01:00
|
|
|
final class UnpooledDirectByteBuf extends AbstractByteBuf implements Unsafe {
|
2012-07-19 13:23:55 +02:00
|
|
|
|
|
|
|
private static final Field CLEANER_FIELD;
|
|
|
|
|
|
|
|
static {
|
|
|
|
ByteBuffer direct = ByteBuffer.allocateDirect(1);
|
|
|
|
Field cleanerField;
|
|
|
|
try {
|
|
|
|
cleanerField = direct.getClass().getDeclaredField("cleaner");
|
|
|
|
cleanerField.setAccessible(true);
|
|
|
|
Cleaner cleaner = (Cleaner) cleanerField.get(direct);
|
|
|
|
cleaner.clean();
|
|
|
|
} catch (Throwable t) {
|
|
|
|
cleanerField = null;
|
|
|
|
}
|
|
|
|
CLEANER_FIELD = cleanerField;
|
|
|
|
}
|
|
|
|
|
2012-12-05 22:11:48 +01:00
|
|
|
static void freeDirect(ByteBuffer buffer) {
|
2012-11-15 22:04:37 +01:00
|
|
|
if (CLEANER_FIELD == null) {
|
|
|
|
// Doomed to wait for GC.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-19 13:23:55 +02:00
|
|
|
Cleaner cleaner;
|
|
|
|
try {
|
|
|
|
cleaner = (Cleaner) CLEANER_FIELD.get(buffer);
|
|
|
|
cleaner.clean();
|
|
|
|
} catch (Throwable t) {
|
|
|
|
// Nothing we can do here.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
private final ByteBufAllocator alloc;
|
2012-07-19 13:23:55 +02:00
|
|
|
private ByteBuffer buffer;
|
2012-11-15 22:04:37 +01:00
|
|
|
private ByteBuffer tmpNioBuf;
|
2012-07-19 13:23:55 +02:00
|
|
|
private int capacity;
|
2012-11-15 22:04:37 +01:00
|
|
|
private boolean freed;
|
|
|
|
private boolean doNotFree;
|
2012-12-02 13:50:33 +01:00
|
|
|
private Queue<ByteBuffer> suspendedDeallocations;
|
2012-07-19 13:23:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-11-15 22:04:37 +01:00
|
|
|
public UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
|
|
|
|
super(maxCapacity);
|
|
|
|
if (alloc == null) {
|
|
|
|
throw new NullPointerException("alloc");
|
|
|
|
}
|
2012-07-19 13:23:55 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
this.alloc = alloc;
|
2012-07-19 13:23:55 +02:00
|
|
|
setByteBuffer(ByteBuffer.allocateDirect(initialCapacity));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new direct buffer by wrapping the specified initial buffer.
|
|
|
|
*
|
|
|
|
* @param maxCapacity the maximum capacity of the underlying direct buffer
|
|
|
|
*/
|
2012-11-15 22:04:37 +01:00
|
|
|
public UnpooledDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity) {
|
|
|
|
super(maxCapacity);
|
|
|
|
if (alloc == null) {
|
|
|
|
throw new NullPointerException("alloc");
|
|
|
|
}
|
2012-07-19 13:23:55 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
this.alloc = alloc;
|
2012-07-19 13:23:55 +02:00
|
|
|
doNotFree = true;
|
|
|
|
setByteBuffer(initialBuffer.slice().order(ByteOrder.BIG_ENDIAN));
|
|
|
|
writerIndex(initialCapacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setByteBuffer(ByteBuffer buffer) {
|
|
|
|
ByteBuffer oldBuffer = this.buffer;
|
|
|
|
if (oldBuffer != null) {
|
|
|
|
if (doNotFree) {
|
|
|
|
doNotFree = false;
|
|
|
|
} else {
|
2012-12-02 13:50:33 +01:00
|
|
|
if (suspendedDeallocations == null) {
|
|
|
|
freeDirect(oldBuffer);
|
|
|
|
} else {
|
|
|
|
suspendedDeallocations.add(oldBuffer);
|
|
|
|
}
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.buffer = buffer;
|
2012-11-15 22:04:37 +01:00
|
|
|
tmpNioBuf = null;
|
2012-07-19 13:23:55 +02:00
|
|
|
capacity = buffer.remaining();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isDirect() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int capacity() {
|
|
|
|
return capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf capacity(int newCapacity) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
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;
|
|
|
|
ByteBuffer newBuffer = ByteBuffer.allocateDirect(newCapacity);
|
|
|
|
oldBuffer.position(readerIndex).limit(writerIndex);
|
|
|
|
newBuffer.position(readerIndex).limit(writerIndex);
|
|
|
|
newBuffer.put(oldBuffer);
|
|
|
|
newBuffer.clear();
|
|
|
|
setByteBuffer(newBuffer);
|
|
|
|
} else if (newCapacity < oldCapacity) {
|
|
|
|
ByteBuffer oldBuffer = buffer;
|
|
|
|
ByteBuffer newBuffer = ByteBuffer.allocateDirect(newCapacity);
|
|
|
|
if (readerIndex < newCapacity) {
|
|
|
|
if (writerIndex > newCapacity) {
|
|
|
|
writerIndex(writerIndex = newCapacity);
|
|
|
|
}
|
|
|
|
oldBuffer.position(readerIndex).limit(writerIndex);
|
|
|
|
newBuffer.position(readerIndex).limit(writerIndex);
|
|
|
|
newBuffer.put(oldBuffer);
|
|
|
|
newBuffer.clear();
|
2012-07-20 06:18:17 +02:00
|
|
|
} else {
|
|
|
|
setIndex(newCapacity, newCapacity);
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
setByteBuffer(newBuffer);
|
|
|
|
}
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
@Override
|
|
|
|
public ByteBufAllocator alloc() {
|
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteOrder order() {
|
|
|
|
return ByteOrder.BIG_ENDIAN;
|
|
|
|
}
|
|
|
|
|
2012-07-19 13:23:55 +02: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");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public byte getByte(int index) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
return buffer.get(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public short getShort(int index) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
return buffer.getShort(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getUnsignedMedium(int index) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-11-09 09:26:11 +01:00
|
|
|
return (getByte(index) & 0xff) << 16 | (getByte(index + 1) & 0xff) << 8 | getByte(index + 2) & 0xff;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInt(int index) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
return buffer.getInt(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getLong(int index) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
return buffer.getLong(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
|
|
|
if (dst instanceof UnpooledDirectByteBuf) {
|
|
|
|
UnpooledDirectByteBuf bbdst = (UnpooledDirectByteBuf) dst;
|
|
|
|
ByteBuffer data = bbdst.internalNioBuffer();
|
2012-07-19 13:23:55 +02:00
|
|
|
data.clear().position(dstIndex).limit(dstIndex + length);
|
|
|
|
getBytes(index, data);
|
2012-12-05 22:11:48 +01:00
|
|
|
} else if (dst.hasArray()) {
|
|
|
|
getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
|
2012-07-19 13:23:55 +02:00
|
|
|
} else {
|
|
|
|
dst.setBytes(dstIndex, this, index, length);
|
|
|
|
}
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
2012-07-19 13:23:55 +02:00
|
|
|
try {
|
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
throw new IndexOutOfBoundsException("Too many bytes to read - Need " +
|
|
|
|
(index + length) + ", maximum is " + buffer.limit());
|
|
|
|
}
|
|
|
|
tmpBuf.get(dst, dstIndex, length);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
int bytesToCopy = Math.min(capacity() - index, dst.remaining());
|
2012-11-15 22:04:37 +01:00
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
2012-07-19 13:23:55 +02:00
|
|
|
try {
|
|
|
|
tmpBuf.clear().position(index).limit(index + bytesToCopy);
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
throw new IndexOutOfBoundsException("Too many bytes to read - Need " +
|
|
|
|
(index + bytesToCopy) + ", maximum is " + buffer.limit());
|
|
|
|
}
|
|
|
|
dst.put(tmpBuf);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setByte(int index, int value) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
buffer.put(index, (byte) value);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setShort(int index, int value) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
buffer.putShort(index, (short) value);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setMedium(int index, int value) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
setByte(index, (byte) (value >>> 16));
|
|
|
|
setByte(index + 1, (byte) (value >>> 8));
|
2012-11-09 09:26:11 +01:00
|
|
|
setByte(index + 2, (byte) value);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setInt(int index, int value) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
buffer.putInt(index, value);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setLong(int index, long value) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
buffer.putLong(index, value);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
|
|
|
if (src instanceof UnpooledDirectByteBuf) {
|
|
|
|
UnpooledDirectByteBuf bbsrc = (UnpooledDirectByteBuf) src;
|
|
|
|
ByteBuffer data = bbsrc.internalNioBuffer();
|
2012-07-19 13:23:55 +02:00
|
|
|
|
|
|
|
data.clear().position(srcIndex).limit(srcIndex + length);
|
|
|
|
setBytes(index, data);
|
|
|
|
} else if (buffer.hasArray()) {
|
|
|
|
src.getBytes(srcIndex, buffer.array(), index + buffer.arrayOffset(), length);
|
|
|
|
} else {
|
|
|
|
src.getBytes(srcIndex, this, index, length);
|
|
|
|
}
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
2012-07-19 13:23:55 +02:00
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
|
|
|
tmpBuf.put(src, srcIndex, length);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setBytes(int index, ByteBuffer src) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
2012-07-19 13:23:55 +02:00
|
|
|
if (src == tmpBuf) {
|
|
|
|
src = src.duplicate();
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpBuf.clear().position(index).limit(index + src.remaining());
|
|
|
|
tmpBuf.put(src);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
if (length == 0) {
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer.hasArray()) {
|
|
|
|
out.write(buffer.array(), index + buffer.arrayOffset(), length);
|
|
|
|
} else {
|
|
|
|
byte[] tmp = new byte[length];
|
2012-11-15 22:04:37 +01:00
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
2012-07-19 13:23:55 +02:00
|
|
|
tmpBuf.clear().position(index);
|
|
|
|
tmpBuf.get(tmp);
|
|
|
|
out.write(tmp);
|
|
|
|
}
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
if (length == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
2012-07-19 13:23:55 +02:00
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
|
|
|
return out.write(tmpBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int setBytes(int index, InputStream in, int length) throws IOException {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
if (buffer.hasArray()) {
|
|
|
|
return in.read(buffer.array(), buffer.arrayOffset() + index, length);
|
|
|
|
} else {
|
|
|
|
byte[] tmp = new byte[length];
|
|
|
|
int readBytes = in.read(tmp);
|
2012-12-05 22:11:48 +01:00
|
|
|
if (readBytes <= 0) {
|
|
|
|
return readBytes;
|
|
|
|
}
|
2012-11-15 22:04:37 +01:00
|
|
|
ByteBuffer tmpNioBuf = internalNioBuffer();
|
|
|
|
tmpNioBuf.clear().position(index);
|
2012-12-05 22:11:48 +01:00
|
|
|
tmpNioBuf.put(tmp, 0, readBytes);
|
2012-07-19 13:23:55 +02:00
|
|
|
return readBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
|
|
|
ByteBuffer tmpNioBuf = internalNioBuffer();
|
|
|
|
tmpNioBuf.clear().position(index).limit(index + length);
|
2012-07-19 13:23:55 +02:00
|
|
|
try {
|
2012-11-15 22:04:37 +01:00
|
|
|
return in.read(tmpNioBuf);
|
2012-07-19 13:23:55 +02:00
|
|
|
} catch (ClosedChannelException e) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-14 04:20:33 +01:00
|
|
|
public int nioBufferCount() {
|
|
|
|
return 1;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuffer nioBuffer(int index, int length) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
if (index == 0 && length == capacity()) {
|
|
|
|
return buffer.duplicate();
|
|
|
|
} else {
|
2012-11-15 22:04:37 +01:00
|
|
|
return ((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length)).slice();
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-13 02:29:07 +02:00
|
|
|
@Override
|
2012-12-05 22:11:48 +01:00
|
|
|
public ByteBuffer[] nioBuffers(int index, int length) {
|
2012-12-14 04:20:33 +01:00
|
|
|
return new ByteBuffer[] { nioBuffer(index, length) };
|
2012-08-13 02:29:07 +02:00
|
|
|
}
|
|
|
|
|
2012-07-19 13:23:55 +02:00
|
|
|
@Override
|
|
|
|
public ByteBuf copy(int index, int length) {
|
2012-11-15 22:04:37 +01:00
|
|
|
assert !freed;
|
2012-07-19 13:23:55 +02:00
|
|
|
ByteBuffer src;
|
|
|
|
try {
|
2012-11-15 22:04:37 +01:00
|
|
|
src = (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
|
2012-07-19 13:23:55 +02:00
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
throw new IndexOutOfBoundsException("Too many bytes to read - Need " + (index + length));
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer dst =
|
|
|
|
src.isDirect()? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length);
|
|
|
|
dst.put(src);
|
|
|
|
dst.order(order());
|
|
|
|
dst.clear();
|
2012-11-15 22:04:37 +01:00
|
|
|
return new UnpooledDirectByteBuf(alloc(), dst, maxCapacity());
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-11-15 22:04:37 +01:00
|
|
|
public ByteBuffer internalNioBuffer() {
|
|
|
|
ByteBuffer tmpNioBuf = this.tmpNioBuf;
|
|
|
|
if (tmpNioBuf == null) {
|
|
|
|
this.tmpNioBuf = tmpNioBuf = buffer.duplicate();
|
|
|
|
}
|
|
|
|
return tmpNioBuf;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
@Override
|
|
|
|
public ByteBuffer[] internalNioBuffers() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
2012-07-19 13:23:55 +02:00
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
@Override
|
|
|
|
public void discardSomeReadBytes() {
|
|
|
|
final int readerIndex = readerIndex();
|
|
|
|
if (readerIndex == writerIndex()) {
|
|
|
|
discardReadBytes();
|
|
|
|
return;
|
2012-08-13 02:29:07 +02:00
|
|
|
}
|
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
if (readerIndex > 0 && readerIndex >= capacity >>> 1) {
|
|
|
|
discardReadBytes();
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
2012-11-15 22:04:37 +01:00
|
|
|
}
|
2012-07-19 13:23:55 +02:00
|
|
|
|
2012-12-02 12:17:53 +01:00
|
|
|
@Override
|
|
|
|
public boolean isFreed() {
|
|
|
|
return freed;
|
|
|
|
}
|
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
@Override
|
|
|
|
public void free() {
|
|
|
|
if (freed) {
|
|
|
|
return;
|
2012-08-08 10:34:00 +02:00
|
|
|
}
|
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
freed = true;
|
|
|
|
if (doNotFree) {
|
|
|
|
return;
|
2012-07-20 05:33:17 +02:00
|
|
|
}
|
2012-07-19 13:23:55 +02:00
|
|
|
|
2012-12-02 13:50:33 +01:00
|
|
|
resumeIntermediaryDeallocations();
|
2012-11-15 22:04:37 +01:00
|
|
|
freeDirect(buffer);
|
|
|
|
}
|
2012-07-19 13:23:55 +02:00
|
|
|
|
2012-12-02 13:50:33 +01:00
|
|
|
@Override
|
|
|
|
public void suspendIntermediaryDeallocations() {
|
|
|
|
if (suspendedDeallocations == null) {
|
|
|
|
suspendedDeallocations = new ArrayDeque<ByteBuffer>(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void resumeIntermediaryDeallocations() {
|
|
|
|
if (suspendedDeallocations == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Queue<ByteBuffer> suspendedDeallocations = this.suspendedDeallocations;
|
|
|
|
this.suspendedDeallocations = null;
|
|
|
|
|
|
|
|
for (ByteBuffer buf: suspendedDeallocations) {
|
|
|
|
freeDirect(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
@Override
|
|
|
|
public ByteBuf unwrap() {
|
|
|
|
return null;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
2012-12-05 11:28:56 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Unsafe unsafe() {
|
|
|
|
return this;
|
|
|
|
}
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|