Source code: org/apache/derby/impl/drda/SignedBinary.java
1 /*
2
3 Derby - Class org.apache.derby.iapi.services.cache.SignedBinary
4
5 Copyright 2001, 2004 The Apache Software Foundation or its licensors, as applicable.
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18
19 */
20 package org.apache.derby.impl.drda;
21
22 /**
23 * Converters from signed binary bytes to Java <code>short</code>, <code>int</code>, or <code>long</code>.
24 */
25 public class SignedBinary
26 {
27 // Hide the default constructor, this is a static class.
28 private SignedBinary () {}
29
30 /**
31 * AS/400, Unix, System/390 byte-order for signed binary representations.
32 */
33 public final static int BIG_ENDIAN = 1;
34
35 /**
36 * Intel 80/86 reversed byte-order for signed binary representations.
37 */
38 public final static int LITTLE_ENDIAN = 2;
39
40 /**
41 * Build a Java short from a 2-byte signed binary representation.
42 * <p>
43 * Depending on machine type, byte orders are
44 * {@link #BIG_ENDIAN BIG_ENDIAN} for signed binary integers, and
45 * {@link #LITTLE_ENDIAN LITTLE_ENDIAN} for pc8087 signed binary integers.
46 *
47 * @exception IllegalArgumentException if the specified byte order is not recognized.
48 */
49 public static short getShort (byte[] buffer, int offset, int byteOrder)
50 {
51 switch (byteOrder) {
52 case BIG_ENDIAN:
53 return bigEndianBytesToShort (buffer, offset);
54 case LITTLE_ENDIAN:
55 return littleEndianBytesToShort (buffer, offset);
56 default:
57 throw new java.lang.IllegalArgumentException();
58 }
59 }
60
61 /**
62 * Build a Java int from a 4-byte signed binary representation.
63 * <p>
64 * Depending on machine type, byte orders are
65 * {@link #BIG_ENDIAN BIG_ENDIAN} for signed binary integers, and
66 * {@link #LITTLE_ENDIAN LITTLE_ENDIAN} for pc8087 signed binary integers.
67 *
68 * @exception IllegalArgumentException if the specified byte order is not recognized.
69 */
70 public static int getInt (byte[] buffer, int offset, int byteOrder)
71 {
72 switch (byteOrder) {
73 case BIG_ENDIAN:
74 return bigEndianBytesToInt (buffer, offset);
75 case LITTLE_ENDIAN:
76 return littleEndianBytesToInt (buffer, offset);
77 default:
78 throw new java.lang.IllegalArgumentException();
79 }
80 }
81
82 /**
83 * Build a Java long from an 8-byte signed binary representation.
84 * <p>
85 * Depending on machine type, byte orders are
86 * {@link #BIG_ENDIAN BIG_ENDIAN} for signed binary integers, and
87 * {@link #LITTLE_ENDIAN LITTLE_ENDIAN} for pc8087 signed binary integers.
88 * <p>
89 *
90 * @exception IllegalArgumentException if the specified byte order is not recognized.
91 */
92 public static long getLong (byte[] buffer, int offset, int byteOrder)
93 {
94 switch (byteOrder) {
95 case BIG_ENDIAN:
96 return bigEndianBytesToLong (buffer, offset);
97 case LITTLE_ENDIAN:
98 return littleEndianBytesToLong (buffer, offset);
99 default:
100 throw new java.lang.IllegalArgumentException();
101 }
102 }
103
104 /**
105 * Build a Java short from a 2-byte big endian signed binary representation.
106 */
107 public static short bigEndianBytesToShort (byte[] buffer, int offset)
108 {
109 return (short) (((buffer[offset+0] & 0xff) << 8) +
110 ((buffer[offset+1] & 0xff) << 0));
111 }
112
113 /**
114 * Build a Java short from a 2-byte little endian signed binary representation.
115 */
116 public static short littleEndianBytesToShort (byte[] buffer, int offset)
117 {
118 return (short) (((buffer[offset+0] & 0xff) << 0) +
119 ((buffer[offset+1] & 0xff) << 8));
120 }
121
122 /**
123 * Build a Java int from a 4-byte big endian signed binary representation.
124 */
125 public static int bigEndianBytesToInt (byte[] buffer, int offset)
126 {
127 return (int) (((buffer[offset+0] & 0xff) << 24) +
128 ((buffer[offset+1] & 0xff) << 16) +
129 ((buffer[offset+2] & 0xff) << 8) +
130 ((buffer[offset+3] & 0xff) << 0));
131 }
132
133 /**
134 * Build a Java int from a 4-byte little endian signed binary representation.
135 */
136 public static int littleEndianBytesToInt (byte[] buffer, int offset)
137 {
138 return (int) (((buffer[offset+0] & 0xff) << 0) +
139 ((buffer[offset+1] & 0xff) << 8) +
140 ((buffer[offset+2] & 0xff) << 16) +
141 ((buffer[offset+3] & 0xff) << 24));
142 }
143
144 /**
145 * Build a Java long from an 8-byte big endian signed binary representation.
146 */
147 public static long bigEndianBytesToLong (byte[] buffer, int offset)
148 {
149 return (long) (((buffer[offset+0] & 0xffL) << 56) +
150 ((buffer[offset+1] & 0xffL) << 48) +
151 ((buffer[offset+2] & 0xffL) << 40) +
152 ((buffer[offset+3] & 0xffL) << 32) +
153 ((buffer[offset+4] & 0xffL) << 24) +
154 ((buffer[offset+5] & 0xffL) << 16) +
155 ((buffer[offset+6] & 0xffL) << 8) +
156 ((buffer[offset+7] & 0xffL) << 0));
157 }
158
159 /**
160 * Build a Java long from an 8-byte little endian signed binary representation.
161 */
162 public static long littleEndianBytesToLong (byte[] buffer, int offset)
163 {
164 return (long) (((buffer[offset+0] & 0xffL) << 0) +
165 ((buffer[offset+1] & 0xffL) << 8) +
166 ((buffer[offset+2] & 0xffL) << 16) +
167 ((buffer[offset+3] & 0xffL) << 24) +
168 ((buffer[offset+4] & 0xffL) << 32) +
169 ((buffer[offset+5] & 0xffL) << 40) +
170 ((buffer[offset+6] & 0xffL) << 48) +
171 ((buffer[offset+7] & 0xffL) << 56));
172 }
173 }