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    
029    /**
030     * @author The Netty Project (netty@googlegroups.com)
031     * @author Trustin Lee (trustin@gmail.com)
032     *
033     * @version $Rev$, $Date$
034     *
035     */
036    public class DuplicatedChannelBuffer extends AbstractChannelBuffer implements WrappedChannelBuffer {
037    
038        private final ChannelBuffer buffer;
039    
040        public DuplicatedChannelBuffer(ChannelBuffer buffer) {
041            if (buffer == null) {
042                throw new NullPointerException("buffer");
043            }
044            this.buffer = buffer;
045            setIndex(buffer.readerIndex(), buffer.writerIndex());
046        }
047    
048        private DuplicatedChannelBuffer(DuplicatedChannelBuffer buffer) {
049            this.buffer = buffer.buffer;
050            setIndex(buffer.readerIndex(), buffer.writerIndex());
051        }
052    
053        public ChannelBuffer unwrap() {
054            return buffer;
055        }
056    
057        public ByteOrder order() {
058            return buffer.order();
059        }
060    
061        public int capacity() {
062            return buffer.capacity();
063        }
064    
065        public byte getByte(int index) {
066            return buffer.getByte(index);
067        }
068    
069        public short getShort(int index) {
070            return buffer.getShort(index);
071        }
072    
073        public int getMedium(int index) {
074            return buffer.getMedium(index);
075        }
076    
077        public int getInt(int index) {
078            return buffer.getInt(index);
079        }
080    
081        public long getLong(int index) {
082            return buffer.getLong(index);
083        }
084    
085        public ChannelBuffer duplicate() {
086            return new DuplicatedChannelBuffer(this);
087        }
088    
089        public ChannelBuffer copy(int index, int length) {
090            return buffer.copy(index, length);
091        }
092    
093        public ChannelBuffer slice(int index, int length) {
094            return buffer.slice(index, length);
095        }
096    
097        public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
098            buffer.getBytes(index, dst, dstIndex, length);
099        }
100    
101        public void getBytes(int index, byte[] dst, int dstIndex, int length) {
102            buffer.getBytes(index, dst, dstIndex, length);
103        }
104    
105        public void getBytes(int index, ByteBuffer dst) {
106            buffer.getBytes(index, dst);
107        }
108    
109        public void setByte(int index, byte value) {
110            buffer.setByte(index, value);
111        }
112    
113        public void setShort(int index, short value) {
114            buffer.setShort(index, value);
115        }
116    
117        public void setMedium(int index, int value) {
118            buffer.setMedium(index, value);
119        }
120    
121        public void setInt(int index, int value) {
122            buffer.setInt(index, value);
123        }
124    
125        public void setLong(int index, long value) {
126            buffer.setLong(index, value);
127        }
128    
129        public void setBytes(int index, byte[] src, int srcIndex, int length) {
130            buffer.setBytes(index, src, srcIndex, length);
131        }
132    
133        public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
134            buffer.setBytes(index, src, srcIndex, length);
135        }
136    
137        public void setBytes(int index, ByteBuffer src) {
138            buffer.setBytes(index, src);
139        }
140    
141        public void getBytes(int index, OutputStream out, int length)
142                throws IOException {
143            buffer.getBytes(index, out, length);
144        }
145    
146        public int getBytes(int index, GatheringByteChannel out, int length)
147                throws IOException {
148            return buffer.getBytes(index, out, length);
149        }
150    
151        public void setBytes(int index, InputStream in, int length)
152                throws IOException {
153            buffer.setBytes(index, in, length);
154        }
155    
156        public int setBytes(int index, ScatteringByteChannel in, int length)
157                throws IOException {
158            return buffer.setBytes(index, in, length);
159        }
160    
161        public ByteBuffer toByteBuffer(int index, int length) {
162            return buffer.toByteBuffer(index, length);
163        }
164    }