2008-08-08 02:37:18 +02:00
|
|
|
/*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Copyright 2009 Red Hat, Inc.
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Red Hat 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:
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-08-08 03:27:24 +02:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* 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.
|
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;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
import java.nio.channels.GatheringByteChannel;
|
|
|
|
import java.nio.channels.ScatteringByteChannel;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-09-02 09:13:20 +02:00
|
|
|
* A derived buffer which exposes its parent's sub-region only. It is
|
|
|
|
* recommended to use {@link ChannelBuffer#slice()} and
|
|
|
|
* {@link ChannelBuffer#slice(int, int)} instead of calling the constructor
|
|
|
|
* explicitly.
|
2008-08-10 07:52:36 +02:00
|
|
|
*
|
2010-01-26 10:04:19 +01:00
|
|
|
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
|
|
|
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
|
|
|
* @version $Rev$, $Date$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class SlicedChannelBuffer extends AbstractChannelBuffer implements WrappedChannelBuffer {
|
|
|
|
|
|
|
|
private final ChannelBuffer buffer;
|
|
|
|
private final int adjustment;
|
|
|
|
private final int length;
|
|
|
|
|
|
|
|
public SlicedChannelBuffer(ChannelBuffer buffer, int index, int length) {
|
|
|
|
if (index < 0 || index > buffer.capacity()) {
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index + length > buffer.capacity()) {
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.buffer = buffer;
|
|
|
|
adjustment = index;
|
|
|
|
this.length = length;
|
|
|
|
writerIndex(length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ChannelBuffer unwrap() {
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2008-12-08 09:20:34 +01:00
|
|
|
public ChannelBufferFactory factory() {
|
|
|
|
return buffer.factory();
|
|
|
|
}
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
public ByteOrder order() {
|
|
|
|
return buffer.order();
|
|
|
|
}
|
|
|
|
|
2010-01-27 06:07:32 +01:00
|
|
|
public boolean isDirect() {
|
|
|
|
return buffer.isDirect();
|
|
|
|
}
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
public int capacity() {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2009-11-25 08:08:52 +01:00
|
|
|
public boolean hasArray() {
|
|
|
|
return buffer.hasArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] array() {
|
|
|
|
return buffer.array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int arrayOffset() {
|
|
|
|
return buffer.arrayOffset() + adjustment;
|
|
|
|
}
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
public byte getByte(int index) {
|
|
|
|
checkIndex(index);
|
|
|
|
return buffer.getByte(index + adjustment);
|
|
|
|
}
|
|
|
|
|
|
|
|
public short getShort(int index) {
|
|
|
|
checkIndex(index, 2);
|
|
|
|
return buffer.getShort(index + adjustment);
|
|
|
|
}
|
|
|
|
|
2008-08-10 02:52:59 +02:00
|
|
|
public int getUnsignedMedium(int index) {
|
2008-08-08 02:37:18 +02:00
|
|
|
checkIndex(index, 3);
|
2008-08-10 02:52:59 +02:00
|
|
|
return buffer.getUnsignedMedium(index + adjustment);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getInt(int index) {
|
|
|
|
checkIndex(index, 4);
|
|
|
|
return buffer.getInt(index + adjustment);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getLong(int index) {
|
|
|
|
checkIndex(index, 8);
|
|
|
|
return buffer.getLong(index + adjustment);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ChannelBuffer duplicate() {
|
2008-08-26 13:08:55 +02:00
|
|
|
ChannelBuffer duplicate = new SlicedChannelBuffer(buffer, adjustment, length);
|
|
|
|
duplicate.setIndex(readerIndex(), writerIndex());
|
|
|
|
return duplicate;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ChannelBuffer copy(int index, int length) {
|
2008-09-04 13:53:44 +02:00
|
|
|
checkIndex(index, length);
|
2008-08-08 02:37:18 +02:00
|
|
|
return buffer.copy(index + adjustment, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ChannelBuffer slice(int index, int length) {
|
|
|
|
checkIndex(index, length);
|
2008-08-26 17:37:08 +02:00
|
|
|
if (length == 0) {
|
|
|
|
return ChannelBuffers.EMPTY_BUFFER;
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
return new SlicedChannelBuffer(buffer, index + adjustment, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
|
|
|
|
checkIndex(index, length);
|
|
|
|
buffer.getBytes(index + adjustment, dst, dstIndex, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
|
|
|
|
checkIndex(index, length);
|
|
|
|
buffer.getBytes(index + adjustment, dst, dstIndex, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getBytes(int index, ByteBuffer dst) {
|
|
|
|
checkIndex(index, dst.remaining());
|
|
|
|
buffer.getBytes(index + adjustment, dst);
|
|
|
|
}
|
|
|
|
|
2010-03-03 06:35:01 +01:00
|
|
|
public void setByte(int index, int value) {
|
2008-08-08 02:37:18 +02:00
|
|
|
checkIndex(index);
|
|
|
|
buffer.setByte(index + adjustment, value);
|
|
|
|
}
|
|
|
|
|
2010-03-03 06:35:01 +01:00
|
|
|
public void setShort(int index, int value) {
|
2008-08-08 02:37:18 +02:00
|
|
|
checkIndex(index, 2);
|
|
|
|
buffer.setShort(index + adjustment, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMedium(int index, int value) {
|
|
|
|
checkIndex(index, 3);
|
|
|
|
buffer.setMedium(index + adjustment, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setInt(int index, int value) {
|
|
|
|
checkIndex(index, 4);
|
|
|
|
buffer.setInt(index + adjustment, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLong(int index, long value) {
|
|
|
|
checkIndex(index, 8);
|
|
|
|
buffer.setLong(index + adjustment, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBytes(int index, byte[] src, int srcIndex, int length) {
|
|
|
|
checkIndex(index, length);
|
|
|
|
buffer.setBytes(index + adjustment, src, srcIndex, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
|
|
|
|
checkIndex(index, length);
|
|
|
|
buffer.setBytes(index + adjustment, src, srcIndex, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBytes(int index, ByteBuffer src) {
|
|
|
|
checkIndex(index, src.remaining());
|
|
|
|
buffer.setBytes(index + adjustment, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getBytes(int index, OutputStream out, int length)
|
|
|
|
throws IOException {
|
|
|
|
checkIndex(index, length);
|
|
|
|
buffer.getBytes(index + adjustment, out, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getBytes(int index, GatheringByteChannel out, int length)
|
|
|
|
throws IOException {
|
|
|
|
checkIndex(index, length);
|
|
|
|
return buffer.getBytes(index + adjustment, out, length);
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
checkIndex(index, length);
|
2008-08-20 05:09:23 +02:00
|
|
|
return buffer.setBytes(index + adjustment, in, length);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int setBytes(int index, ScatteringByteChannel in, int length)
|
|
|
|
throws IOException {
|
|
|
|
checkIndex(index, length);
|
|
|
|
return buffer.setBytes(index + adjustment, in, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ByteBuffer toByteBuffer(int index, int length) {
|
|
|
|
checkIndex(index, length);
|
|
|
|
return buffer.toByteBuffer(index + adjustment, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkIndex(int index) {
|
|
|
|
if (index < 0 || index >= capacity()) {
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkIndex(int startIndex, int length) {
|
|
|
|
if (length < 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"length is negative: " + length);
|
|
|
|
}
|
|
|
|
if (startIndex < 0) {
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
if (startIndex + length > capacity()) {
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|