View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the 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.nio.ByteOrder;
    19
    20
    21  /**
    22   * A little-endian Java heap buffer.  It is recommended to use {@link ChannelBuffers#buffer(ByteOrder, int)}
    23   * and {@link ChannelBuffers#wrappedBuffer(ByteOrder, byte[])} instead of
    24   * calling the constructor explicitly.
    25   */
    26  public class LittleEndianHeapChannelBuffer extends HeapChannelBuffer {
    27
    28      /**
    29       * Creates a new little-endian heap buffer with a newly allocated byte array.
    30       *
    31       * @param length the length of the new byte array
    32       */
    33      public LittleEndianHeapChannelBuffer(int length) {
    34          super(length);
    35      }
    36
    37      /**
    38       * Creates a new little-endian heap buffer with an existing byte array.
    39       *
    40       * @param array the byte array to wrap
    41       */
    42      public LittleEndianHeapChannelBuffer(byte[] array) {
    43          super(array);
    44      }
    45
    46      private LittleEndianHeapChannelBuffer(byte[] array, int readerIndex, int writerIndex) {
    47          super(array, readerIndex, writerIndex);
    48      }
    49
    50      public ChannelBufferFactory factory() {
    51          return HeapChannelBufferFactory.getInstance(ByteOrder.LITTLE_ENDIAN);
    52      }
    53
    54      public ByteOrder order() {
    55          return ByteOrder.LITTLE_ENDIAN;
    56      }
    57
    58      public short getShort(int index) {
    59          return (short) (array[index] & 0xFF | array[index + 1] << 8);
    60      }
    61
    62      public int getUnsignedMedium(int index) {
    63          return (array[index] & 0xff) |
    64                 (array[index + 1] & 0xff) <<  8 |
    65                 (array[index + 2] & 0xff) << 16;
    66      }
    67
    68      public int getInt(int index) {
    69          return (array[index] & 0xff) |
    70                 (array[index + 1] & 0xff) <<  8 |
    71                 (array[index + 2] & 0xff) << 16 |
    72                 (array[index + 3] & 0xff) << 24;
    73      }
    74
    75      public long getLong(int index) {
    76          return ((long) array[index] & 0xff) |
    77                 ((long) array[index + 1] & 0xff) <<  8 |
    78                 ((long) array[index + 2] & 0xff) << 16 |
    79                 ((long) array[index + 3] & 0xff) << 24 |
    80                 ((long) array[index + 4] & 0xff) << 32 |
    81                 ((long) array[index + 5] & 0xff) << 40 |
    82                 ((long) array[index + 6] & 0xff) << 48 |
    83                 ((long) array[index + 7] & 0xff) << 56;
    84      }
    85
    86      public void setShort(int index, int value) {
    87          array[index]     = (byte) value;
    88          array[index + 1] = (byte) (value >>> 8);
    89      }
    90
    91      public void setMedium(int index, int   value) {
    92          array[index]     = (byte) value;
    93          array[index + 1] = (byte) (value >>> 8);
    94          array[index + 2] = (byte) (value >>> 16);
    95      }
    96
    97      public void setInt(int index, int   value) {
    98          array[index]     = (byte) value;
    99          array[index + 1] = (byte) (value >>> 8);
    100         array[index + 2] = (byte) (value >>> 16);
    101         array[index + 3] = (byte) (value >>> 24);
    102     }
    103
    104     public void setLong(int index, long  value) {
    105         array[index]     = (byte) value;
    106         array[index + 1] = (byte) (value >>> 8);
    107         array[index + 2] = (byte) (value >>> 16);
    108         array[index + 3] = (byte) (value >>> 24);
    109         array[index + 4] = (byte) (value >>> 32);
    110         array[index + 5] = (byte) (value >>> 40);
    111         array[index + 6] = (byte) (value >>> 48);
    112         array[index + 7] = (byte) (value >>> 56);
    113     }
    114
    115     public ChannelBuffer duplicate() {
    116         return new LittleEndianHeapChannelBuffer(array, readerIndex(), writerIndex());
    117     }
    118
    119     public ChannelBuffer copy(int index, int length) {
    120         if (index < 0 || length < 0 || index + length > array.length) {
    121             throw new IndexOutOfBoundsException("Too many bytes to copy - Need "
    122                     + (index + length) + ", maximum is " + array.length);
    123         }
    124
    125         byte[] copiedArray = new byte[length];
    126         System.arraycopy(array, index, copiedArray, 0, length);
    127         return new LittleEndianHeapChannelBuffer(copiedArray);
    128     }
    129 }