001 /*
002 * Copyright (C) 2008 Trustin Heuiseung Lee
003 *
004 * This library is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License as published by the Free Software Foundation; either
007 * version 2.1 of the License, or (at your option) any later version.
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this library; if not, write to the Free Software
016 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA
017 */
018 package net.gleamynode.netty.buffer;
019
020 import java.io.IOException;
021 import java.io.InputStream;
022 import java.io.OutputStream;
023 import java.nio.ByteBuffer;
024 import java.nio.ByteOrder;
025 import java.nio.channels.GatheringByteChannel;
026 import java.nio.channels.ScatteringByteChannel;
027
028 public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements WrappedChannelBuffer {
029
030 private final ChannelBuffer buffer;
031
032 public ReadOnlyChannelBuffer(ChannelBuffer buffer) {
033 if (buffer == null) {
034 throw new NullPointerException("buffer");
035 }
036 this.buffer = buffer;
037 setIndex(buffer.readerIndex(), buffer.writerIndex());
038 }
039
040 private ReadOnlyChannelBuffer(ReadOnlyChannelBuffer buffer) {
041 this.buffer = buffer.buffer;
042 setIndex(buffer.readerIndex(), buffer.writerIndex());
043 }
044
045 public ChannelBuffer unwrap() {
046 return buffer;
047 }
048
049 public ByteOrder order() {
050 return buffer.order();
051 }
052
053 @Override
054 public void discardReadBytes() {
055 rejectModification();
056 }
057
058 public void setByte(int index, byte value) {
059 rejectModification();
060 }
061
062 public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
063 rejectModification();
064 }
065
066 public void setBytes(int index, byte[] src, int srcIndex, int length) {
067 rejectModification();
068 }
069
070 public void setBytes(int index, ByteBuffer src) {
071 rejectModification();
072 }
073
074 public void setShort(int index, short value) {
075 rejectModification();
076 }
077
078 public void setMedium(int index, int value) {
079 rejectModification();
080 }
081
082 public void setInt(int index, int value) {
083 rejectModification();
084 }
085
086 public void setLong(int index, long value) {
087 rejectModification();
088 }
089
090 public void setBytes(int index, InputStream in, int length)
091 throws IOException {
092 rejectModification();
093 }
094
095 public int setBytes(int index, ScatteringByteChannel in, int length)
096 throws IOException {
097 rejectModification();
098 return 0;
099 }
100
101 protected void rejectModification() {
102 throw new UnsupportedOperationException("read-only");
103 }
104
105 public int getBytes(int index, GatheringByteChannel out, int length)
106 throws IOException {
107 return buffer.getBytes(index, out, length);
108 }
109
110 public void getBytes(int index, OutputStream out, int length)
111 throws IOException {
112 buffer.getBytes(index, out, length);
113 }
114
115 public void getBytes(int index, byte[] dst, int dstIndex, int length) {
116 buffer.getBytes(index, dst, dstIndex, length);
117 }
118
119 public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
120 buffer.getBytes(index, dst, dstIndex, length);
121 }
122
123 public void getBytes(int index, ByteBuffer dst) {
124 buffer.getBytes(index, dst);
125 }
126
127 public ChannelBuffer duplicate() {
128 return new ReadOnlyChannelBuffer(this);
129 }
130
131 public ChannelBuffer copy(int index, int length) {
132 return new ReadOnlyChannelBuffer(buffer.copy(index, length));
133 }
134
135 public ChannelBuffer slice(int index, int length) {
136 return new ReadOnlyChannelBuffer(buffer.slice(index, length));
137 }
138
139 public byte getByte(int index) {
140 return buffer.getByte(index);
141 }
142
143 public short getShort(int index) {
144 return buffer.getShort(index);
145 }
146
147 public int getMedium(int index) {
148 return buffer.getMedium(index);
149 }
150
151 public int getInt(int index) {
152 return buffer.getInt(index);
153 }
154
155 public long getLong(int index) {
156 return buffer.getLong(index);
157 }
158
159 public ByteBuffer toByteBuffer(int index, int length) {
160 return buffer.toByteBuffer(index, length).asReadOnlyBuffer();
161 }
162
163 public int capacity() {
164 return buffer.capacity();
165 }
166 }