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.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   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
27   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
28   *
29   * @version $Rev: 2206 $, $Date: 2010-03-03 14:35:01 +0900 (Wed, 03 Mar 2010) $
30   */
31  public class LittleEndianHeapChannelBuffer extends HeapChannelBuffer {
32  
33      /**
34       * Creates a new little-endian heap buffer with a newly allocated byte array.
35       *
36       * @param length the length of the new byte array
37       */
38      public LittleEndianHeapChannelBuffer(int length) {
39          super(length);
40      }
41  
42      /**
43       * Creates a new little-endian heap buffer with an existing byte array.
44       *
45       * @param array the byte array to wrap
46       */
47      public LittleEndianHeapChannelBuffer(byte[] array) {
48          super(array);
49      }
50  
51      private LittleEndianHeapChannelBuffer(byte[] array, int readerIndex, int writerIndex) {
52          super(array, readerIndex, writerIndex);
53      }
54  
55      public ChannelBufferFactory factory() {
56          return HeapChannelBufferFactory.getInstance(ByteOrder.LITTLE_ENDIAN);
57      }
58  
59      public ByteOrder order() {
60          return ByteOrder.LITTLE_ENDIAN;
61      }
62  
63      public short getShort(int index) {
64          return (short) (array[index] & 0xFF | array[index+1] << 8);
65      }
66  
67      public int getUnsignedMedium(int index) {
68          return (array[index  ] & 0xff) <<  0 |
69                 (array[index+1] & 0xff) <<  8 |
70                 (array[index+2] & 0xff) << 16;
71      }
72  
73      public int getInt(int index) {
74          return (array[index  ] & 0xff) <<  0 |
75                 (array[index+1] & 0xff) <<  8 |
76                 (array[index+2] & 0xff) << 16 |
77                 (array[index+3] & 0xff) << 24;
78      }
79  
80      public long getLong(int index) {
81          return ((long) array[index]   & 0xff) <<  0 |
82                 ((long) array[index+1] & 0xff) <<  8 |
83                 ((long) array[index+2] & 0xff) << 16 |
84                 ((long) array[index+3] & 0xff) << 24 |
85                 ((long) array[index+4] & 0xff) << 32 |
86                 ((long) array[index+5] & 0xff) << 40 |
87                 ((long) array[index+6] & 0xff) << 48 |
88                 ((long) array[index+7] & 0xff) << 56;
89      }
90  
91      public void setShort(int index, int value) {
92          array[index  ] = (byte) (value >>> 0);
93          array[index+1] = (byte) (value >>> 8);
94      }
95  
96      public void setMedium(int index, int   value) {
97          array[index  ] = (byte) (value >>> 0);
98          array[index+1] = (byte) (value >>> 8);
99          array[index+2] = (byte) (value >>> 16);
100     }
101 
102     public void setInt(int index, int   value) {
103         array[index  ] = (byte) (value >>> 0);
104         array[index+1] = (byte) (value >>> 8);
105         array[index+2] = (byte) (value >>> 16);
106         array[index+3] = (byte) (value >>> 24);
107     }
108 
109     public void setLong(int index, long  value) {
110         array[index  ] = (byte) (value >>> 0);
111         array[index+1] = (byte) (value >>> 8);
112         array[index+2] = (byte) (value >>> 16);
113         array[index+3] = (byte) (value >>> 24);
114         array[index+4] = (byte) (value >>> 32);
115         array[index+5] = (byte) (value >>> 40);
116         array[index+6] = (byte) (value >>> 48);
117         array[index+7] = (byte) (value >>> 56);
118     }
119 
120     public ChannelBuffer duplicate() {
121         return new LittleEndianHeapChannelBuffer(array, readerIndex(), writerIndex());
122     }
123 
124     public ChannelBuffer copy(int index, int length) {
125         if (index < 0 || length < 0 || index + length > array.length) {
126             throw new IndexOutOfBoundsException();
127         }
128 
129         byte[] copiedArray = new byte[length];
130         System.arraycopy(array, index, copiedArray, 0, length);
131         return new LittleEndianHeapChannelBuffer(copiedArray);
132     }
133 }