2008-08-08 02:37:18 +02:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* 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:
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2012-06-04 22:31:44 +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
|
2011-12-09 06:18:34 +01:00
|
|
|
* 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.
|
2008-08-08 02:37:18 +02:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.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;
|
2009-02-13 11:21:28 +01:00
|
|
|
import java.nio.ReadOnlyBufferException;
|
2008-08-08 02:37:18 +02:00
|
|
|
import java.nio.channels.GatheringByteChannel;
|
|
|
|
import java.nio.channels.ScatteringByteChannel;
|
|
|
|
|
2008-08-10 07:52:36 +02:00
|
|
|
/**
|
2008-09-02 09:13:20 +02:00
|
|
|
* A derived buffer which forbids any write requests to its parent. It is
|
|
|
|
* recommended to use {@link ChannelBuffers#unmodifiableBuffer(ChannelBuffer)}
|
|
|
|
* instead of calling the constructor explicitly.
|
2008-08-10 07:52:36 +02:00
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements WrappedChannelBuffer {
|
|
|
|
|
|
|
|
private final ChannelBuffer buffer;
|
|
|
|
|
|
|
|
public ReadOnlyChannelBuffer(ChannelBuffer buffer) {
|
|
|
|
if (buffer == null) {
|
|
|
|
throw new NullPointerException("buffer");
|
|
|
|
}
|
|
|
|
this.buffer = buffer;
|
|
|
|
setIndex(buffer.readerIndex(), buffer.writerIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
private ReadOnlyChannelBuffer(ReadOnlyChannelBuffer buffer) {
|
|
|
|
this.buffer = buffer.buffer;
|
|
|
|
setIndex(buffer.readerIndex(), buffer.writerIndex());
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public ChannelBuffer unwrap() {
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-12-08 09:20:34 +01:00
|
|
|
public ChannelBufferFactory factory() {
|
|
|
|
return buffer.factory();
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public ByteOrder order() {
|
|
|
|
return buffer.order();
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2010-01-27 06:07:32 +01:00
|
|
|
public boolean isDirect() {
|
|
|
|
return buffer.isDirect();
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-11-25 08:08:52 +01:00
|
|
|
public boolean hasArray() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-11-25 08:08:52 +01:00
|
|
|
public byte[] array() {
|
|
|
|
throw new ReadOnlyBufferException();
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-11-25 08:08:52 +01:00
|
|
|
public int arrayOffset() {
|
|
|
|
throw new ReadOnlyBufferException();
|
|
|
|
}
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
@Override
|
|
|
|
public void discardReadBytes() {
|
2009-02-13 11:21:28 +01:00
|
|
|
throw new ReadOnlyBufferException();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2010-03-03 06:35:01 +01:00
|
|
|
public void setByte(int index, int value) {
|
2009-02-13 11:21:28 +01:00
|
|
|
throw new ReadOnlyBufferException();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
|
2009-02-13 11:21:28 +01:00
|
|
|
throw new ReadOnlyBufferException();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void setBytes(int index, byte[] src, int srcIndex, int length) {
|
2009-02-13 11:21:28 +01:00
|
|
|
throw new ReadOnlyBufferException();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void setBytes(int index, ByteBuffer src) {
|
2009-02-13 11:21:28 +01:00
|
|
|
throw new ReadOnlyBufferException();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2010-03-03 06:35:01 +01:00
|
|
|
public void setShort(int index, int value) {
|
2009-02-13 11:21:28 +01:00
|
|
|
throw new ReadOnlyBufferException();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void setMedium(int index, int value) {
|
2009-02-13 11:21:28 +01:00
|
|
|
throw new ReadOnlyBufferException();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void setInt(int index, int value) {
|
2009-02-13 11:21:28 +01:00
|
|
|
throw new ReadOnlyBufferException();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void setLong(int index, long value) {
|
2009-02-13 11:21:28 +01:00
|
|
|
throw new ReadOnlyBufferException();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
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 {
|
2009-02-13 11:21:28 +01:00
|
|
|
throw new ReadOnlyBufferException();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public int setBytes(int index, ScatteringByteChannel in, int length)
|
|
|
|
throws IOException {
|
2009-02-13 11:21:28 +01:00
|
|
|
throw new ReadOnlyBufferException();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public int getBytes(int index, GatheringByteChannel out, int length)
|
|
|
|
throws IOException {
|
|
|
|
return buffer.getBytes(index, out, length);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void getBytes(int index, OutputStream out, int length)
|
|
|
|
throws IOException {
|
|
|
|
buffer.getBytes(index, out, length);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
|
|
|
|
buffer.getBytes(index, dst, dstIndex, length);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
|
|
|
|
buffer.getBytes(index, dst, dstIndex, length);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public void getBytes(int index, ByteBuffer dst) {
|
|
|
|
buffer.getBytes(index, dst);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public ChannelBuffer duplicate() {
|
|
|
|
return new ReadOnlyChannelBuffer(this);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public ChannelBuffer copy(int index, int length) {
|
2008-09-04 14:37:37 +02:00
|
|
|
return buffer.copy(index, length);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public ChannelBuffer slice(int index, int length) {
|
|
|
|
return new ReadOnlyChannelBuffer(buffer.slice(index, length));
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public byte getByte(int index) {
|
|
|
|
return buffer.getByte(index);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public short getShort(int index) {
|
|
|
|
return buffer.getShort(index);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-10 02:52:59 +02:00
|
|
|
public int getUnsignedMedium(int index) {
|
|
|
|
return buffer.getUnsignedMedium(index);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public int getInt(int index) {
|
|
|
|
return buffer.getInt(index);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public long getLong(int index) {
|
|
|
|
return buffer.getLong(index);
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2012-06-02 10:30:55 +02:00
|
|
|
public boolean hasNioBuffer() {
|
|
|
|
return buffer.hasNioBuffer();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2008-09-04 14:37:37 +02:00
|
|
|
@Override
|
2012-06-02 10:30:55 +02:00
|
|
|
public ByteBuffer nioBuffer(int index, int length) {
|
|
|
|
return buffer.nioBuffer(index, length).asReadOnlyBuffer();
|
2008-09-04 14:37:37 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-08-08 02:37:18 +02:00
|
|
|
public int capacity() {
|
|
|
|
return buffer.capacity();
|
|
|
|
}
|
|
|
|
}
|