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;
|
|
|
|
|
2013-01-11 14:47:54 +01:00
|
|
|
import io.netty.util.ResourceLeak;
|
2013-01-11 06:03:27 +01:00
|
|
|
import io.netty.util.internal.PlatformDependent;
|
2012-11-09 09:26:11 +01:00
|
|
|
|
2012-07-19 13:23:55 +02: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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
|
2012-07-19 13:23:55 +02:00
|
|
|
|
2013-04-22 10:07:01 +02:00
|
|
|
private final ResourceLeak leak;
|
2012-11-15 22:04:37 +01:00
|
|
|
private final ByteBufAllocator alloc;
|
2013-01-11 14:47:54 +01:00
|
|
|
|
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 doNotFree;
|
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
|
|
|
|
*/
|
2013-04-04 10:26:14 +02:00
|
|
|
protected UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
|
2012-11-15 22:04:37 +01:00
|
|
|
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));
|
2013-04-22 10:07:01 +02:00
|
|
|
leak = leakDetector.open(this);
|
2012-07-19 13:23:55 +02: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 UnpooledDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity) {
|
2012-11-15 22:04:37 +01:00
|
|
|
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);
|
2013-04-22 10:07:01 +02:00
|
|
|
leak = leakDetector.open(this);
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setByteBuffer(ByteBuffer buffer) {
|
|
|
|
ByteBuffer oldBuffer = this.buffer;
|
|
|
|
if (oldBuffer != null) {
|
|
|
|
if (doNotFree) {
|
|
|
|
doNotFree = false;
|
|
|
|
} else {
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 13:40:19 +02:00
|
|
|
PlatformDependent.freeDirectBuffer(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) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
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");
|
|
|
|
}
|
|
|
|
|
2013-03-06 10:22:16 +01:00
|
|
|
@Override
|
|
|
|
public boolean hasMemoryAddress() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long memoryAddress() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2012-07-19 13:23:55 +02:00
|
|
|
@Override
|
|
|
|
public byte getByte(int index) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2013-03-06 02:32:29 +01:00
|
|
|
return _getByte(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected byte _getByte(int index) {
|
2012-07-19 13:23:55 +02:00
|
|
|
return buffer.get(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public short getShort(int index) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2013-03-06 02:32:29 +01:00
|
|
|
return _getShort(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected short _getShort(int index) {
|
2012-07-19 13:23:55 +02:00
|
|
|
return buffer.getShort(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getUnsignedMedium(int index) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2013-03-06 02:32:29 +01:00
|
|
|
return _getUnsignedMedium(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected int _getUnsignedMedium(int index) {
|
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) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2013-03-06 02:32:29 +01:00
|
|
|
return _getInt(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected int _getInt(int index) {
|
2012-07-19 13:23:55 +02:00
|
|
|
return buffer.getInt(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getLong(int index) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2013-03-06 02:32:29 +01:00
|
|
|
return _getLong(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected long _getLong(int index) {
|
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) {
|
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-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) {
|
2013-03-06 10:22:16 +01:00
|
|
|
checkDstIndex(index, length, dstIndex, dst.length);
|
2013-01-18 05:10:54 +01:00
|
|
|
if (dst == null) {
|
|
|
|
throw new NullPointerException("dst");
|
|
|
|
}
|
|
|
|
if (dstIndex < 0 || dstIndex > dst.length - length) {
|
|
|
|
throw new IndexOutOfBoundsException(String.format(
|
|
|
|
"dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length));
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
2013-01-18 05:10:54 +01:00
|
|
|
|
|
|
|
ByteBuffer tmpBuf = internalNioBuffer();
|
|
|
|
tmpBuf.clear().position(index).limit(index + length);
|
2012-07-19 13:23:55 +02:00
|
|
|
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) {
|
2013-01-18 05:10:54 +01:00
|
|
|
checkIndex(index);
|
|
|
|
if (dst == null) {
|
|
|
|
throw new NullPointerException("dst");
|
|
|
|
}
|
|
|
|
|
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();
|
2013-01-18 05:10:54 +01:00
|
|
|
tmpBuf.clear().position(index).limit(index + bytesToCopy);
|
2012-07-19 13:23:55 +02:00
|
|
|
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) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2013-03-06 02:32:29 +01:00
|
|
|
_setByte(index, value);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
2013-03-06 02:32:29 +01:00
|
|
|
@Override
|
|
|
|
protected void _setByte(int index, int value) {
|
|
|
|
buffer.put(index, (byte) value);
|
|
|
|
}
|
|
|
|
|
2012-07-19 13:23:55 +02:00
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setShort(int index, int value) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2013-03-06 02:32:29 +01:00
|
|
|
_setShort(index, value);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
2013-03-06 02:32:29 +01:00
|
|
|
@Override
|
|
|
|
protected void _setShort(int index, int value) {
|
|
|
|
buffer.putShort(index, (short) value);
|
|
|
|
}
|
|
|
|
|
2012-07-19 13:23:55 +02:00
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setMedium(int index, int value) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2013-03-06 02:32:29 +01:00
|
|
|
_setMedium(index, value);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void _setMedium(int index, int value) {
|
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-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setInt(int index, int value) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2013-03-06 02:32:29 +01:00
|
|
|
_setInt(index, value);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
2013-03-06 02:32:29 +01:00
|
|
|
@Override
|
|
|
|
protected void _setInt(int index, int value) {
|
|
|
|
buffer.putInt(index, value);
|
|
|
|
}
|
|
|
|
|
2012-07-19 13:23:55 +02:00
|
|
|
@Override
|
2012-10-18 08:57:23 +02:00
|
|
|
public ByteBuf setLong(int index, long value) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2013-03-06 02:32:29 +01:00
|
|
|
_setLong(index, value);
|
2012-10-18 08:57:23 +02:00
|
|
|
return this;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
2013-03-06 02:32:29 +01:00
|
|
|
@Override
|
|
|
|
protected void _setLong(int index, long value) {
|
|
|
|
buffer.putLong(index, value);
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-03-06 10:22:16 +01:00
|
|
|
checkSrcIndex(index, length, srcIndex, src.capacity());
|
|
|
|
if (buffer.hasArray()) {
|
2012-07-19 13:23:55 +02:00
|
|
|
src.getBytes(srcIndex, buffer.array(), index + buffer.arrayOffset(), 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-07-19 13:23:55 +02:00
|
|
|
} 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) {
|
2013-03-06 10:22:16 +01:00
|
|
|
checkSrcIndex(index, length, srcIndex, src.length);
|
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);
|
|
|
|
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) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2012-11-15 22:04:37 +01:00
|
|
|
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 {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
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 {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
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 {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
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 {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
2012-11-15 22:04:37 +01:00
|
|
|
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) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
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) {
|
2013-02-10 05:10:09 +01:00
|
|
|
ensureAccessible();
|
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
|
|
|
}
|
|
|
|
|
2012-12-17 09:41:21 +01:00
|
|
|
private ByteBuffer internalNioBuffer() {
|
2012-11-15 22:04:37 +01:00
|
|
|
ByteBuffer tmpNioBuf = this.tmpNioBuf;
|
|
|
|
if (tmpNioBuf == null) {
|
|
|
|
this.tmpNioBuf = tmpNioBuf = buffer.duplicate();
|
|
|
|
}
|
|
|
|
return tmpNioBuf;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
|
2012-12-02 12:17:53 +01:00
|
|
|
@Override
|
2013-02-10 05:10:09 +01:00
|
|
|
protected void deallocate() {
|
2012-12-17 10:27:30 +01:00
|
|
|
ByteBuffer buffer = this.buffer;
|
|
|
|
if (buffer == null) {
|
2012-11-15 22:04:37 +01:00
|
|
|
return;
|
2012-08-08 10:34:00 +02:00
|
|
|
}
|
|
|
|
|
2012-12-17 10:27:30 +01:00
|
|
|
this.buffer = null;
|
|
|
|
|
2013-01-11 14:47:54 +01:00
|
|
|
if (!doNotFree) {
|
|
|
|
PlatformDependent.freeDirectBuffer(buffer);
|
|
|
|
}
|
|
|
|
leak.close();
|
2012-11-15 22:04:37 +01:00
|
|
|
}
|
2012-07-19 13:23:55 +02:00
|
|
|
|
2012-11-15 22:04:37 +01:00
|
|
|
@Override
|
|
|
|
public ByteBuf unwrap() {
|
|
|
|
return null;
|
2012-07-19 13:23:55 +02:00
|
|
|
}
|
|
|
|
}
|