Coverage Report - org.apache.commons.io.EndianUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
EndianUtils
100%
86/86
100%
4/4
1,097
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.io;
 18  
 
 19  
 import static org.apache.commons.io.IOUtils.EOF;
 20  
 
 21  
 import java.io.EOFException;
 22  
 import java.io.IOException;
 23  
 import java.io.InputStream;
 24  
 import java.io.OutputStream;
 25  
 
 26  
 /**
 27  
  * Utility code for dealing with different endian systems.
 28  
  * <p>
 29  
  * Different computer architectures adopt different conventions for
 30  
  * byte ordering. In so-called "Little Endian" architectures (eg Intel),
 31  
  * the low-order byte is stored in memory at the lowest address, and
 32  
  * subsequent bytes at higher addresses. For "Big Endian" architectures
 33  
  * (eg Motorola), the situation is reversed.
 34  
  * This class helps you solve this incompatibility.
 35  
  * <p>
 36  
  * Origin of code: Excalibur
 37  
  *
 38  
  * @see org.apache.commons.io.input.SwappedDataInputStream
 39  
  */
 40  
 public class EndianUtils {
 41  
 
 42  
     /**
 43  
      * Instances should NOT be constructed in standard programming.
 44  
      */
 45  
     public EndianUtils() {
 46  2
         super();
 47  2
     }
 48  
 
 49  
     // ========================================== Swapping routines
 50  
 
 51  
     /**
 52  
      * Converts a "short" value between endian systems.
 53  
      * @param value value to convert
 54  
      * @return the converted value
 55  
      */
 56  
     public static short swapShort(final short value) {
 57  12
         return (short) ( ( ( ( value >> 0 ) & 0xff ) << 8 ) +
 58  
             ( ( ( value >> 8 ) & 0xff ) << 0 ) );
 59  
     }
 60  
 
 61  
     /**
 62  
      * Converts a "int" value between endian systems.
 63  
      * @param value value to convert
 64  
      * @return the converted value
 65  
      */
 66  
     public static int swapInteger(final int value) {
 67  26
         return
 68  
             ( ( ( value >> 0 ) & 0xff ) << 24 ) +
 69  
             ( ( ( value >> 8 ) & 0xff ) << 16 ) +
 70  
             ( ( ( value >> 16 ) & 0xff ) << 8 ) +
 71  
             ( ( ( value >> 24 ) & 0xff ) << 0 );
 72  
     }
 73  
 
 74  
     /**
 75  
      * Converts a "long" value between endian systems.
 76  
      * @param value value to convert
 77  
      * @return the converted value
 78  
      */
 79  
     public static long swapLong(final long value) {
 80  20
         return
 81  
             ( ( ( value >> 0 ) & 0xff ) << 56 ) +
 82  
             ( ( ( value >> 8 ) & 0xff ) << 48 ) +
 83  
             ( ( ( value >> 16 ) & 0xff ) << 40 ) +
 84  
             ( ( ( value >> 24 ) & 0xff ) << 32 ) +
 85  
             ( ( ( value >> 32 ) & 0xff ) << 24 ) +
 86  
             ( ( ( value >> 40 ) & 0xff ) << 16 ) +
 87  
             ( ( ( value >> 48 ) & 0xff ) << 8 ) +
 88  
             ( ( ( value >> 56 ) & 0xff ) << 0 );
 89  
     }
 90  
 
 91  
     /**
 92  
      * Converts a "float" value between endian systems.
 93  
      * @param value value to convert
 94  
      * @return the converted value
 95  
      */
 96  
     public static float swapFloat(final float value) {
 97  8
         return Float.intBitsToFloat( swapInteger( Float.floatToIntBits( value ) ) );
 98  
     }
 99  
 
 100  
     /**
 101  
      * Converts a "double" value between endian systems.
 102  
      * @param value value to convert
 103  
      * @return the converted value
 104  
      */
 105  
     public static double swapDouble(final double value) {
 106  8
         return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) );
 107  
     }
 108  
 
 109  
     // ========================================== Swapping read/write routines
 110  
 
 111  
     /**
 112  
      * Writes a "short" value to a byte array at a given offset. The value is
 113  
      * converted to the opposed endian system while writing.
 114  
      * @param data target byte array
 115  
      * @param offset starting offset in the byte array
 116  
      * @param value value to write
 117  
      */
 118  
     public static void writeSwappedShort(final byte[] data, final int offset, final short value) {
 119  2
         data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
 120  2
         data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
 121  2
     }
 122  
 
 123  
     /**
 124  
      * Reads a "short" value from a byte array at a given offset. The value is
 125  
      * converted to the opposed endian system while reading.
 126  
      * @param data source byte array
 127  
      * @param offset starting offset in the byte array
 128  
      * @return the value read
 129  
      */
 130  
     public static short readSwappedShort(final byte[] data, final int offset) {
 131  2
         return (short)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
 132  
             ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
 133  
     }
 134  
 
 135  
     /**
 136  
      * Reads an unsigned short (16-bit) value from a byte array at a given
 137  
      * offset. The value is converted to the opposed endian system while
 138  
      * reading.
 139  
      * @param data source byte array
 140  
      * @param offset starting offset in the byte array
 141  
      * @return the value read
 142  
      */
 143  
     public static int readSwappedUnsignedShort(final byte[] data, final int offset) {
 144  2
         return ( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
 145  
             ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
 146  
     }
 147  
 
 148  
     /**
 149  
      * Writes a "int" value to a byte array at a given offset. The value is
 150  
      * converted to the opposed endian system while writing.
 151  
      * @param data target byte array
 152  
      * @param offset starting offset in the byte array
 153  
      * @param value value to write
 154  
      */
 155  
     public static void writeSwappedInteger(final byte[] data, final int offset, final int value) {
 156  4
         data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
 157  4
         data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
 158  4
         data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
 159  4
         data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
 160  4
     }
 161  
 
 162  
     /**
 163  
      * Reads a "int" value from a byte array at a given offset. The value is
 164  
      * converted to the opposed endian system while reading.
 165  
      * @param data source byte array
 166  
      * @param offset starting offset in the byte array
 167  
      * @return the value read
 168  
      */
 169  
     public static int readSwappedInteger(final byte[] data, final int offset) {
 170  68
         return ( ( ( 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(final byte[] data, final int offset) {
 185  4
         final long low = ( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
 186  
                      ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
 187  
                      ( ( data[ offset + 2 ] & 0xff ) << 16 ) );
 188  
 
 189  4
         final long high = data[ offset + 3 ] & 0xff;
 190  
 
 191  4
         return (high << 24) + (0xffffffffL & low); 
 192  
     }
 193  
 
 194  
     /**
 195  
      * Writes a "long" value to a byte array at a given offset. The value is
 196  
      * converted to the opposed endian system while writing.
 197  
      * @param data target byte array
 198  
      * @param offset starting offset in the byte array
 199  
      * @param value value to write
 200  
      */
 201  
     public static void writeSwappedLong(final byte[] data, final int offset, final long value) {
 202  24
         data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
 203  24
         data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
 204  24
         data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
 205  24
         data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
 206  24
         data[ offset + 4 ] = (byte)( ( value >> 32 ) & 0xff );
 207  24
         data[ offset + 5 ] = (byte)( ( value >> 40 ) & 0xff );
 208  24
         data[ offset + 6 ] = (byte)( ( value >> 48 ) & 0xff );
 209  24
         data[ offset + 7 ] = (byte)( ( value >> 56 ) & 0xff );
 210  24
     }
 211  
 
 212  
     /**
 213  
      * Reads a "long" value from a byte array at a given offset. The value is
 214  
      * converted to the opposed endian system while reading.
 215  
      * @param data source byte array
 216  
      * @param offset starting offset in the byte array
 217  
      * @return the value read
 218  
      */
 219  
     public static long readSwappedLong(final byte[] data, final int offset) {
 220  32
         final long low = readSwappedInteger(data, offset);
 221  32
         final long high = readSwappedInteger(data, offset + 4);
 222  32
         return (high << 32) + (0xffffffffL & low);
 223  
     }
 224  
 
 225  
     /**
 226  
      * Writes a "float" value to a byte array at a given offset. The value is
 227  
      * converted to the opposed endian system while writing.
 228  
      * @param data target byte array
 229  
      * @param offset starting offset in the byte array
 230  
      * @param value value to write
 231  
      */
 232  
     public static void writeSwappedFloat(final byte[] data, final int offset, final float value) {
 233  2
         writeSwappedInteger( data, offset, Float.floatToIntBits( value ) );
 234  2
     }
 235  
 
 236  
     /**
 237  
      * Reads a "float" value from a byte array at a given offset. The value is
 238  
      * converted to the opposed endian system while reading.
 239  
      * @param data source byte array
 240  
      * @param offset starting offset in the byte array
 241  
      * @return the value read
 242  
      */
 243  
     public static float readSwappedFloat(final byte[] data, final int offset) {
 244  2
         return Float.intBitsToFloat( readSwappedInteger( data, offset ) );
 245  
     }
 246  
 
 247  
     /**
 248  
      * Writes a "double" value to a byte array at a given offset. The value is
 249  
      * converted to the opposed endian system while writing.
 250  
      * @param data target byte array
 251  
      * @param offset starting offset in the byte array
 252  
      * @param value value to write
 253  
      */
 254  
     public static void writeSwappedDouble(final byte[] data, final int offset, final double value) {
 255  12
         writeSwappedLong( data, offset, Double.doubleToLongBits( value ) );
 256  12
     }
 257  
 
 258  
     /**
 259  
      * Reads a "double" value from a byte array at a given offset. The value is
 260  
      * converted to the opposed endian system while reading.
 261  
      * @param data source byte array
 262  
      * @param offset starting offset in the byte array
 263  
      * @return the value read
 264  
      */
 265  
     public static double readSwappedDouble(final byte[] data, final int offset) {
 266  12
         return Double.longBitsToDouble( readSwappedLong( data, offset ) );
 267  
     }
 268  
 
 269  
     /**
 270  
      * Writes a "short" value to an OutputStream. The value is
 271  
      * converted to the opposed endian system while writing.
 272  
      * @param output target OutputStream
 273  
      * @param value value to write
 274  
      * @throws IOException in case of an I/O problem
 275  
      */
 276  
     public static void writeSwappedShort(final OutputStream output, final short value)
 277  
         throws IOException
 278  
     {
 279  2
         output.write( (byte)( ( value >> 0 ) & 0xff ) );
 280  2
         output.write( (byte)( ( value >> 8 ) & 0xff ) );
 281  2
     }
 282  
 
 283  
     /**
 284  
      * Reads a "short" value from an InputStream. The value is
 285  
      * converted to the opposed endian system while reading.
 286  
      * @param input source InputStream
 287  
      * @return the value just read
 288  
      * @throws IOException in case of an I/O problem
 289  
      */
 290  
     public static short readSwappedShort(final InputStream input)
 291  
         throws IOException
 292  
     {
 293  6
         return (short)( ( ( read( input ) & 0xff ) << 0 ) +
 294  
             ( ( read( input ) & 0xff ) << 8 ) );
 295  
     }
 296  
 
 297  
     /**
 298  
      * Reads a unsigned short (16-bit) from an InputStream. The value is
 299  
      * converted to the opposed endian system while reading.
 300  
      * @param input source InputStream
 301  
      * @return the value just read
 302  
      * @throws IOException in case of an I/O problem
 303  
      */
 304  
     public static int readSwappedUnsignedShort(final InputStream input)
 305  
         throws IOException
 306  
     {
 307  4
         final int value1 = read( input );
 308  4
         final int value2 = read( input );
 309  
 
 310  4
         return ( ( ( value1 & 0xff ) << 0 ) +
 311  
             ( ( value2 & 0xff ) << 8 ) );
 312  
     }
 313  
 
 314  
     /**
 315  
      * Writes a "int" value to an OutputStream. The value is
 316  
      * converted to the opposed endian system while writing.
 317  
      * @param output target OutputStream
 318  
      * @param value value to write
 319  
      * @throws IOException in case of an I/O problem
 320  
      */
 321  
     public static void writeSwappedInteger(final OutputStream output, final int value)
 322  
         throws IOException
 323  
     {
 324  4
         output.write( (byte)( ( value >> 0 ) & 0xff ) );
 325  4
         output.write( (byte)( ( value >> 8 ) & 0xff ) );
 326  4
         output.write( (byte)( ( value >> 16 ) & 0xff ) );
 327  4
         output.write( (byte)( ( value >> 24 ) & 0xff ) );
 328  4
     }
 329  
 
 330  
     /**
 331  
      * Reads a "int" value from an InputStream. The value is
 332  
      * converted to the opposed endian system while reading.
 333  
      * @param input source InputStream
 334  
      * @return the value just read
 335  
      * @throws IOException in case of an I/O problem
 336  
      */
 337  
     public static int readSwappedInteger(final InputStream input)
 338  
         throws IOException
 339  
     {
 340  10
         final int value1 = read( input );
 341  10
         final int value2 = read( input );
 342  10
         final int value3 = read( input );
 343  10
         final int value4 = read( input );
 344  
 
 345  10
         return ( ( value1 & 0xff ) << 0 ) +
 346  
             ( ( value2 & 0xff ) << 8 ) +
 347  
             ( ( value3 & 0xff ) << 16 ) +
 348  
             ( ( value4 & 0xff ) << 24 );
 349  
     }
 350  
 
 351  
     /**
 352  
      * Reads a unsigned integer (32-bit) from an InputStream. The value is
 353  
      * converted to the opposed endian system while reading.
 354  
      * @param input source InputStream
 355  
      * @return the value just read
 356  
      * @throws IOException in case of an I/O problem
 357  
      */
 358  
     public static long readSwappedUnsignedInteger(final InputStream input)
 359  
         throws IOException
 360  
     {
 361  4
         final int value1 = read( input );
 362  4
         final int value2 = read( input );
 363  4
         final int value3 = read( input );
 364  4
         final int value4 = read( input );
 365  
 
 366  4
         final long low = ( ( ( value1 & 0xff ) << 0 ) +
 367  
                      ( ( value2 & 0xff ) << 8 ) +
 368  
                      ( ( value3 & 0xff ) << 16 ) );
 369  
 
 370  4
         final long high = value4 & 0xff;
 371  
 
 372  4
         return (high << 24) + (0xffffffffL & low); 
 373  
     }
 374  
 
 375  
     /**
 376  
      * Writes a "long" value to an OutputStream. The value is
 377  
      * converted to the opposed endian system while writing.
 378  
      * @param output target OutputStream
 379  
      * @param value value to write
 380  
      * @throws IOException in case of an I/O problem
 381  
      */
 382  
     public static void writeSwappedLong(final OutputStream output, final long value)
 383  
         throws IOException
 384  
     {
 385  4
         output.write( (byte)( ( value >> 0 ) & 0xff ) );
 386  4
         output.write( (byte)( ( value >> 8 ) & 0xff ) );
 387  4
         output.write( (byte)( ( value >> 16 ) & 0xff ) );
 388  4
         output.write( (byte)( ( value >> 24 ) & 0xff ) );
 389  4
         output.write( (byte)( ( value >> 32 ) & 0xff ) );
 390  4
         output.write( (byte)( ( value >> 40 ) & 0xff ) );
 391  4
         output.write( (byte)( ( value >> 48 ) & 0xff ) );
 392  4
         output.write( (byte)( ( value >> 56 ) & 0xff ) );
 393  4
     }
 394  
 
 395  
     /**
 396  
      * Reads a "long" value from an InputStream. The value is
 397  
      * converted to the opposed endian system while reading.
 398  
      * @param input source InputStream
 399  
      * @return the value just read
 400  
      * @throws IOException in case of an I/O problem
 401  
      */
 402  
     public static long readSwappedLong(final InputStream input)
 403  
         throws IOException
 404  
     {
 405  10
         final byte[] bytes = new byte[8];
 406  74
         for ( int i=0; i<8; i++ ) {
 407  66
             bytes[i] = (byte) read( input );
 408  
         }
 409  8
         return readSwappedLong( bytes, 0 );
 410  
     }
 411  
 
 412  
     /**
 413  
      * Writes a "float" value to an OutputStream. The value is
 414  
      * converted to the opposed endian system while writing.
 415  
      * @param output target OutputStream
 416  
      * @param value value to write
 417  
      * @throws IOException in case of an I/O problem
 418  
      */
 419  
     public static void writeSwappedFloat(final OutputStream output, final float value)
 420  
         throws IOException
 421  
     {
 422  2
         writeSwappedInteger( output, Float.floatToIntBits( value ) );
 423  2
     }
 424  
 
 425  
     /**
 426  
      * Reads a "float" value from an InputStream. The value is
 427  
      * converted to the opposed endian system while reading.
 428  
      * @param input source InputStream
 429  
      * @return the value just read
 430  
      * @throws IOException in case of an I/O problem
 431  
      */
 432  
     public static float readSwappedFloat(final InputStream input)
 433  
         throws IOException
 434  
     {
 435  4
         return Float.intBitsToFloat( readSwappedInteger( input ) );
 436  
     }
 437  
 
 438  
     /**
 439  
      * Writes a "double" value to an OutputStream. The value is
 440  
      * converted to the opposed endian system while writing.
 441  
      * @param output target OutputStream
 442  
      * @param value value to write
 443  
      * @throws IOException in case of an I/O problem
 444  
      */
 445  
     public static void writeSwappedDouble(final OutputStream output, final double value)
 446  
         throws IOException
 447  
     {
 448  2
         writeSwappedLong( output, Double.doubleToLongBits( value ) );
 449  2
     }
 450  
 
 451  
     /**
 452  
      * Reads a "double" value from an InputStream. The value is
 453  
      * converted to the opposed endian system while reading.
 454  
      * @param input source InputStream
 455  
      * @return the value just read
 456  
      * @throws IOException in case of an I/O problem
 457  
      */
 458  
     public static double readSwappedDouble(final InputStream input)
 459  
         throws IOException
 460  
     {
 461  6
         return Double.longBitsToDouble( readSwappedLong( input ) );
 462  
     }
 463  
 
 464  
     /**
 465  
      * Reads the next byte from the input stream.
 466  
      * @param input  the stream
 467  
      * @return the byte
 468  
      * @throws IOException if the end of file is reached
 469  
      */
 470  
     private static int read(final InputStream input)
 471  
         throws IOException
 472  
     {
 473  142
         final int value = input.read();
 474  
 
 475  142
         if( EOF == value ) {
 476  2
             throw new EOFException( "Unexpected EOF reached" );
 477  
         }
 478  
 
 479  140
         return value;
 480  
     }
 481  
 }