KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > EndianUtils


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.io;
17
18 import java.io.EOFException JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 /**
24  * Utility code for dealing with different endian systems.
25  * <br>
26  * Origin of code: Apache Avalon (Excalibur)
27  *
28  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
29  * @version CVS $Revision: 1.13 $ $Date: 2004/02/23 04:35:59 $
30  */

31 public final class EndianUtils
32 {
33
34     /**
35      * Instances should NOT be constructed in standard programming.
36      */

37     public EndianUtils() { }
38
39     // ========================================== Swapping routines
40

41     /**
42      * Converts a "short" value between endian systems.
43      * @param value value to convert
44      * @return the converted value
45      */

46     public static short swapShort( short value )
47     {
48         return (short)( ( ( ( value >> 0 ) & 0xff ) << 8 ) +
49             ( ( ( value >> 8 ) & 0xff ) << 0 ) );
50     }
51
52     /**
53      * Converts a "int" value between endian systems.
54      * @param value value to convert
55      * @return the converted value
56      */

57     public static int swapInteger( int value )
58     {
59         return
60             ( ( ( value >> 0 ) & 0xff ) << 24 ) +
61             ( ( ( value >> 8 ) & 0xff ) << 16 ) +
62             ( ( ( value >> 16 ) & 0xff ) << 8 ) +
63             ( ( ( value >> 24 ) & 0xff ) << 0 );
64     }
65
66     /**
67      * Converts a "long" value between endian systems.
68      * @param value value to convert
69      * @return the converted value
70      */

71     public static long swapLong( long value )
72     {
73         return
74             ( ( ( value >> 0 ) & 0xff ) << 56 ) +
75             ( ( ( value >> 8 ) & 0xff ) << 48 ) +
76             ( ( ( value >> 16 ) & 0xff ) << 40 ) +
77             ( ( ( value >> 24 ) & 0xff ) << 32 ) +
78             ( ( ( value >> 32 ) & 0xff ) << 24 ) +
79             ( ( ( value >> 40 ) & 0xff ) << 16 ) +
80             ( ( ( value >> 48 ) & 0xff ) << 8 ) +
81             ( ( ( value >> 56 ) & 0xff ) << 0 );
82     }
83
84     /**
85      * Converts a "float" value between endian systems.
86      * @param value value to convert
87      * @return the converted value
88      */

89     public static float swapFloat( float value )
90     {
91         return Float.intBitsToFloat( swapInteger( Float.floatToIntBits( value ) ) );
92     }
93
94     /**
95      * Converts a "double" value between endian systems.
96      * @param value value to convert
97      * @return the converted value
98      */

99     public static double swapDouble( double value )
100     {
101         return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) );
102     }
103
104     // ========================================== Swapping read/write routines
105

106     /**
107      * Writes a "short" value to a byte array at a given offset. The value is
108      * converted to the opposed endian system while writing.
109      * @param data target byte array
110      * @param offset starting offset in the byte array
111      * @param value value to write
112      */

113     public static void writeSwappedShort( byte[] data, int offset, short value )
114     {
115         data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
116         data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
117     }
118
119     /**
120      * Reads a "short" value from a byte array at a given offset. The value is
121      * converted to the opposed endian system while reading.
122      * @param data source byte array
123      * @param offset starting offset in the byte array
124      * @return the value read
125      */

126     public static short readSwappedShort( byte[] data, int offset )
127     {
128         return (short)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
129             ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
130     }
131
132     /**
133      * Reads an unsigned short (16-bit) value from a byte array at a given
134      * offset. The value is converted to the opposed endian system while
135      * reading.
136      * @param data source byte array
137      * @param offset starting offset in the byte array
138      * @return the value read
139      */

140     public static int readSwappedUnsignedShort( byte[] data, int offset )
141     {
142         return (int)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
143             ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
144     }
145
146     /**
147      * Writes a "int" value to a byte array at a given offset. The value is
148      * converted to the opposed endian system while writing.
149      * @param data target byte array
150      * @param offset starting offset in the byte array
151      * @param value value to write
152      */

153     public static void writeSwappedInteger( byte[] data, int offset, int value )
154     {
155         data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
156         data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
157         data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
158         data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
159     }
160
161     /**
162      * Reads a "int" value from a byte array at a given offset. The value is
163      * converted to the opposed endian system while reading.
164      * @param data source byte array
165      * @param offset starting offset in the byte array
166      * @return the value read
167      */

168     public static int readSwappedInteger( byte[] data, int offset )
169     {
170         return (int)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
171             ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
172             ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
173             ( ( data[ offset + 3 ] & 0xff ) << 24 ) );
174     }
175
176     /**
177      * Reads an unsigned integer (32-bit) value from a byte array at a given
178      * offset. The value is converted to the opposed endian system while
179      * reading.
180      * @param data source byte array
181      * @param offset starting offset in the byte array
182      * @return the value read
183      */

184     public static long readSwappedUnsignedInteger( byte[] data, int offset )
185     {
186         return (long)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
187             ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
188             ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
189             ( ( data[ offset + 3 ] & 0xff ) << 24 ) );
190     }
191
192     /**
193      * Writes a "long" value to a byte array at a given offset. The value is
194      * converted to the opposed endian system while writing.
195      * @param data target byte array
196      * @param offset starting offset in the byte array
197      * @param value value to write
198      */

199     public static void writeSwappedLong( byte[] data, int offset, long value )
200     {
201         data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
202         data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
203         data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
204         data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
205         data[ offset + 4 ] = (byte)( ( value >> 32 ) & 0xff );
206         data[ offset + 5 ] = (byte)( ( value >> 40 ) & 0xff );
207         data[ offset + 6 ] = (byte)( ( value >> 48 ) & 0xff );
208         data[ offset + 7 ] = (byte)( ( value >> 56 ) & 0xff );
209     }
210
211     /**
212      * Reads a "long" value from a byte array at a given offset. The value is
213      * converted to the opposed endian system while reading.
214      * @param data source byte array
215      * @param offset starting offset in the byte array
216      * @return the value read
217      */

218     public static long readSwappedLong( byte[] data, int offset )
219     {
220         long low = (long)(
221             ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
222             ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
223             ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
224             ( ( data[ offset + 3 ] & 0xff ) << 24 ) );
225         long high = (long)(
226             ( ( data[ offset + 4 ] & 0xff ) << 0 ) +
227             ( ( data[ offset + 5 ] & 0xff ) << 8 ) +
228             ( ( data[ offset + 6 ] & 0xff ) << 16 ) +
229             ( ( data[ offset + 7 ] & 0xff ) << 24 ) );
230         return low + (high << 32);
231     }
232
233     /**
234      * Writes a "float" value to a byte array at a given offset. The value is
235      * converted to the opposed endian system while writing.
236      * @param data target byte array
237      * @param offset starting offset in the byte array
238      * @param value value to write
239      */

240     public static void writeSwappedFloat( byte[] data, int offset, float value )
241     {
242         writeSwappedInteger( data, offset, Float.floatToIntBits( value ) );
243     }
244
245     /**
246      * Reads a "float" value from a byte array at a given offset. The value is
247      * converted to the opposed endian system while reading.
248      * @param data source byte array
249      * @param offset starting offset in the byte array
250      * @return the value read
251      */

252     public static float readSwappedFloat( byte[] data, int offset )
253     {
254         return Float.intBitsToFloat( readSwappedInteger( data, offset ) );
255     }
256
257     /**
258      * Writes a "double" value to a byte array at a given offset. The value is
259      * converted to the opposed endian system while writing.
260      * @param data target byte array
261      * @param offset starting offset in the byte array
262      * @param value value to write
263      */

264     public static void writeSwappedDouble( byte[] data, int offset, double value )
265     {
266         writeSwappedLong( data, offset, Double.doubleToLongBits( value ) );
267     }
268
269     /**
270      * Reads a "double" value from a byte array at a given offset. The value is
271      * converted to the opposed endian system while reading.
272      * @param data source byte array
273      * @param offset starting offset in the byte array
274      * @return the value read
275      */

276     public static double readSwappedDouble( byte[] data, int offset )
277     {
278         return Double.longBitsToDouble( readSwappedLong( data, offset ) );
279     }
280
281     /**
282      * Writes a "short" value to an OutputStream. The value is
283      * converted to the opposed endian system while writing.
284      * @param output target OutputStream
285      * @param value value to write
286      * @throws IOException in case of an I/O problem
287      */

288     public static void writeSwappedShort( OutputStream JavaDoc output, short value )
289         throws IOException JavaDoc
290     {
291         output.write( (byte)( ( value >> 0 ) & 0xff ) );
292         output.write( (byte)( ( value >> 8 ) & 0xff ) );
293     }
294
295     /**
296      * Reads a "short" value from an InputStream. The value is
297      * converted to the opposed endian system while reading.
298      * @param input source InputStream
299      * @return the value just read
300      * @throws IOException in case of an I/O problem
301      */

302     public static short readSwappedShort( InputStream JavaDoc input )
303         throws IOException JavaDoc
304     {
305         return (short)( ( ( read( input ) & 0xff ) << 0 ) +
306             ( ( read( input ) & 0xff ) << 8 ) );
307     }
308
309     /**
310      * Reads a unsigned short (16-bit) from an InputStream. The value is
311      * converted to the opposed endian system while reading.
312      * @param input source InputStream
313      * @return the value just read
314      * @throws IOException in case of an I/O problem
315      */

316     public static int readSwappedUnsignedShort( InputStream JavaDoc input )
317         throws IOException JavaDoc
318     {
319         int value1 = read( input );
320         int value2 = read( input );
321
322         return (int)( ( ( value1 & 0xff ) << 0 ) +
323             ( ( value2 & 0xff ) << 8 ) );
324     }
325
326     /**
327      * Writes a "int" value to an OutputStream. The value is
328      * converted to the opposed endian system while writing.
329      * @param output target OutputStream
330      * @param value value to write
331      * @throws IOException in case of an I/O problem
332      */

333     public static void writeSwappedInteger( OutputStream JavaDoc output, int value )
334         throws IOException JavaDoc
335     {
336         output.write( (byte)( ( value >> 0 ) & 0xff ) );
337         output.write( (byte)( ( value >> 8 ) & 0xff ) );
338         output.write( (byte)( ( value >> 16 ) & 0xff ) );
339         output.write( (byte)( ( value >> 24 ) & 0xff ) );
340     }
341
342     /**
343      * Reads a "int" value from an InputStream. The value is
344      * converted to the opposed endian system while reading.
345      * @param input source InputStream
346      * @return the value just read
347      * @throws IOException in case of an I/O problem
348      */

349     public static int readSwappedInteger( InputStream JavaDoc input )
350         throws IOException JavaDoc
351     {
352         int value1 = read( input );
353         int value2 = read( input );
354         int value3 = read( input );
355         int value4 = read( input );
356
357         return (int)( ( ( value1 & 0xff ) << 0 ) +
358             ( ( value2 & 0xff ) << 8 ) +
359             ( ( value3 & 0xff ) << 16 ) +
360             ( ( value4 & 0xff ) << 24 ) );
361     }
362
363     /**
364      * Reads a unsigned integer (32-bit) from an InputStream. The value is
365      * converted to the opposed endian system while reading.
366      * @param input source InputStream
367      * @return the value just read
368      * @throws IOException in case of an I/O problem
369      */

370     public static long readSwappedUnsignedInteger( InputStream JavaDoc input )
371         throws IOException JavaDoc
372     {
373         int value1 = read( input );
374         int value2 = read( input );
375         int value3 = read( input );
376         int value4 = read( input );
377
378         return (long)( ( ( value1 & 0xff ) << 0 ) +
379             ( ( value2 & 0xff ) << 8 ) +
380             ( ( value3 & 0xff ) << 16 ) +
381             ( ( value4 & 0xff ) << 24 ) );
382     }
383
384     /**
385      * Writes a "long" value to an OutputStream. The value is
386      * converted to the opposed endian system while writing.
387      * @param output target OutputStream
388      * @param value value to write
389      * @throws IOException in case of an I/O problem
390      */

391     public static void writeSwappedLong( OutputStream JavaDoc output, long value )
392         throws IOException JavaDoc
393     {
394         output.write( (byte)( ( value >> 0 ) & 0xff ) );
395         output.write( (byte)( ( value >> 8 ) & 0xff ) );
396         output.write( (byte)( ( value >> 16 ) & 0xff ) );
397         output.write( (byte)( ( value >> 24 ) & 0xff ) );
398         output.write( (byte)( ( value >> 32 ) & 0xff ) );
399         output.write( (byte)( ( value >> 40 ) & 0xff ) );
400         output.write( (byte)( ( value >> 48 ) & 0xff ) );
401         output.write( (byte)( ( value >> 56 ) & 0xff ) );
402     }
403
404     /**
405      * Reads a "long" value from an InputStream. The value is
406      * converted to the opposed endian system while reading.
407      * @param input source InputStream
408      * @return the value just read
409      * @throws IOException in case of an I/O problem
410      */

411     public static long readSwappedLong( InputStream JavaDoc input )
412         throws IOException JavaDoc
413     {
414         byte[] bytes = new byte[8];
415         input.read( bytes );
416         return readSwappedLong( bytes, 0 );
417     }
418
419     /**
420      * Writes a "float" value to an OutputStream. The value is
421      * converted to the opposed endian system while writing.
422      * @param output target OutputStream
423      * @param value value to write
424      * @throws IOException in case of an I/O problem
425      */

426     public static void writeSwappedFloat( OutputStream JavaDoc output, float value )
427         throws IOException JavaDoc
428     {
429         writeSwappedInteger( output, Float.floatToIntBits( value ) );
430     }
431
432     /**
433      * Reads a "float" value from an InputStream. The value is
434      * converted to the opposed endian system while reading.
435      * @param input source InputStream
436      * @return the value just read
437      * @throws IOException in case of an I/O problem
438      */

439     public static float readSwappedFloat( InputStream JavaDoc input )
440         throws IOException JavaDoc
441     {
442         return Float.intBitsToFloat( readSwappedInteger( input ) );
443     }
444
445     /**
446      * Writes a "double" value to an OutputStream. The value is
447      * converted to the opposed endian system while writing.
448      * @param output target OutputStream
449      * @param value value to write
450      * @throws IOException in case of an I/O problem
451      */

452     public static void writeSwappedDouble( OutputStream JavaDoc output, double value )
453         throws IOException JavaDoc
454     {
455         writeSwappedLong( output, Double.doubleToLongBits( value ) );
456     }
457
458     /**
459      * Reads a "double" value from an InputStream. The value is
460      * converted to the opposed endian system while reading.
461      * @param input source InputStream
462      * @return the value just read
463      * @throws IOException in case of an I/O problem
464      */

465     public static double readSwappedDouble( InputStream JavaDoc input )
466         throws IOException JavaDoc
467     {
468         return Double.longBitsToDouble( readSwappedLong( input ) );
469     }
470
471     private static int read( InputStream JavaDoc input )
472         throws IOException JavaDoc
473     {
474         int value = input.read();
475
476         if( -1 == value )
477         {
478             throw new EOFException JavaDoc( "Unexpected EOF reached" );
479         }
480
481         return value;
482     }
483 }
484
Popular Tags