View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * License.  You may obtain a copy of the License at:
    7    *
    8    *    http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.buffer;
    17
    18  import java.io.IOException;
    19  import java.io.InputStream;
    20  import java.io.OutputStream;
    21  import java.nio.ByteBuffer;
    22  import java.nio.ByteOrder;
    23  import java.nio.channels.GatheringByteChannel;
    24  import java.nio.channels.ScatteringByteChannel;
    25
    26
    27  /**
    28   * A derived buffer which simply forwards all data access requests to its
    29   * parent.  It is recommended to use {@link ChannelBuffer#duplicate()} instead
    30   * of calling the constructor explicitly.
    31   *
    32   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    33   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    34   *
    35   * @version $Rev: 2206 $, $Date: 2010-03-03 14:35:01 +0900 (Wed, 03 Mar 2010) $
    36   *
    37   */
    38  public class DuplicatedChannelBuffer extends AbstractChannelBuffer implements WrappedChannelBuffer {
    39
    40      private final ChannelBuffer buffer;
    41
    42      public DuplicatedChannelBuffer(ChannelBuffer buffer) {
    43          if (buffer == null) {
    44              throw new NullPointerException("buffer");
    45          }
    46          this.buffer = buffer;
    47          setIndex(buffer.readerIndex(), buffer.writerIndex());
    48      }
    49
    50      private DuplicatedChannelBuffer(DuplicatedChannelBuffer buffer) {
    51          this.buffer = buffer.buffer;
    52          setIndex(buffer.readerIndex(), buffer.writerIndex());
    53      }
    54
    55      public ChannelBuffer unwrap() {
    56          return buffer;
    57      }
    58
    59      public ChannelBufferFactory factory() {
    60          return buffer.factory();
    61      }
    62
    63      public ByteOrder order() {
    64          return buffer.order();
    65      }
    66
    67      public boolean isDirect() {
    68          return buffer.isDirect();
    69      }
    70
    71      public int capacity() {
    72          return buffer.capacity();
    73      }
    74
    75      public boolean hasArray() {
    76          return buffer.hasArray();
    77      }
    78
    79      public byte[] array() {
    80          return buffer.array();
    81      }
    82
    83      public int arrayOffset() {
    84          return buffer.arrayOffset();
    85      }
    86
    87      public byte getByte(int index) {
    88          return buffer.getByte(index);
    89      }
    90
    91      public short getShort(int index) {
    92          return buffer.getShort(index);
    93      }
    94
    95      public int getUnsignedMedium(int index) {
    96          return buffer.getUnsignedMedium(index);
    97      }
    98
    99      public int getInt(int index) {
    100         return buffer.getInt(index);
    101     }
    102
    103     public long getLong(int index) {
    104         return buffer.getLong(index);
    105     }
    106
    107     public ChannelBuffer duplicate() {
    108         return new DuplicatedChannelBuffer(this);
    109     }
    110
    111     public ChannelBuffer copy(int index, int length) {
    112         return buffer.copy(index, length);
    113     }
    114
    115     public ChannelBuffer slice(int index, int length) {
    116         return buffer.slice(index, 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, byte[] dst, int dstIndex, int length) {
    124         buffer.getBytes(index, dst, dstIndex, length);
    125     }
    126
    127     public void getBytes(int index, ByteBuffer dst) {
    128         buffer.getBytes(index, dst);
    129     }
    130
    131     public void setByte(int index, int value) {
    132         buffer.setByte(index, value);
    133     }
    134
    135     public void setShort(int index, int value) {
    136         buffer.setShort(index, value);
    137     }
    138
    139     public void setMedium(int index, int value) {
    140         buffer.setMedium(index, value);
    141     }
    142
    143     public void setInt(int index, int value) {
    144         buffer.setInt(index, value);
    145     }
    146
    147     public void setLong(int index, long value) {
    148         buffer.setLong(index, value);
    149     }
    150
    151     public void setBytes(int index, byte[] src, int srcIndex, int length) {
    152         buffer.setBytes(index, src, srcIndex, length);
    153     }
    154
    155     public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
    156         buffer.setBytes(index, src, srcIndex, length);
    157     }
    158
    159     public void setBytes(int index, ByteBuffer src) {
    160         buffer.setBytes(index, src);
    161     }
    162
    163     public void getBytes(int index, OutputStream out, int length)
    164             throws IOException {
    165         buffer.getBytes(index, out, length);
    166     }
    167
    168     public int getBytes(int index, GatheringByteChannel out, int length)
    169             throws IOException {
    170         return buffer.getBytes(index, out, length);
    171     }
    172
    173     public int setBytes(int index, InputStream in, int length)
    174             throws IOException {
    175         return buffer.setBytes(index, in, length);
    176     }
    177
    178     public int setBytes(int index, ScatteringByteChannel in, int length)
    179             throws IOException {
    180         return buffer.setBytes(index, in, length);
    181     }
    182
    183     public ByteBuffer toByteBuffer(int index, int length) {
    184         return buffer.toByteBuffer(index, length);
    185     }
    186 }