KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > drda > SignedBinary


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.cache.SignedBinary
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21 package org.apache.derby.impl.drda;
22
23 /**
24  * Converters from signed binary bytes to Java <code>short</code>, <code>int</code>, or <code>long</code>.
25  */

26 class SignedBinary
27 {
28   // Hide the default constructor, this is a static class.
29
private SignedBinary () {}
30
31   /**
32    * AS/400, Unix, System/390 byte-order for signed binary representations.
33    */

34   public final static int BIG_ENDIAN = 1;
35
36   /**
37    * Intel 80/86 reversed byte-order for signed binary representations.
38    */

39   public final static int LITTLE_ENDIAN = 2;
40
41   /**
42    * Build a Java short from a 2-byte signed binary representation.
43    * <p>
44    * Depending on machine type, byte orders are
45    * {@link #BIG_ENDIAN BIG_ENDIAN} for signed binary integers, and
46    * {@link #LITTLE_ENDIAN LITTLE_ENDIAN} for pc8087 signed binary integers.
47    *
48    * @exception IllegalArgumentException if the specified byte order is not recognized.
49    */

50   public static short getShort (byte[] buffer, int offset, int byteOrder)
51   {
52     switch (byteOrder) {
53     case BIG_ENDIAN:
54       return bigEndianBytesToShort (buffer, offset);
55     case LITTLE_ENDIAN:
56       return littleEndianBytesToShort (buffer, offset);
57     default:
58       throw new java.lang.IllegalArgumentException JavaDoc();
59     }
60   }
61
62   /**
63    * Build a Java int from a 4-byte signed binary representation.
64    * <p>
65    * Depending on machine type, byte orders are
66    * {@link #BIG_ENDIAN BIG_ENDIAN} for signed binary integers, and
67    * {@link #LITTLE_ENDIAN LITTLE_ENDIAN} for pc8087 signed binary integers.
68    *
69    * @exception IllegalArgumentException if the specified byte order is not recognized.
70    */

71   public static int getInt (byte[] buffer, int offset, int byteOrder)
72   {
73     switch (byteOrder) {
74     case BIG_ENDIAN:
75       return bigEndianBytesToInt (buffer, offset);
76     case LITTLE_ENDIAN:
77       return littleEndianBytesToInt (buffer, offset);
78     default:
79       throw new java.lang.IllegalArgumentException JavaDoc();
80     }
81   }
82
83   /**
84    * Build a Java long from an 8-byte signed binary representation.
85    * <p>
86    * Depending on machine type, byte orders are
87    * {@link #BIG_ENDIAN BIG_ENDIAN} for signed binary integers, and
88    * {@link #LITTLE_ENDIAN LITTLE_ENDIAN} for pc8087 signed binary integers.
89    * <p>
90    *
91    * @exception IllegalArgumentException if the specified byte order is not recognized.
92    */

93   public static long getLong (byte[] buffer, int offset, int byteOrder)
94   {
95     switch (byteOrder) {
96     case BIG_ENDIAN:
97       return bigEndianBytesToLong (buffer, offset);
98     case LITTLE_ENDIAN:
99       return littleEndianBytesToLong (buffer, offset);
100     default:
101       throw new java.lang.IllegalArgumentException JavaDoc();
102     }
103   }
104
105   /**
106    * Build a Java short from a 2-byte big endian signed binary representation.
107    */

108   public static short bigEndianBytesToShort (byte[] buffer, int offset)
109   {
110     return (short) (((buffer[offset+0] & 0xff) << 8) +
111                     ((buffer[offset+1] & 0xff) << 0));
112   }
113
114   /**
115    * Build a Java short from a 2-byte little endian signed binary representation.
116    */

117   public static short littleEndianBytesToShort (byte[] buffer, int offset)
118   {
119     return (short) (((buffer[offset+0] & 0xff) << 0) +
120                     ((buffer[offset+1] & 0xff) << 8));
121   }
122
123   /**
124    * Build a Java int from a 4-byte big endian signed binary representation.
125    */

126   public static int bigEndianBytesToInt (byte[] buffer, int offset)
127   {
128     return (int) (((buffer[offset+0] & 0xff) << 24) +
129                   ((buffer[offset+1] & 0xff) << 16) +
130                   ((buffer[offset+2] & 0xff) << 8) +
131                   ((buffer[offset+3] & 0xff) << 0));
132   }
133
134   /**
135    * Build a Java int from a 4-byte little endian signed binary representation.
136    */

137   public static int littleEndianBytesToInt (byte[] buffer, int offset)
138   {
139     return (int) (((buffer[offset+0] & 0xff) << 0) +
140                   ((buffer[offset+1] & 0xff) << 8) +
141                   ((buffer[offset+2] & 0xff) << 16) +
142                   ((buffer[offset+3] & 0xff) << 24));
143   }
144
145   /**
146    * Build a Java long from an 8-byte big endian signed binary representation.
147    */

148   public static long bigEndianBytesToLong (byte[] buffer, int offset)
149   {
150     return (long) (((buffer[offset+0] & 0xffL) << 56) +
151                    ((buffer[offset+1] & 0xffL) << 48) +
152                    ((buffer[offset+2] & 0xffL) << 40) +
153                    ((buffer[offset+3] & 0xffL) << 32) +
154                    ((buffer[offset+4] & 0xffL) << 24) +
155                    ((buffer[offset+5] & 0xffL) << 16) +
156                    ((buffer[offset+6] & 0xffL) << 8) +
157                    ((buffer[offset+7] & 0xffL) << 0));
158   }
159
160   /**
161    * Build a Java long from an 8-byte little endian signed binary representation.
162    */

163   public static long littleEndianBytesToLong (byte[] buffer, int offset)
164   {
165     return (long) (((buffer[offset+0] & 0xffL) << 0) +
166                    ((buffer[offset+1] & 0xffL) << 8) +
167                    ((buffer[offset+2] & 0xffL) << 16) +
168                    ((buffer[offset+3] & 0xffL) << 24) +
169                    ((buffer[offset+4] & 0xffL) << 32) +
170                    ((buffer[offset+5] & 0xffL) << 40) +
171                    ((buffer[offset+6] & 0xffL) << 48) +
172                    ((buffer[offset+7] & 0xffL) << 56));
173   }
174 }
175
Popular Tags