2012-12-05 22:11:48 +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;
|
|
|
|
|
2013-06-10 12:52:56 +02:00
|
|
|
import io.netty.util.Recycler;
|
|
|
|
|
2012-12-05 22:11:48 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.channels.ClosedChannelException;
|
|
|
|
import java.nio.channels.GatheringByteChannel;
|
|
|
|
import java.nio.channels.ScatteringByteChannel;
|
|
|
|
|
|
|
|
final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|
|
|
|
2013-06-10 12:52:56 +02:00
|
|
|
private static final Recycler<PooledDirectByteBuf> RECYCLER = new Recycler<PooledDirectByteBuf>() {
|
|
|
|
@Override
|
2013-12-18 15:05:37 +01:00
|
|
|
protected PooledDirectByteBuf newObject(Handle<PooledDirectByteBuf> handle) {
|
2013-06-11 21:18:40 +02:00
|
|
|
return new PooledDirectByteBuf(handle, 0);
|
2013-06-10 12:52:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PooledDirectByteBuf newInstance(int maxCapacity) {
|
2013-06-11 21:18:40 +02:00
|
|
|
PooledDirectByteBuf buf = RECYCLER.get();
|
2013-07-23 07:06:58 +02:00
|
|
|
buf.setRefCnt(1);
|
2013-06-11 21:18:40 +02:00
|
|
|
buf.maxCapacity(maxCapacity);
|
|
|
|
return buf;
|
2013-06-10 12:52:56 +02:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:05:37 +01:00
|
|
|
private PooledDirectByteBuf(Recycler.Handle<PooledDirectByteBuf> recyclerHandle, int maxCapacity) {
|
2013-06-10 12:52:56 +02:00
|
|
|
super(recyclerHandle, maxCapacity);
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ByteBuffer newInternalNioBuffer(ByteBuffer memory) {
|
|
|
|
return memory.duplicate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isDirect() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected byte _getByte(int index) {
|
2012-12-05 22:11:48 +01:00
|
|
|
return memory.get(idx(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected short _getShort(int index) {
|
2012-12-05 22:11:48 +01:00
|
|
|
return memory.getShort(idx(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected int _getUnsignedMedium(int index) {
|
2012-12-05 22:11:48 +01:00
|
|
|
index = idx(index);
|
|
|
|
return (memory.get(index) & 0xff) << 16 | (memory.get(index + 1) & 0xff) << 8 | memory.get(index + 2) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected int _getInt(int index) {
|
2012-12-05 22:11:48 +01:00
|
|
|
return memory.getInt(idx(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected long _getLong(int index) {
|
2012-12-05 22:11:48 +01:00
|
|
|
return memory.getLong(idx(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
2013-03-06 10:22:16 +01:00
|
|
|
checkDstIndex(index, length, dstIndex, dst.capacity());
|
|
|
|
if (dst.hasArray()) {
|
2012-12-05 22:11:48 +01:00
|
|
|
getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
|
2013-03-06 10:22:16 +01:00
|
|
|
} else if (dst.nioBufferCount() > 0) {
|
|
|
|
for (ByteBuffer bb: dst.nioBuffers(dstIndex, length)) {
|
|
|
|
int bbLen = bb.remaining();
|
|
|
|
getBytes(index, bb);
|
|
|
|
index += bbLen;
|
|
|
|
}
|
2012-12-05 22:11:48 +01:00
|
|
|
} else {
|
|
|
|
dst.setBytes(dstIndex, this, index, length);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
2013-09-20 16:24:46 +02:00
|
|
|
getBytes(index, dst, dstIndex, length, false);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getBytes(int index, byte[] dst, int dstIndex, int length, boolean internal) {
|
2013-03-06 10:22:16 +01:00
|
|
|
checkDstIndex(index, length, dstIndex, dst.length);
|
2013-09-20 16:24:46 +02:00
|
|
|
ByteBuffer tmpBuf;
|
|
|
|
if (internal) {
|
|
|
|
tmpBuf = internalNioBuffer();
|
|
|
|
} else {
|
|
|
|
tmpBuf = memory.duplicate();
|
|
|
|
}
|
2012-12-05 22:11:48 +01:00
|
|
|
index = idx(index);
|
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
|
|
|
tmpBuf.get(dst, dstIndex, length);
|
2013-09-20 16:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf readBytes(byte[] dst, int dstIndex, int length) {
|
|
|
|
checkReadableBytes(length);
|
|
|
|
getBytes(readerIndex, dst, dstIndex, length, true);
|
|
|
|
readerIndex += length;
|
2012-12-05 22:11:48 +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) {
|
2012-12-05 22:11:48 +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();
|
|
|
|
}
|
2012-12-05 22:11:48 +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;
|
2012-12-05 22:11:48 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
|
2013-09-20 16:24:46 +02:00
|
|
|
getBytes(index, out, length, false);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getBytes(int index, OutputStream out, int length, boolean internal) throws IOException {
|
2012-12-05 22:11:48 +01:00
|
|
|
checkIndex(index, length);
|
|
|
|
if (length == 0) {
|
2013-09-20 16:24:46 +02:00
|
|
|
return;
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
byte[] tmp = new byte[length];
|
2013-09-20 16:24:46 +02:00
|
|
|
ByteBuffer tmpBuf;
|
|
|
|
if (internal) {
|
|
|
|
tmpBuf = internalNioBuffer();
|
|
|
|
} else {
|
|
|
|
tmpBuf = memory.duplicate();
|
|
|
|
}
|
2012-12-05 22:11:48 +01:00
|
|
|
tmpBuf.clear().position(idx(index));
|
|
|
|
tmpBuf.get(tmp);
|
|
|
|
out.write(tmp);
|
2013-09-20 16:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf readBytes(OutputStream out, int length) throws IOException {
|
|
|
|
checkReadableBytes(length);
|
|
|
|
getBytes(readerIndex, out, length, true);
|
|
|
|
readerIndex += length;
|
2012-12-05 22:11:48 +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 {
|
2012-12-05 22:11:48 +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();
|
|
|
|
}
|
2012-12-05 22:11:48 +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;
|
|
|
|
}
|
|
|
|
|
2012-12-05 22:11:48 +01:00
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected void _setByte(int index, int value) {
|
2012-12-05 22:11:48 +01:00
|
|
|
memory.put(idx(index), (byte) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected void _setShort(int index, int value) {
|
2012-12-05 22:11:48 +01:00
|
|
|
memory.putShort(idx(index), (short) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected void _setMedium(int index, int value) {
|
2012-12-05 22:11:48 +01:00
|
|
|
index = idx(index);
|
|
|
|
memory.put(index, (byte) (value >>> 16));
|
|
|
|
memory.put(index + 1, (byte) (value >>> 8));
|
|
|
|
memory.put(index + 2, (byte) value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected void _setInt(int index, int value) {
|
2012-12-05 22:11:48 +01:00
|
|
|
memory.putInt(idx(index), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-06 02:32:29 +01:00
|
|
|
protected void _setLong(int index, long value) {
|
2012-12-05 22:11:48 +01:00
|
|
|
memory.putLong(idx(index), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
|
2013-03-06 10:22:16 +01:00
|
|
|
checkSrcIndex(index, length, srcIndex, src.capacity());
|
|
|
|
if (src.hasArray()) {
|
2012-12-05 22:11:48 +01:00
|
|
|
setBytes(index, src.array(), src.arrayOffset() + srcIndex, length);
|
2013-03-06 10:22:16 +01:00
|
|
|
} else if (src.nioBufferCount() > 0) {
|
|
|
|
for (ByteBuffer bb: src.nioBuffers(srcIndex, length)) {
|
|
|
|
int bbLen = bb.remaining();
|
|
|
|
setBytes(index, bb);
|
|
|
|
index += bbLen;
|
|
|
|
}
|
2012-12-05 22:11:48 +01:00
|
|
|
} else {
|
|
|
|
src.getBytes(srcIndex, this, index, length);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
|
2013-03-06 10:22:16 +01:00
|
|
|
checkSrcIndex(index, length, srcIndex, src.length);
|
2012-12-05 22:11:48 +01:00
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
|
|
|
index = idx(index);
|
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
|
|
|
tmpBuf.put(src, srcIndex, length);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf setBytes(int index, ByteBuffer src) {
|
2014-05-09 22:32:43 +02:00
|
|
|
checkIndex(index, src.remaining());
|
2012-12-05 22:11:48 +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);
|
|
|
|
if (readBytes <= 0) {
|
|
|
|
return readBytes;
|
|
|
|
}
|
2013-09-20 16:24:46 +02:00
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
|
|
|
tmpBuf.clear().position(idx(index));
|
|
|
|
tmpBuf.put(tmp, 0, readBytes);
|
2012-12-05 22:11:48 +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();
|
2012-12-05 22:11:48 +01:00
|
|
|
index = idx(index);
|
2013-09-20 16:24:46 +02:00
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
2012-12-05 22:11:48 +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) {
|
2012-12-05 22:11:48 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuf copy(int index, int length) {
|
|
|
|
checkIndex(index, length);
|
2013-03-06 10:22:16 +01:00
|
|
|
ByteBuf copy = alloc().directBuffer(length, maxCapacity());
|
2012-12-05 22:11:48 +01:00
|
|
|
copy.writeBytes(this, index, length);
|
2013-01-10 10:43:20 +01:00
|
|
|
return copy;
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-14 04:20:33 +01:00
|
|
|
public int nioBufferCount() {
|
|
|
|
return 1;
|
2012-12-05 22:11:48 +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
|
|
|
}
|
|
|
|
|
2012-12-05 22:11:48 +01:00
|
|
|
@Override
|
2013-06-13 05:40:26 +02:00
|
|
|
public ByteBuffer[] nioBuffers(int index, int length) {
|
|
|
|
return new ByteBuffer[] { nioBuffer(index, length) };
|
2012-12-05 22:11:48 +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);
|
2012-12-05 22:11:48 +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 false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long memoryAddress() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|