2008-08-08 02:37:18 +02:00
|
|
|
/*
|
2008-08-08 03:27:24 +02:00
|
|
|
* JBoss, Home of Professional Open Source
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2008-08-08 03:27:24 +02:00
|
|
|
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
|
|
|
|
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
|
|
|
* full listing of individual contributors.
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2008-08-08 03:27:24 +02:00
|
|
|
* This is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful,
|
2008-08-08 02:37:18 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-08 03:27:24 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2008-08-08 02:37:18 +02:00
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2008-08-08 03:27:24 +02:00
|
|
|
* License along with this software; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
2008-08-08 02:37:18 +02:00
|
|
|
*/
|
2008-08-08 03:40:10 +02:00
|
|
|
package org.jboss.netty.buffer;
|
2008-08-08 02:37:18 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2008-08-08 12:17:26 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2008-08-08 02:37:18 +02:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
import java.nio.channels.GatheringByteChannel;
|
|
|
|
import java.nio.channels.ScatteringByteChannel;
|
2008-08-08 12:17:26 +02:00
|
|
|
import java.nio.charset.UnsupportedCharsetException;
|
2008-08-08 02:37:18 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-09-02 09:13:20 +02:00
|
|
|
* A virtual buffer which shows multiple buffers as a single merged buffer. It
|
|
|
|
* is recommended to use {@link ChannelBuffers#wrappedBuffer(ChannelBuffer...)}
|
|
|
|
* instead of calling the constructor explicitly.
|
2008-08-10 07:52:36 +02:00
|
|
|
*
|
2008-08-08 03:27:24 +02:00
|
|
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
|
|
|
* @author Trustin Lee (tlee@redhat.com)
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
|
|
|
* @version $Rev$, $Date$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class CompositeChannelBuffer extends AbstractChannelBuffer {
|
|
|
|
|
|
|
|
private final ChannelBuffer[] slices;
|
2008-08-27 14:21:04 +02:00
|
|
|
private final ByteOrder order;
|
2008-08-08 02:37:18 +02:00
|
|
|
private final int[] indices;
|
|
|
|
private int lastSliceId;
|
|
|
|
|
|
|
|
public CompositeChannelBuffer(ChannelBuffer... buffers) {
|
|
|
|
if (buffers.length == 0) {
|
|
|
|
throw new IllegalArgumentException("buffers should not be empty.");
|
|
|
|
}
|
|
|
|
|
2008-08-27 09:17:41 +02:00
|
|
|
ByteOrder expectedEndianness = null;
|
|
|
|
for (ChannelBuffer buffer : buffers) {
|
|
|
|
if (buffer.capacity() != 0) {
|
|
|
|
expectedEndianness = buffer.order();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expectedEndianness == null) {
|
|
|
|
throw new IllegalArgumentException("buffers have only empty buffers.");
|
|
|
|
}
|
|
|
|
|
2008-08-27 14:21:04 +02:00
|
|
|
order = expectedEndianness;
|
2008-08-08 02:37:18 +02:00
|
|
|
slices = new ChannelBuffer[buffers.length];
|
|
|
|
for (int i = 0; i < buffers.length; i ++) {
|
2008-08-27 09:17:41 +02:00
|
|
|
if (buffers[i].capacity() != 0 && buffers[i].order() != expectedEndianness) {
|
2008-08-08 02:37:18 +02:00
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"All buffers must have the same endianness.");
|
|
|
|
}
|
|
|
|
slices[i] = buffers[i].slice();
|
|
|
|
}
|
|
|
|
indices = new int[buffers.length + 1];
|
|
|
|
for (int i = 1; i <= buffers.length; i ++) {
|
|
|
|
indices[i] = indices[i - 1] + slices[i - 1].capacity();
|
|
|
|
}
|
|
|
|
writerIndex(capacity());
|
|
|
|
}
|
|
|
|
|
|
|
|
private CompositeChannelBuffer(CompositeChannelBuffer buffer) {
|
2008-08-27 14:21:04 +02:00
|
|
|
order = buffer.order;
|
2008-08-08 02:37:18 +02:00
|
|
|
slices = buffer.slices.clone();
|
|
|
|
indices = buffer.indices.clone();
|
|
|
|
setIndex(buffer.readerIndex(), buffer.writerIndex());
|
|
|
|
}
|
|
|
|
|
2008-12-08 09:20:34 +01:00
|
|
|
public ChannelBufferFactory factory() {
|
|
|
|
return HeapChannelBufferFactory.getInstance(order());
|
|
|
|
}
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
public ByteOrder order() {
|
2008-08-27 14:21:04 +02:00
|
|
|
return order;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int capacity() {
|
|
|
|
return indices[slices.length];
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte getByte(int index) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
return slices[sliceId].getByte(index - indices[sliceId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public short getShort(int index) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
if (index + 2 <= indices[sliceId + 1]) {
|
|
|
|
return slices[sliceId].getShort(index - indices[sliceId]);
|
|
|
|
} else if (order() == ByteOrder.BIG_ENDIAN) {
|
|
|
|
return (short) ((getByte(index) & 0xff) << 8 | getByte(index + 1) & 0xff);
|
|
|
|
} else {
|
|
|
|
return (short) (getByte(index) & 0xff | (getByte(index + 1) & 0xff) << 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-10 02:52:59 +02:00
|
|
|
public int getUnsignedMedium(int index) {
|
2008-08-08 02:37:18 +02:00
|
|
|
int sliceId = sliceId(index);
|
|
|
|
if (index + 3 <= indices[sliceId + 1]) {
|
2008-08-10 02:52:59 +02:00
|
|
|
return slices[sliceId].getUnsignedMedium(index - indices[sliceId]);
|
2008-08-08 02:37:18 +02:00
|
|
|
} else if (order() == ByteOrder.BIG_ENDIAN) {
|
|
|
|
return (getShort(index) & 0xffff) << 8 | getByte(index + 2) & 0xff;
|
|
|
|
} else {
|
|
|
|
return getShort(index) & 0xFFFF | (getByte(index + 2) & 0xFF) << 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getInt(int index) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
if (index + 4 <= indices[sliceId + 1]) {
|
|
|
|
return slices[sliceId].getInt(index - indices[sliceId]);
|
|
|
|
} else if (order() == ByteOrder.BIG_ENDIAN) {
|
|
|
|
return (getShort(index) & 0xffff) << 16 | getShort(index + 2) & 0xffff;
|
|
|
|
} else {
|
|
|
|
return getShort(index) & 0xFFFF | (getShort(index + 2) & 0xFFFF) << 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getLong(int index) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
if (index + 8 <= indices[sliceId + 1]) {
|
|
|
|
return slices[sliceId].getLong(index - indices[sliceId]);
|
|
|
|
} else if (order() == ByteOrder.BIG_ENDIAN) {
|
|
|
|
return (getInt(index) & 0xffffffffL) << 32 | getInt(index + 4) & 0xffffffffL;
|
|
|
|
} else {
|
|
|
|
return getInt(index) & 0xFFFFFFFFL | (getInt(index + 4) & 0xFFFFFFFFL) << 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
|
|
|
|
int sliceId = sliceId(index);
|
2008-08-26 09:30:41 +02:00
|
|
|
if (index > capacity() - length || dstIndex > dst.length - length) {
|
2008-08-08 02:37:18 +02:00
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = sliceId;
|
|
|
|
while (length > 0) {
|
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
|
|
|
s.getBytes(index - adjustment, dst, dstIndex, localLength);
|
|
|
|
index += localLength;
|
|
|
|
dstIndex += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getBytes(int index, ByteBuffer dst) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
int limit = dst.limit();
|
|
|
|
int length = dst.remaining();
|
2008-08-26 09:30:41 +02:00
|
|
|
if (index > capacity() - length) {
|
2008-08-08 02:37:18 +02:00
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = sliceId;
|
|
|
|
try {
|
|
|
|
while (length > 0) {
|
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
|
|
|
dst.limit(dst.position() + localLength);
|
|
|
|
s.getBytes(index - adjustment, dst);
|
|
|
|
index += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
dst.limit(limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
|
|
|
|
int sliceId = sliceId(index);
|
2008-08-26 09:30:41 +02:00
|
|
|
if (index > capacity() - length || dstIndex > dst.capacity() - length) {
|
2008-08-08 02:37:18 +02:00
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = sliceId;
|
|
|
|
while (length > 0) {
|
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
|
|
|
s.getBytes(index - adjustment, dst, dstIndex, localLength);
|
|
|
|
index += localLength;
|
|
|
|
dstIndex += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getBytes(int index, GatheringByteChannel out, int length)
|
|
|
|
throws IOException {
|
|
|
|
// XXX Gathering write is not supported because of a known issue.
|
|
|
|
// See http://bugs.sun.com/view_bug.do?bug_id=6210541
|
2009-03-12 10:21:45 +01:00
|
|
|
// This issue appeared in 2004 and is still unresolved!?
|
2009-03-04 14:43:56 +01:00
|
|
|
return out.write(toByteBuffer(index, length));
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void getBytes(int index, OutputStream out, int length)
|
|
|
|
throws IOException {
|
|
|
|
int sliceId = sliceId(index);
|
2008-08-26 09:30:41 +02:00
|
|
|
if (index > capacity() - length) {
|
2008-08-08 02:37:18 +02:00
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = sliceId;
|
|
|
|
while (length > 0) {
|
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
|
|
|
s.getBytes(index - adjustment, out, localLength);
|
|
|
|
index += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setByte(int index, byte value) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
slices[sliceId].setByte(index - indices[sliceId], value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setShort(int index, short value) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
if (index + 2 <= indices[sliceId + 1]) {
|
|
|
|
slices[sliceId].setShort(index - indices[sliceId], value);
|
|
|
|
} else if (order() == ByteOrder.BIG_ENDIAN) {
|
|
|
|
setByte(index, (byte) (value >>> 8));
|
|
|
|
setByte(index + 1, (byte) value);
|
|
|
|
} else {
|
|
|
|
setByte(index , (byte) value);
|
|
|
|
setByte(index + 1, (byte) (value >>> 8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMedium(int index, int value) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
if (index + 3 <= indices[sliceId + 1]) {
|
|
|
|
slices[sliceId].setMedium(index - indices[sliceId], value);
|
|
|
|
} else if (order() == ByteOrder.BIG_ENDIAN) {
|
2008-11-07 03:08:36 +01:00
|
|
|
setShort(index, (short) (value >> 8));
|
2008-08-08 02:37:18 +02:00
|
|
|
setByte(index + 2, (byte) value);
|
|
|
|
} else {
|
|
|
|
setShort(index , (short) value);
|
|
|
|
setByte (index + 2, (byte) (value >>> 16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setInt(int index, int value) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
if (index + 4 <= indices[sliceId + 1]) {
|
|
|
|
slices[sliceId].setInt(index - indices[sliceId], value);
|
|
|
|
} else if (order() == ByteOrder.BIG_ENDIAN) {
|
|
|
|
setShort(index, (short) (value >>> 16));
|
|
|
|
setShort(index + 2, (short) value);
|
|
|
|
} else {
|
|
|
|
setShort(index , (short) value);
|
|
|
|
setShort(index + 2, (short) (value >>> 16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLong(int index, long value) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
if (index + 8 <= indices[sliceId + 1]) {
|
|
|
|
slices[sliceId].setLong(index - indices[sliceId], value);
|
|
|
|
} else if (order() == ByteOrder.BIG_ENDIAN) {
|
|
|
|
setInt(index, (int) (value >>> 32));
|
|
|
|
setInt(index + 4, (int) value);
|
|
|
|
} else {
|
|
|
|
setInt(index , (int) value);
|
|
|
|
setInt(index + 4, (int) (value >>> 32));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBytes(int index, byte[] src, int srcIndex, int length) {
|
|
|
|
int sliceId = sliceId(index);
|
2008-08-26 09:30:41 +02:00
|
|
|
if (index > capacity() - length || srcIndex > src.length - length) {
|
2008-08-08 02:37:18 +02:00
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = sliceId;
|
|
|
|
while (length > 0) {
|
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
|
|
|
s.setBytes(index - adjustment, src, srcIndex, localLength);
|
|
|
|
index += localLength;
|
|
|
|
srcIndex += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBytes(int index, ByteBuffer src) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
int limit = src.limit();
|
|
|
|
int length = src.remaining();
|
2008-08-26 09:30:41 +02:00
|
|
|
if (index > capacity() - length) {
|
2008-08-08 02:37:18 +02:00
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = sliceId;
|
|
|
|
try {
|
|
|
|
while (length > 0) {
|
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
|
|
|
src.limit(src.position() + localLength);
|
|
|
|
s.setBytes(index - adjustment, src);
|
|
|
|
index += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
src.limit(limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
|
|
|
|
int sliceId = sliceId(index);
|
2008-08-26 09:30:41 +02:00
|
|
|
if (index > capacity() - length || srcIndex > src.capacity() - length) {
|
2008-08-08 02:37:18 +02:00
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = sliceId;
|
|
|
|
while (length > 0) {
|
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
|
|
|
s.setBytes(index - adjustment, src, srcIndex, localLength);
|
|
|
|
index += localLength;
|
|
|
|
srcIndex += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-20 05:09:23 +02:00
|
|
|
public int setBytes(int index, InputStream in, int length)
|
2008-08-08 02:37:18 +02:00
|
|
|
throws IOException {
|
|
|
|
int sliceId = sliceId(index);
|
2008-08-26 09:30:41 +02:00
|
|
|
if (index > capacity() - length) {
|
2008-08-08 02:37:18 +02:00
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = sliceId;
|
2008-08-20 05:09:23 +02:00
|
|
|
int readBytes = 0;
|
|
|
|
|
2008-08-26 09:12:04 +02:00
|
|
|
do {
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
2008-08-20 05:09:23 +02:00
|
|
|
int localReadBytes = s.setBytes(index - adjustment, in, localLength);
|
|
|
|
if (localReadBytes < 0) {
|
|
|
|
if (readBytes == 0) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-08-26 09:12:04 +02:00
|
|
|
|
|
|
|
if (localReadBytes == localLength) {
|
|
|
|
index += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
readBytes += localLength;
|
|
|
|
i ++;
|
|
|
|
} else {
|
|
|
|
index += localReadBytes;
|
|
|
|
length -= localReadBytes;
|
|
|
|
readBytes += localReadBytes;
|
|
|
|
}
|
|
|
|
} while (length > 0);
|
2008-08-20 05:09:23 +02:00
|
|
|
|
|
|
|
return readBytes;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int setBytes(int index, ScatteringByteChannel in, int length)
|
|
|
|
throws IOException {
|
|
|
|
int sliceId = sliceId(index);
|
2008-08-26 09:30:41 +02:00
|
|
|
if (index > capacity() - length) {
|
2008-08-08 02:37:18 +02:00
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = sliceId;
|
2008-08-26 09:12:04 +02:00
|
|
|
int readBytes = 0;
|
|
|
|
do {
|
2008-08-08 02:37:18 +02:00
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
2008-08-26 09:12:04 +02:00
|
|
|
int localReadBytes = s.setBytes(index - adjustment, in, localLength);
|
|
|
|
|
|
|
|
if (localReadBytes == localLength) {
|
|
|
|
index += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
readBytes += localLength;
|
|
|
|
i ++;
|
|
|
|
} else {
|
|
|
|
index += localReadBytes;
|
|
|
|
length -= localReadBytes;
|
|
|
|
readBytes += localReadBytes;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
2008-08-26 09:12:04 +02:00
|
|
|
} while (length > 0);
|
2008-08-08 02:37:18 +02:00
|
|
|
|
2008-08-26 09:12:04 +02:00
|
|
|
return readBytes;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ChannelBuffer duplicate() {
|
2008-08-26 13:08:55 +02:00
|
|
|
ChannelBuffer duplicate = new CompositeChannelBuffer(this);
|
|
|
|
duplicate.setIndex(readerIndex(), writerIndex());
|
|
|
|
return duplicate;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ChannelBuffer copy(int index, int length) {
|
|
|
|
int sliceId = sliceId(index);
|
2008-08-26 09:30:41 +02:00
|
|
|
if (index > capacity() - length) {
|
2008-08-08 02:37:18 +02:00
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
2008-12-08 09:20:34 +01:00
|
|
|
ChannelBuffer dst = factory().getBuffer(order(), length);
|
|
|
|
copyTo(index, length, sliceId, dst);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void copyTo(int index, int length, int sliceId, ChannelBuffer dst) {
|
2008-08-08 02:37:18 +02:00
|
|
|
int dstIndex = 0;
|
|
|
|
int i = sliceId;
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
|
|
|
s.getBytes(index - adjustment, dst, dstIndex, localLength);
|
|
|
|
index += localLength;
|
|
|
|
dstIndex += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst.writerIndex(dst.capacity());
|
|
|
|
}
|
|
|
|
|
|
|
|
public ChannelBuffer slice(int index, int length) {
|
2008-08-27 14:21:04 +02:00
|
|
|
if (index == 0) {
|
|
|
|
if (length == 0) {
|
|
|
|
return ChannelBuffers.EMPTY_BUFFER;
|
|
|
|
} else {
|
|
|
|
return new TruncatedChannelBuffer(this, length);
|
|
|
|
}
|
|
|
|
} else if (index < 0 || index > capacity() - length) {
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
} else if (length == 0) {
|
2008-08-26 17:37:08 +02:00
|
|
|
return ChannelBuffers.EMPTY_BUFFER;
|
2008-08-27 14:21:04 +02:00
|
|
|
} else {
|
|
|
|
return new SlicedChannelBuffer(this, index, length);
|
2008-08-26 17:37:08 +02:00
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ByteBuffer toByteBuffer(int index, int length) {
|
2008-08-08 12:17:26 +02:00
|
|
|
if (slices.length == 1) {
|
|
|
|
return slices[0].toByteBuffer(index, length);
|
|
|
|
}
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
ByteBuffer[] buffers = toByteBuffers(index, length);
|
2009-06-15 08:13:50 +02:00
|
|
|
ByteBuffer merged = ByteBuffer.allocate(length).order(order());
|
2008-08-08 02:37:18 +02:00
|
|
|
for (ByteBuffer b: buffers) {
|
|
|
|
merged.put(b);
|
|
|
|
}
|
|
|
|
merged.flip();
|
|
|
|
return merged;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ByteBuffer[] toByteBuffers(int index, int length) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
if (index + length > capacity()) {
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<ByteBuffer> buffers = new ArrayList<ByteBuffer>(slices.length);
|
|
|
|
|
|
|
|
int i = sliceId;
|
|
|
|
while (length > 0) {
|
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
|
|
|
buffers.add(s.toByteBuffer(index - adjustment, localLength));
|
|
|
|
index += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffers.toArray(new ByteBuffer[buffers.size()]);
|
|
|
|
}
|
|
|
|
|
2008-08-08 12:17:26 +02:00
|
|
|
public String toString(int index, int length, String charsetName) {
|
|
|
|
int sliceId = sliceId(index);
|
|
|
|
if (index + length <= indices[sliceId + 1]) {
|
|
|
|
return slices[sliceId].toString(
|
|
|
|
index - indices[sliceId], length, charsetName);
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] data = new byte[length];
|
|
|
|
int dataIndex = 0;
|
|
|
|
int i = sliceId;
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
ChannelBuffer s = slices[i];
|
|
|
|
int adjustment = indices[i];
|
|
|
|
int localLength = Math.min(length, s.capacity() - (index - adjustment));
|
|
|
|
s.getBytes(index - adjustment, data, dataIndex, localLength);
|
|
|
|
index += localLength;
|
|
|
|
dataIndex += localLength;
|
|
|
|
length -= localLength;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return new String(data, charsetName);
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw new UnsupportedCharsetException(charsetName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
private int sliceId(int index) {
|
|
|
|
int lastSliceId = this.lastSliceId;
|
|
|
|
if (index >= indices[lastSliceId]) {
|
|
|
|
if (index < indices[lastSliceId + 1]) {
|
|
|
|
return lastSliceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search right
|
|
|
|
for (int i = lastSliceId + 1; i < slices.length; i ++) {
|
|
|
|
if (index < indices[i + 1]) {
|
|
|
|
this.lastSliceId = i;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Search left
|
|
|
|
for (int i = lastSliceId - 1; i >= 0; i --) {
|
|
|
|
if (index >= indices[i]) {
|
|
|
|
this.lastSliceId = i;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
}
|