netty5/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java

342 lines
9.4 KiB
Java
Raw Normal View History

/*
2012-06-04 22:31:44 +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:
*
2012-06-04 22:31:44 +02:00
* http://www.apache.org/licenses/LICENSE-2.0
*
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
2009-08-28 09:15:49 +02:00
* License for the specific language governing permissions and limitations
* under the License.
*/
2011-12-09 04:38:59 +01:00
package io.netty.buffer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
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 ByteBuf#slice()} and
* {@link ByteBuf#slice(int, int)} instead of calling the constructor
2008-09-02 09:13:20 +02:00
* explicitly.
*/
public class SlicedByteBuf extends AbstractWrappedByteBuf {
private final Unsafe unsafe = new SlicedUnsafe();
private final ByteBuf buffer;
private final int adjustment;
private final int length;
public SlicedByteBuf(ByteBuf buffer, int index, int length) {
super(buffer.order(), length);
if (index < 0 || index > buffer.capacity()) {
throw new IndexOutOfBoundsException("Invalid index of " + index
+ ", maximum is " + buffer.capacity());
}
if (index + length > buffer.capacity()) {
throw new IndexOutOfBoundsException("Invalid combined index of "
+ (index + length) + ", maximum is " + buffer.capacity());
}
if (buffer instanceof SlicedByteBuf) {
this.buffer = ((SlicedByteBuf) buffer).buffer;
adjustment = ((SlicedByteBuf) buffer).adjustment + index;
} else if (buffer instanceof DuplicatedByteBuf) {
this.buffer = ((DuplicatedByteBuf) buffer).buffer;
adjustment = index;
} else {
this.buffer = buffer;
adjustment = index;
}
this.length = length;
writerIndex(length);
buffer.unsafe().acquire();
}
@Override
public ByteBuf unwrap() {
return buffer;
}
@Override
public boolean isDirect() {
return buffer.isDirect();
}
@Override
public int capacity() {
return length;
}
@Override
public WrappedByteBuf capacity(int newCapacity) {
throw new UnsupportedOperationException("sliced buffer");
}
@Override
public boolean hasArray() {
return buffer.hasArray();
}
@Override
public byte[] array() {
return buffer.array();
}
@Override
public int arrayOffset() {
return buffer.arrayOffset() + adjustment;
}
@Override
public byte getByte(int index) {
checkIndex(index);
return buffer.getByte(index + adjustment);
}
@Override
public short getShort(int index) {
checkIndex(index, 2);
return buffer.getShort(index + adjustment);
}
@Override
2008-08-10 02:52:59 +02:00
public int getUnsignedMedium(int index) {
checkIndex(index, 3);
2008-08-10 02:52:59 +02:00
return buffer.getUnsignedMedium(index + adjustment);
}
@Override
public int getInt(int index) {
checkIndex(index, 4);
return buffer.getInt(index + adjustment);
}
@Override
public long getLong(int index) {
checkIndex(index, 8);
return buffer.getLong(index + adjustment);
}
@Override
public ByteBuf duplicate() {
ByteBuf duplicate = new SlicedByteBuf(buffer, adjustment, length);
duplicate.setIndex(readerIndex(), writerIndex());
return duplicate;
}
@Override
public ByteBuf copy(int index, int length) {
2008-09-04 13:53:44 +02:00
checkIndex(index, length);
return buffer.copy(index + adjustment, length);
}
@Override
public ByteBuf slice(int index, int length) {
checkIndex(index, length);
if (length == 0) {
return Unpooled.EMPTY_BUFFER;
}
return new SlicedByteBuf(buffer, index + adjustment, length);
}
@Override
public WrappedByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
checkIndex(index, length);
buffer.getBytes(index + adjustment, dst, dstIndex, length);
return this;
}
@Override
public WrappedByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
checkIndex(index, length);
buffer.getBytes(index + adjustment, dst, dstIndex, length);
return this;
}
@Override
public WrappedByteBuf getBytes(int index, ByteBuffer dst) {
checkIndex(index, dst.remaining());
buffer.getBytes(index + adjustment, dst);
return this;
}
@Override
public WrappedByteBuf setByte(int index, int value) {
checkIndex(index);
buffer.setByte(index + adjustment, value);
return this;
}
@Override
public WrappedByteBuf setShort(int index, int value) {
checkIndex(index, 2);
buffer.setShort(index + adjustment, value);
return this;
}
@Override
public WrappedByteBuf setMedium(int index, int value) {
checkIndex(index, 3);
buffer.setMedium(index + adjustment, value);
return this;
}
@Override
public WrappedByteBuf setInt(int index, int value) {
checkIndex(index, 4);
buffer.setInt(index + adjustment, value);
return this;
}
@Override
public WrappedByteBuf setLong(int index, long value) {
checkIndex(index, 8);
buffer.setLong(index + adjustment, value);
return this;
}
@Override
public WrappedByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
checkIndex(index, length);
buffer.setBytes(index + adjustment, src, srcIndex, length);
return this;
}
@Override
public WrappedByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
checkIndex(index, length);
buffer.setBytes(index + adjustment, src, srcIndex, length);
return this;
}
@Override
public WrappedByteBuf setBytes(int index, ByteBuffer src) {
checkIndex(index, src.remaining());
buffer.setBytes(index + adjustment, src);
return this;
}
@Override
public WrappedByteBuf getBytes(int index, OutputStream out, int length)
throws IOException {
checkIndex(index, length);
buffer.getBytes(index + adjustment, out, length);
return this;
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length)
throws IOException {
checkIndex(index, length);
return buffer.getBytes(index + adjustment, out, length);
}
@Override
public int setBytes(int index, InputStream in, int length)
throws IOException {
checkIndex(index, length);
return buffer.setBytes(index + adjustment, in, length);
}
@Override
public int setBytes(int index, ScatteringByteChannel in, int length)
throws IOException {
checkIndex(index, length);
return buffer.setBytes(index + adjustment, in, length);
}
@Override
public boolean hasNioBuffer() {
return buffer.hasNioBuffer();
}
@Override
public ByteBuffer nioBuffer(int index, int length) {
checkIndex(index, length);
return buffer.nioBuffer(index + adjustment, length);
}
@Override
public boolean hasNioBuffers() {
return buffer.hasNioBuffers();
}
@Override
public ByteBuffer[] nioBuffers(int index, int length) {
checkIndex(index, length);
return buffer.nioBuffers(index, length);
}
private void checkIndex(int index) {
if (index < 0 || index >= capacity()) {
throw new IndexOutOfBoundsException("Invalid index: " + index
+ ", maximum is " + capacity());
}
}
private void checkIndex(int startIndex, int length) {
if (length < 0) {
throw new IllegalArgumentException(
"length is negative: " + length);
}
if (startIndex < 0) {
throw new IndexOutOfBoundsException("startIndex cannot be negative");
}
if (startIndex + length > capacity()) {
throw new IndexOutOfBoundsException("Index too big - Bytes needed: "
+ (startIndex + length) + ", maximum is " + capacity());
}
}
@Override
public Unsafe unsafe() {
return unsafe;
}
private final class SlicedUnsafe implements Unsafe {
@Override
public ByteBuffer nioBuffer() {
return buffer.nioBuffer(adjustment, length);
}
@Override
public ByteBuffer[] nioBuffers() {
return buffer.nioBuffers(adjustment, length);
}
@Override
public ByteBuf newBuffer(int initialCapacity) {
return buffer.unsafe().newBuffer(initialCapacity);
}
@Override
public void discardSomeReadBytes() {
throw new UnsupportedOperationException();
}
@Override
public void acquire() {
buffer.unsafe().acquire();
}
@Override
public void release() {
buffer.unsafe().release();
}
}
}