1 16 package org.apache.commons.io; 17 18 import java.io.EOFException ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 23 31 public final class EndianUtils 32 { 33 34 37 public EndianUtils() { } 38 39 41 46 public static short swapShort( short value ) 47 { 48 return (short)( ( ( ( value >> 0 ) & 0xff ) << 8 ) + 49 ( ( ( value >> 8 ) & 0xff ) << 0 ) ); 50 } 51 52 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 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 89 public static float swapFloat( float value ) 90 { 91 return Float.intBitsToFloat( swapInteger( Float.floatToIntBits( value ) ) ); 92 } 93 94 99 public static double swapDouble( double value ) 100 { 101 return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) ); 102 } 103 104 106 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 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 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 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 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 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 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 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 240 public static void writeSwappedFloat( byte[] data, int offset, float value ) 241 { 242 writeSwappedInteger( data, offset, Float.floatToIntBits( value ) ); 243 } 244 245 252 public static float readSwappedFloat( byte[] data, int offset ) 253 { 254 return Float.intBitsToFloat( readSwappedInteger( data, offset ) ); 255 } 256 257 264 public static void writeSwappedDouble( byte[] data, int offset, double value ) 265 { 266 writeSwappedLong( data, offset, Double.doubleToLongBits( value ) ); 267 } 268 269 276 public static double readSwappedDouble( byte[] data, int offset ) 277 { 278 return Double.longBitsToDouble( readSwappedLong( data, offset ) ); 279 } 280 281 288 public static void writeSwappedShort( OutputStream output, short value ) 289 throws IOException 290 { 291 output.write( (byte)( ( value >> 0 ) & 0xff ) ); 292 output.write( (byte)( ( value >> 8 ) & 0xff ) ); 293 } 294 295 302 public static short readSwappedShort( InputStream input ) 303 throws IOException 304 { 305 return (short)( ( ( read( input ) & 0xff ) << 0 ) + 306 ( ( read( input ) & 0xff ) << 8 ) ); 307 } 308 309 316 public static int readSwappedUnsignedShort( InputStream input ) 317 throws IOException 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 333 public static void writeSwappedInteger( OutputStream output, int value ) 334 throws IOException 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 349 public static int readSwappedInteger( InputStream input ) 350 throws IOException 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 370 public static long readSwappedUnsignedInteger( InputStream input ) 371 throws IOException 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 391 public static void writeSwappedLong( OutputStream output, long value ) 392 throws IOException 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 411 public static long readSwappedLong( InputStream input ) 412 throws IOException 413 { 414 byte[] bytes = new byte[8]; 415 input.read( bytes ); 416 return readSwappedLong( bytes, 0 ); 417 } 418 419 426 public static void writeSwappedFloat( OutputStream output, float value ) 427 throws IOException 428 { 429 writeSwappedInteger( output, Float.floatToIntBits( value ) ); 430 } 431 432 439 public static float readSwappedFloat( InputStream input ) 440 throws IOException 441 { 442 return Float.intBitsToFloat( readSwappedInteger( input ) ); 443 } 444 445 452 public static void writeSwappedDouble( OutputStream output, double value ) 453 throws IOException 454 { 455 writeSwappedLong( output, Double.doubleToLongBits( value ) ); 456 } 457 458 465 public static double readSwappedDouble( InputStream input ) 466 throws IOException 467 { 468 return Double.longBitsToDouble( readSwappedLong( input ) ); 469 } 470 471 private static int read( InputStream input ) 472 throws IOException 473 { 474 int value = input.read(); 475 476 if( -1 == value ) 477 { 478 throw new EOFException ( "Unexpected EOF reached" ); 479 } 480 481 return value; 482 } 483 } 484 | Popular Tags |