1 /*
2 * Copyright (C) The Spice Group. All rights reserved.
3 *
4 * This software is published under the terms of the Spice
5 * Software License version 1.1, a copy of which has been included
6 * with this distribution in the LICENSE.txt file.
7 */
8 package org.realityforge.salt.io;
9
10 import java.io.EOFException;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14
15 /***
16 * Utility code for dealing with different endian systems.
17 *
18 * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
19 * @version CVS $Revision: 1.3 $ $Date: 2003/06/27 03:45:57 $
20 */
21 public final class EndianUtil
22 {
23 public static final int SIZEOF_BYTE = 1;
24 public static final int SIZEOF_SHORT = 2;
25 public static final int SIZEOF_INT = 4;
26 public static final int SIZEOF_FLOAT = 4;
27 public static final int SIZEOF_LONG = 8;
28
29 public static short swapShort( final short value )
30 {
31 return (short)(
32 ( ( ( value >> 0 ) & 0xff ) << 8 ) +
33 ( ( ( value >> 8 ) & 0xff ) << 0 ) );
34 }
35
36 public static int swapInteger( final int value )
37 {
38 return
39 ( ( ( value >> 0 ) & 0xff ) << 24 ) +
40 ( ( ( value >> 8 ) & 0xff ) << 16 ) +
41 ( ( ( value >> 16 ) & 0xff ) << 8 ) +
42 ( ( ( value >> 24 ) & 0xff ) << 0 );
43 }
44
45 public static long swapLong( final long value )
46 {
47 return
48 ( ( ( value >> 0 ) & 0xff ) << 56 ) +
49 ( ( ( value >> 8 ) & 0xff ) << 48 ) +
50 ( ( ( value >> 16 ) & 0xff ) << 40 ) +
51 ( ( ( value >> 24 ) & 0xff ) << 32 ) +
52 ( ( ( value >> 32 ) & 0xff ) << 24 ) +
53 ( ( ( value >> 40 ) & 0xff ) << 16 ) +
54 ( ( ( value >> 48 ) & 0xff ) << 8 ) +
55 ( ( ( value >> 56 ) & 0xff ) << 0 );
56 }
57
58 public static float swapFloat( final float value )
59 {
60 return Float.intBitsToFloat( swapInteger( Float.floatToIntBits( value ) ) );
61 }
62
63 public static double swapDouble( final double value )
64 {
65 return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) );
66 }
67
68 public static void writeSwappedShort( final byte[] data, final int offset, final short value )
69 {
70 data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
71 data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
72 }
73
74 public static void writeSwappedUnsignedShort( final byte[] data, final int offset, final int value )
75 {
76 data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
77 data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
78 }
79
80 public static short readSwappedShort( final byte[] data, final int offset )
81 {
82 return (short)(
83 ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
84 ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
85 }
86
87 public static int readSwappedUnsignedShort( final byte[] data, final int offset )
88 {
89 return (
90 ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
91 ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
92 }
93
94 public static void writeSwappedUnsignedInteger( final byte[] data, final int offset, final long value )
95 {
96 data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
97 data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
98 data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
99 data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
100 }
101
102 public static void writeSwappedInteger( final byte[] data, final int offset, final int value )
103 {
104 data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
105 data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
106 data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
107 data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
108 }
109
110 public static int readSwappedInteger( final byte[] data, final int offset )
111 {
112 return (
113 ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
114 ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
115 ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
116 ( ( data[ offset + 3 ] & 0xff ) << 24 ) );
117 }
118
119 public static long readSwappedUnsignedInteger( final byte[] data, final int offset )
120 {
121 return (
122 ( (long)( data[ offset + 0 ] & 0xff ) << 0 ) +
123 ( (long)( data[ offset + 1 ] & 0xff ) << 8 ) +
124 ( (long)( data[ offset + 2 ] & 0xff ) << 16 ) +
125 ( (long)( data[ offset + 3 ] & 0xff ) << 24 ) );
126 }
127
128 public static void writeSwappedLong( final byte[] data, final int offset, final long value )
129 {
130 data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
131 data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
132 data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
133 data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
134 data[ offset + 4 ] = (byte)( ( value >> 32 ) & 0xff );
135 data[ offset + 5 ] = (byte)( ( value >> 40 ) & 0xff );
136 data[ offset + 6 ] = (byte)( ( value >> 48 ) & 0xff );
137 data[ offset + 7 ] = (byte)( ( value >> 56 ) & 0xff );
138 }
139
140 public static long readSwappedLong( final byte[] data, final int offset )
141 {
142 return (
143 ( (long)( data[ offset + 0 ] & 0xff ) << 0 ) +
144 ( (long)( data[ offset + 1 ] & 0xff ) << 8 ) +
145 ( (long)( data[ offset + 2 ] & 0xff ) << 16 ) +
146 ( (long)( data[ offset + 3 ] & 0xff ) << 24 ) +
147 ( (long)( data[ offset + 4 ] & 0xff ) << 32 ) +
148 ( (long)( data[ offset + 5 ] & 0xff ) << 40 ) +
149 ( (long)( data[ offset + 6 ] & 0xff ) << 48 ) +
150 ( (long)( data[ offset + 7 ] & 0xff ) << 56 ) );
151 }
152
153 public static void writeSwappedFloat( final byte[] data, final int offset, final float value )
154 {
155 writeSwappedInteger( data, offset, Float.floatToIntBits( value ) );
156 }
157
158 public static float readSwappedFloat( final byte[] data, final int offset )
159 {
160 return Float.intBitsToFloat( readSwappedInteger( data, offset ) );
161 }
162
163 public static void writeSwappedDouble( final byte[] data, final int offset, final double value )
164 {
165 writeSwappedLong( data, offset, Double.doubleToRawLongBits( value ) );
166 }
167
168 public static double readSwappedDouble( final byte[] data, final int offset )
169 {
170 return Double.longBitsToDouble( readSwappedLong( data, offset ) );
171 }
172
173 //////////////////////////////////////////////////////////////////////
174 //
175 // The following haven't been fully tested yet - unit tests coming soon!!!
176 //
177 //////////////////////////////////////////////////////////////////////
178 public static void writeSwappedShort( final OutputStream output, final short value )
179 throws IOException
180 {
181 output.write( (byte)( ( value >> 0 ) & 0xff ) );
182 output.write( (byte)( ( value >> 8 ) & 0xff ) );
183 }
184
185 public static void writeSwappedUnsignedShort( final OutputStream output, final int value )
186 throws IOException
187 {
188 output.write( (byte)( ( value >> 0 ) & 0xff ) );
189 output.write( (byte)( ( value >> 8 ) & 0xff ) );
190 }
191
192 public static short readSwappedShort( final InputStream input )
193 throws IOException
194 {
195 return (short)(
196 ( ( (short)read( input ) & 0xff ) << 0 ) +
197 ( ( (short)read( input ) & 0xff ) << 8 ) );
198 }
199
200 public static int readSwappedUnsignedShort( final InputStream input )
201 throws IOException
202 {
203 final int value1 = read( input );
204 final int value2 = read( input );
205
206 return ( ( value1 << 0 ) + ( value2 << 8 ) );
207 }
208
209 public static void writeSwappedInteger( final OutputStream output, final int value )
210 throws IOException
211 {
212 output.write( (byte)( ( value >> 0 ) & 0xff ) );
213 output.write( (byte)( ( value >> 8 ) & 0xff ) );
214 output.write( (byte)( ( value >> 16 ) & 0xff ) );
215 output.write( (byte)( ( value >> 24 ) & 0xff ) );
216 }
217
218 public static void writeSwappedUnsignedInteger( final OutputStream output, final long value )
219 throws IOException
220 {
221 output.write( (byte)( ( value >> 0 ) & 0xff ) );
222 output.write( (byte)( ( value >> 8 ) & 0xff ) );
223 output.write( (byte)( ( value >> 16 ) & 0xff ) );
224 output.write( (byte)( ( value >> 24 ) & 0xff ) );
225 }
226
227 public static int readSwappedInteger( final InputStream input )
228 throws IOException
229 {
230 final int value1 = read( input );
231 final int value2 = read( input );
232 final int value3 = read( input );
233 final int value4 = read( input );
234
235 return (
236 ( value1 << 0 ) + ( value2 << 8 ) +
237 ( value3 << 16 ) + ( value4 << 24 ) );
238 }
239
240 public static long readSwappedUnsignedInteger( final InputStream input )
241 throws IOException
242 {
243 final long value1 = read( input );
244 final long value2 = read( input );
245 final long value3 = read( input );
246 final long value4 = read( input );
247
248 return (
249 ( value1 << 0 ) +
250 ( value2 << 8 ) +
251 ( value3 << 16 ) +
252 ( value4 << 24 ) );
253 }
254
255 public static void writeSwappedLong( final OutputStream output, final long value )
256 throws IOException
257 {
258 output.write( (byte)( ( value >> 0 ) & 0xff ) );
259 output.write( (byte)( ( value >> 8 ) & 0xff ) );
260 output.write( (byte)( ( value >> 16 ) & 0xff ) );
261 output.write( (byte)( ( value >> 24 ) & 0xff ) );
262 output.write( (byte)( ( value >> 32 ) & 0xff ) );
263 output.write( (byte)( ( value >> 40 ) & 0xff ) );
264 output.write( (byte)( ( value >> 48 ) & 0xff ) );
265 output.write( (byte)( ( value >> 56 ) & 0xff ) );
266 }
267
268 public static long readSwappedLong( final InputStream input )
269 throws IOException
270 {
271 final long value1 = read( input );
272 final long value2 = read( input );
273 final long value3 = read( input );
274 final long value4 = read( input );
275 final long value5 = read( input );
276 final long value6 = read( input );
277 final long value7 = read( input );
278 final long value8 = read( input );
279
280 return (
281 ( value1 << 0 ) +
282 ( value2 << 8 ) +
283 ( value3 << 16 ) +
284 ( value4 << 24 ) +
285 ( value5 << 32 ) +
286 ( value6 << 40 ) +
287 ( value7 << 48 ) +
288 ( value8 << 56 ) );
289 }
290
291 public static void writeSwappedFloat( final OutputStream output, final float value )
292 throws IOException
293 {
294 writeSwappedInteger( output, Float.floatToIntBits( value ) );
295 }
296
297 public static float readSwappedFloat( final InputStream input )
298 throws IOException
299 {
300 return Float.intBitsToFloat( readSwappedInteger( input ) );
301 }
302
303 public static void writeSwappedDouble( final OutputStream output, final double value )
304 throws IOException
305 {
306 writeSwappedLong( output, Double.doubleToLongBits( value ) );
307 }
308
309 public static double readSwappedDouble( final InputStream input )
310 throws IOException
311 {
312 return Double.longBitsToDouble( readSwappedLong( input ) );
313 }
314
315 private static int read( final InputStream input )
316 throws IOException
317 {
318 final int value = input.read();
319
320 if( -1 == value )
321 {
322 throw new EOFException( "Unexpected EOF reached" );
323 }
324
325 return value;
326 }
327 }
This page was automatically generated by Maven