KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > io > EndianUtil


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
26     must not be used to endorse or promote products derived from this software
27     without prior written permission. For written permission, please contact
28     apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48  
49 */

50 package org.apache.avalon.excalibur.io;
51
52 import java.io.EOFException JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.io.InputStream JavaDoc;
55 import java.io.OutputStream JavaDoc;
56
57 /**
58  * Utility code for dealing with different endian systems.
59  *
60  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
61  * @version CVS $Revision: 1.4 $ $Date: 2003/03/22 12:46:24 $
62  * @since 4.0
63  */

64 public final class EndianUtil
65 {
66     public static final int SIZEOF_BYTE = 1;
67     public static final int SIZEOF_SHORT = 2;
68     public static final int SIZEOF_INT = 4;
69     public static final int SIZEOF_FLOAT = 4;
70     public static final int SIZEOF_LONG = 8;
71
72     public static short swapShort( final short value )
73     {
74         return (short)( ( ( ( value >> 0 ) & 0xff ) << 8 ) +
75             ( ( ( value >> 8 ) & 0xff ) << 0 ) );
76     }
77
78     public static int swapInteger( final int value )
79     {
80         return
81             ( ( ( value >> 0 ) & 0xff ) << 24 ) +
82             ( ( ( value >> 8 ) & 0xff ) << 16 ) +
83             ( ( ( value >> 16 ) & 0xff ) << 8 ) +
84             ( ( ( value >> 24 ) & 0xff ) << 0 );
85     }
86
87     public static long swapLong( final long value )
88     {
89         return
90             ( ( ( value >> 0 ) & 0xff ) << 56 ) +
91             ( ( ( value >> 8 ) & 0xff ) << 48 ) +
92             ( ( ( value >> 16 ) & 0xff ) << 40 ) +
93             ( ( ( value >> 24 ) & 0xff ) << 32 ) +
94             ( ( ( value >> 32 ) & 0xff ) << 24 ) +
95             ( ( ( value >> 40 ) & 0xff ) << 16 ) +
96             ( ( ( value >> 48 ) & 0xff ) << 8 ) +
97             ( ( ( value >> 56 ) & 0xff ) << 0 );
98     }
99
100     public static float swapFloat( final float value )
101     {
102         return Float.intBitsToFloat( swapInteger( Float.floatToIntBits( value ) ) );
103     }
104
105     public static double swapDouble( final double value )
106     {
107         return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) );
108     }
109
110     public static void writeSwappedShort( final byte[] data, final int offset, final int value )
111     {
112         data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
113         data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
114     }
115
116     public static short readSwappedShort( final byte[] data, final int offset )
117     {
118         return (short)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
119             ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
120     }
121
122     public static int readSwappedUnsignedShort( final byte[] data, final int offset )
123     {
124         return (int)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
125             ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
126     }
127
128     public static void writeSwappedInteger( final byte[] data, final int offset, final int 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     }
135
136     public static int readSwappedInteger( final byte[] data, final int offset )
137     {
138         return (int)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
139             ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
140             ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
141             ( ( data[ offset + 3 ] & 0xff ) << 24 ) );
142     }
143
144     public static long readSwappedUnsignedInteger( final byte[] data, final int offset )
145     {
146         return (long)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
147             ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
148             ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
149             ( ( data[ offset + 3 ] & 0xff ) << 24 ) );
150     }
151
152     public static void writeSwappedLong( final byte[] data, final int offset, final long value )
153     {
154         data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
155         data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
156         data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
157         data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
158         data[ offset + 4 ] = (byte)( ( value >> 32 ) & 0xff );
159         data[ offset + 5 ] = (byte)( ( value >> 40 ) & 0xff );
160         data[ offset + 6 ] = (byte)( ( value >> 48 ) & 0xff );
161         data[ offset + 7 ] = (byte)( ( value >> 56 ) & 0xff );
162     }
163
164     public static long readSwappedLong( final byte[] data, final int offset )
165     {
166         return (long)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
167             ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
168             ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
169             ( ( data[ offset + 3 ] & 0xff ) << 24 ) +
170             ( ( data[ offset + 4 ] & 0xff ) << 32 ) +
171             ( ( data[ offset + 5 ] & 0xff ) << 40 ) +
172             ( ( data[ offset + 6 ] & 0xff ) << 48 ) +
173             ( ( data[ offset + 7 ] & 0xff ) << 56 ) );
174     }
175
176     public static void writeSwappedFloat( final byte[] data, final int offset, final float value )
177     {
178         writeSwappedInteger( data, offset, Float.floatToIntBits( value ) );
179     }
180
181     public static float readSwappedFloat( final byte[] data, final int offset )
182     {
183         return Float.intBitsToFloat( readSwappedInteger( data, offset ) );
184     }
185
186     public static void writeSwappedDouble( final byte[] data, final int offset, final double value )
187     {
188         writeSwappedLong( data, offset, Double.doubleToLongBits( value ) );
189     }
190
191     public static double readSwappedDouble( final byte[] data, final int offset )
192     {
193         return Double.longBitsToDouble( readSwappedLong( data, offset ) );
194     }
195
196     //////////////////////////////////////////////////////////////////////
197
//
198
// The following haven't been fully tested yet - unit tests coming soon!!!
199
//
200
//////////////////////////////////////////////////////////////////////
201
public static void writeSwappedShort( final OutputStream JavaDoc output, final int value )
202         throws IOException JavaDoc
203     {
204         output.write( (byte)( ( value >> 0 ) & 0xff ) );
205         output.write( (byte)( ( value >> 8 ) & 0xff ) );
206     }
207
208     public static short readSwappedShort( final InputStream JavaDoc input )
209         throws IOException JavaDoc
210     {
211         return (short)( ( ( read( input ) & 0xff ) << 0 ) +
212             ( ( read( input ) & 0xff ) << 8 ) );
213     }
214
215     public static int readSwappedUnsignedShort( final InputStream JavaDoc input )
216         throws IOException JavaDoc
217     {
218         final int value1 = read( input );
219         final int value2 = read( input );
220
221         return (int)( ( ( value1 & 0xff ) << 0 ) +
222             ( ( value2 & 0xff ) << 8 ) );
223     }
224
225     public static void writeSwappedInteger( final OutputStream JavaDoc output, final int value )
226         throws IOException JavaDoc
227     {
228         output.write( (byte)( ( value >> 0 ) & 0xff ) );
229         output.write( (byte)( ( value >> 8 ) & 0xff ) );
230         output.write( (byte)( ( value >> 16 ) & 0xff ) );
231         output.write( (byte)( ( value >> 24 ) & 0xff ) );
232     }
233
234     public static int readSwappedInteger( final InputStream JavaDoc input )
235         throws IOException JavaDoc
236     {
237         final int value1 = read( input );
238         final int value2 = read( input );
239         final int value3 = read( input );
240         final int value4 = read( input );
241
242         return (int)( ( ( value1 & 0xff ) << 0 ) +
243             ( ( value2 & 0xff ) << 8 ) +
244             ( ( value3 & 0xff ) << 16 ) +
245             ( ( value4 & 0xff ) << 24 ) );
246     }
247
248     public static long readSwappedUnsignedInteger( final InputStream JavaDoc input )
249         throws IOException JavaDoc
250     {
251         final int value1 = read( input );
252         final int value2 = read( input );
253         final int value3 = read( input );
254         final int value4 = read( input );
255
256         return (long)( ( ( value1 & 0xff ) << 0 ) +
257             ( ( value2 & 0xff ) << 8 ) +
258             ( ( value3 & 0xff ) << 16 ) +
259             ( ( value4 & 0xff ) << 24 ) );
260     }
261
262     public static void writeSwappedLong( final OutputStream JavaDoc output, final long value )
263         throws IOException JavaDoc
264     {
265         output.write( (byte)( ( value >> 0 ) & 0xff ) );
266         output.write( (byte)( ( value >> 8 ) & 0xff ) );
267         output.write( (byte)( ( value >> 16 ) & 0xff ) );
268         output.write( (byte)( ( value >> 24 ) & 0xff ) );
269         output.write( (byte)( ( value >> 32 ) & 0xff ) );
270         output.write( (byte)( ( value >> 40 ) & 0xff ) );
271         output.write( (byte)( ( value >> 48 ) & 0xff ) );
272         output.write( (byte)( ( value >> 56 ) & 0xff ) );
273     }
274
275     public static long readSwappedLong( final InputStream JavaDoc input )
276         throws IOException JavaDoc
277     {
278         final int value1 = read( input );
279         final int value2 = read( input );
280         final int value3 = read( input );
281         final int value4 = read( input );
282         final int value5 = read( input );
283         final int value6 = read( input );
284         final int value7 = read( input );
285         final int value8 = read( input );
286
287         return (long)( ( ( value1 & 0xff ) << 0 ) +
288             ( ( value2 & 0xff ) << 8 ) +
289             ( ( value3 & 0xff ) << 16 ) +
290             ( ( value4 & 0xff ) << 24 ) +
291             ( ( value5 & 0xff ) << 32 ) +
292             ( ( value6 & 0xff ) << 40 ) +
293             ( ( value7 & 0xff ) << 48 ) +
294             ( ( value8 & 0xff ) << 56 ) );
295     }
296
297     public static void writeSwappedFloat( final OutputStream JavaDoc output, final float value )
298         throws IOException JavaDoc
299     {
300         writeSwappedInteger( output, Float.floatToIntBits( value ) );
301     }
302
303     public static float readSwappedFloat( final InputStream JavaDoc input )
304         throws IOException JavaDoc
305     {
306         return Float.intBitsToFloat( readSwappedInteger( input ) );
307     }
308
309     public static void writeSwappedDouble( final OutputStream JavaDoc output, final double value )
310         throws IOException JavaDoc
311     {
312         writeSwappedLong( output, Double.doubleToLongBits( value ) );
313     }
314
315     public static double readSwappedDouble( final InputStream JavaDoc input )
316         throws IOException JavaDoc
317     {
318         return Double.longBitsToDouble( readSwappedLong( input ) );
319     }
320
321     private static int read( final InputStream JavaDoc input )
322         throws IOException JavaDoc
323     {
324         final int value = input.read();
325
326         if( -1 == value )
327         {
328             throw new EOFException JavaDoc( "Unexpected EOF reached" );
329         }
330
331         return value;
332     }
333 }
334
Popular Tags