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 java.io.EOFException;
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.io.OutputStream;
 23  
 
 24  
 /**
 25  
  * Utility code for dealing with different endian systems.
 26  
  * <p>
 27  
  * Different computer architectures adopt different conventions for
 28  
  * byte ordering. In so-called "Little Endian" architectures (eg Intel),
 29  
  * the low-order byte is stored in memory at the lowest address, and
 30  
  * subsequent bytes at higher addresses. For "Big Endian" architectures
 31  
  * (eg Motorola), the situation is reversed.
 32  
  * This class helps you solve this incompatibility.
 33  
  * <p>
 34  
  * Origin of code: Excalibur
 35  
  *
 36  
  * @version $Id: EndianUtils.java 1415850 2012-11-30 20:51:39Z ggregory $
 37  
  * @see org.apache.commons.io.input.SwappedDataInputStream
 38  
  */
 39  
 public class EndianUtils {
 40  
 
 41  
     /**
 42  
      * Instances should NOT be constructed in standard programming.
 43  
      */
 44  
     public EndianUtils() {
 45  2
         super();
 46  2
     }
 47  
 
 48  
     // ========================================== Swapping routines
 49  
 
 50  
     /**
 51  
      * Converts a "short" value between endian systems.
 52  
      * @param value value to convert
 53  
      * @return the converted value
 54  
      */
 55  
     public static short swapShort(final short value) {
 56  12
         return (short) ( ( ( ( value >> 0 ) & 0xff ) << 8 ) +
 57  
             ( ( ( value >> 8 ) & 0xff ) << 0 ) );
 58  
     }
 59  
 
 60  
     /**
 61  
      * Converts a "int" value between endian systems.
 62  
      * @param value value to convert
 63  
      * @return the converted value
 64  
      */
 65  
     public static int swapInteger(final int value) {
 66  26
         return
 67  
             ( ( ( value >> 0 ) & 0xff ) << 24 ) +
 68  
             ( ( ( value >> 8 ) & 0xff ) << 16 ) +
 69  
             ( ( ( value >> 16 ) & 0xff ) << 8 ) +
 70  
             ( ( ( value >> 24 ) & 0xff ) << 0 );
 71  
     }
 72  
 
 73  
     /**
 74  
      * Converts a "long" value between endian systems.
 75  
      * @param value value to convert
 76  
      * @return the converted value
 77  
      */
 78  
     public static long swapLong(final long value) {
 79  20
         return
 80  
             ( ( ( value >> 0 ) & 0xff ) << 56 ) +
 81  
             ( ( ( value >> 8 ) & 0xff ) << 48 ) +
 82  
             ( ( ( value >> 16 ) & 0xff ) << 40 ) +
 83  
             ( ( ( value >> 24 ) & 0xff ) << 32 ) +
 84  
             ( ( ( value >> 32 ) & 0xff ) << 24 ) +
 85  
             ( ( ( value >> 40 ) & 0xff ) << 16 ) +
 86  
             ( ( ( value >> 48 ) & 0xff ) << 8 ) +
 87  
             ( ( ( value >> 56 ) & 0xff ) << 0 );
 88  
     }
 89  
 
 90  
     /**
 91  
      * Converts a "float" value between endian systems.
 92  
      * @param value value to convert
 93  
      * @return the converted value
 94  
      */
 95  
     public static float swapFloat(final float value) {
 96  8
         return Float.intBitsToFloat( swapInteger( Float.floatToIntBits( value ) ) );
 97  
     }
 98  
 
 99  
     /**
 100  
      * Converts a "double" value between endian systems.
 101  
      * @param value value to convert
 102  
      * @return the converted value
 103  
      */
 104  
     public static double swapDouble(final double value) {
 105  8
         return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) );
 106  
     }
 107  
 
 108  
     // ========================================== Swapping read/write routines
 109  
 
 110  
     /**
 111  
      * Writes a "short" value to a byte array at a given offset. The value is
 112  
      * converted to the opposed endian system while writing.
 113  
      * @param data target byte array
 114  
      * @param offset starting offset in the byte array
 115  
      * @param value value to write
 116  
      */
 117  
     public static void writeSwappedShort(final byte[] data, final int offset, final short value) {
 118  2
         data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
 119  2
         data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
 120  2
     }
 121  
 
 122  
     /**
 123  
      * Reads a "short" value from a byte array at a given offset. The value is
 124  
      * converted to the opposed endian system while reading.
 125  
      * @param data source byte array
 126  
      * @param offset starting offset in the byte array
 127  
      * @return the value read
 128  
      */
 129  
     public static short readSwappedShort(final byte[] data, final int offset) {
 130  2
         return (short)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
 131  
             ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
 132  
     }
 133  
 
 134  
     /**
 135  
      * Reads an unsigned short (16-bit) value from a byte array at a given
 136  
      * offset. The value is converted to the opposed endian system while
 137  
      * reading.
 138  
      * @param data source byte array
 139  
      * @param offset starting offset in the byte array
 140  
      * @return the value read
 141  
      */
 142  
     public static int readSwappedUnsignedShort(final byte[] data, final int offset) {
 143  2
         return ( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
 144  
             ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
 145  
     }
 146  
 
 147  
     /**
 148  
      * Writes a "int" value to a byte array at a given offset. The value is
 149  
      * converted to the opposed endian system while writing.
 150  
      * @param data target byte array
 151  
      * @param offset starting offset in the byte array
 152  
      * @param value value to write
 153  
      */
 154  
     public static void writeSwappedInteger(final byte[] data, final int offset, final int value) {
 155  4
         data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
 156  4
         data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
 157  4
         data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
 158  4
         data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
 159  4
     }
 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(final byte[] data, final int offset) {
 169  4
         return ( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
 170  
             ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
 171  
             ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
 172  
             ( ( data[ offset + 3 ] & 0xff ) << 24 ) );
 173  
     }
 174  
 
 175  
     /**
 176  
      * Reads an unsigned integer (32-bit) value from a byte array at a given
 177  
      * offset. The value is converted to the opposed endian system while
 178  
      * reading.
 179  
      * @param data source byte array
 180  
      * @param offset starting offset in the byte array
 181  
      * @return the value read
 182  
      */
 183  
     public static long readSwappedUnsignedInteger(final byte[] data, final int offset) {
 184  4
         final long low = ( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
 185  
                      ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
 186  
                      ( ( data[ offset + 2 ] & 0xff ) << 16 ) );
 187  
 
 188  4
         final long high = data[ offset + 3 ] & 0xff;
 189  
 
 190  4
         return (high << 24) + (0xffffffffL & low); 
 191  
     }
 192  
 
 193  
     /**
 194  
      * Writes a "long" value to a byte array at a given offset. The value is
 195  
      * converted to the opposed endian system while writing.
 196  
      * @param data target byte array
 197  
      * @param offset starting offset in the byte array
 198  
      * @param value value to write
 199  
      */
 200  
     public static void writeSwappedLong(final byte[] data, final int offset, final long value) {
 201  24
         data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
 202  24
         data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
 203  24
         data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
 204  24
         data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
 205  24
         data[ offset + 4 ] = (byte)( ( value >> 32 ) & 0xff );
 206  24
         data[ offset + 5 ] = (byte)( ( value >> 40 ) & 0xff );
 207  24
         data[ offset + 6 ] = (byte)( ( value >> 48 ) & 0xff );
 208  24
         data[ offset + 7 ] = (byte)( ( value >> 56 ) & 0xff );
 209  24
     }
 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(final byte[] data, final int offset) {
 219  32
         final long low = 
 220  
             ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
 221  
             ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
 222  
             ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
 223  
             ( ( data[ offset + 3 ] & 0xff ) << 24 );
 224  32
         final long high = 
 225  
             ( ( data[ offset + 4 ] & 0xff ) << 0 ) +
 226  
             ( ( data[ offset + 5 ] & 0xff ) << 8 ) +
 227  
             ( ( data[ offset + 6 ] & 0xff ) << 16 ) +
 228  
             ( ( data[ offset + 7 ] & 0xff ) << 24 );
 229  32
         return (high << 32) + (0xffffffffL & low); 
 230  
     }
 231  
 
 232  
     /**
 233  
      * Writes a "float" value to a byte array at a given offset. The value is
 234  
      * converted to the opposed endian system while writing.
 235  
      * @param data target byte array
 236  
      * @param offset starting offset in the byte array
 237  
      * @param value value to write
 238  
      */
 239  
     public static void writeSwappedFloat(final byte[] data, final int offset, final float value) {
 240  2
         writeSwappedInteger( data, offset, Float.floatToIntBits( value ) );
 241  2
     }
 242  
 
 243  
     /**
 244  
      * Reads a "float" value from a byte array at a given offset. The value is
 245  
      * converted to the opposed endian system while reading.
 246  
      * @param data source byte array
 247  
      * @param offset starting offset in the byte array
 248  
      * @return the value read
 249  
      */
 250  
     public static float readSwappedFloat(final byte[] data, final int offset) {
 251  2
         return Float.intBitsToFloat( readSwappedInteger( data, offset ) );
 252  
     }
 253  
 
 254  
     /**
 255  
      * Writes a "double" value to a byte array at a given offset. The value is
 256  
      * converted to the opposed endian system while writing.
 257  
      * @param data target byte array
 258  
      * @param offset starting offset in the byte array
 259  
      * @param value value to write
 260  
      */
 261  
     public static void writeSwappedDouble(final byte[] data, final int offset, final double value) {
 262  12
         writeSwappedLong( data, offset, Double.doubleToLongBits( value ) );
 263  12
     }
 264  
 
 265  
     /**
 266  
      * Reads a "double" value from a byte array at a given offset. The value is
 267  
      * converted to the opposed endian system while reading.
 268  
      * @param data source byte array
 269  
      * @param offset starting offset in the byte array
 270  
      * @return the value read
 271  
      */
 272  
     public static double readSwappedDouble(final byte[] data, final int offset) {
 273  12
         return Double.longBitsToDouble( readSwappedLong( data, offset ) );
 274  
     }
 275  
 
 276  
     /**
 277  
      * Writes a "short" value to an OutputStream. The value is
 278  
      * converted to the opposed endian system while writing.
 279  
      * @param output target OutputStream
 280  
      * @param value value to write
 281  
      * @throws IOException in case of an I/O problem
 282  
      */
 283  
     public static void writeSwappedShort(final OutputStream output, final short value)
 284  
         throws IOException
 285  
     {
 286  2
         output.write( (byte)( ( value >> 0 ) & 0xff ) );
 287  2
         output.write( (byte)( ( value >> 8 ) & 0xff ) );
 288  2
     }
 289  
 
 290  
     /**
 291  
      * Reads a "short" value from an InputStream. The value is
 292  
      * converted to the opposed endian system while reading.
 293  
      * @param input source InputStream
 294  
      * @return the value just read
 295  
      * @throws IOException in case of an I/O problem
 296  
      */
 297  
     public static short readSwappedShort(final InputStream input)
 298  
         throws IOException
 299  
     {
 300  6
         return (short)( ( ( read( input ) & 0xff ) << 0 ) +
 301  
             ( ( read( input ) & 0xff ) << 8 ) );
 302  
     }
 303  
 
 304  
     /**
 305  
      * Reads a unsigned short (16-bit) from an InputStream. The value is
 306  
      * converted to the opposed endian system while reading.
 307  
      * @param input source InputStream
 308  
      * @return the value just read
 309  
      * @throws IOException in case of an I/O problem
 310  
      */
 311  
     public static int readSwappedUnsignedShort(final InputStream input)
 312  
         throws IOException
 313  
     {
 314  4
         final int value1 = read( input );
 315  4
         final int value2 = read( input );
 316  
 
 317  4
         return ( ( ( value1 & 0xff ) << 0 ) +
 318  
             ( ( value2 & 0xff ) << 8 ) );
 319  
     }
 320  
 
 321  
     /**
 322  
      * Writes a "int" value to an OutputStream. The value is
 323  
      * converted to the opposed endian system while writing.
 324  
      * @param output target OutputStream
 325  
      * @param value value to write
 326  
      * @throws IOException in case of an I/O problem
 327  
      */
 328  
     public static void writeSwappedInteger(final OutputStream output, final int value)
 329  
         throws IOException
 330  
     {
 331  4
         output.write( (byte)( ( value >> 0 ) & 0xff ) );
 332  4
         output.write( (byte)( ( value >> 8 ) & 0xff ) );
 333  4
         output.write( (byte)( ( value >> 16 ) & 0xff ) );
 334  4
         output.write( (byte)( ( value >> 24 ) & 0xff ) );
 335  4
     }
 336  
 
 337  
     /**
 338  
      * Reads a "int" value from an InputStream. The value is
 339  
      * converted to the opposed endian system while reading.
 340  
      * @param input source InputStream
 341  
      * @return the value just read
 342  
      * @throws IOException in case of an I/O problem
 343  
      */
 344  
     public static int readSwappedInteger(final InputStream input)
 345  
         throws IOException
 346  
     {
 347  10
         final int value1 = read( input );
 348  10
         final int value2 = read( input );
 349  10
         final int value3 = read( input );
 350  10
         final int value4 = read( input );
 351  
 
 352  10
         return ( ( value1 & 0xff ) << 0 ) +
 353  
             ( ( value2 & 0xff ) << 8 ) +
 354  
             ( ( value3 & 0xff ) << 16 ) +
 355  
             ( ( value4 & 0xff ) << 24 );
 356  
     }
 357  
 
 358  
     /**
 359  
      * Reads a unsigned integer (32-bit) from an InputStream. The value is
 360  
      * converted to the opposed endian system while reading.
 361  
      * @param input source InputStream
 362  
      * @return the value just read
 363  
      * @throws IOException in case of an I/O problem
 364  
      */
 365  
     public static long readSwappedUnsignedInteger(final InputStream input)
 366  
         throws IOException
 367  
     {
 368  4
         final int value1 = read( input );
 369  4
         final int value2 = read( input );
 370  4
         final int value3 = read( input );
 371  4
         final int value4 = read( input );
 372  
 
 373  4
         final long low = ( ( ( value1 & 0xff ) << 0 ) +
 374  
                      ( ( value2 & 0xff ) << 8 ) +
 375  
                      ( ( value3 & 0xff ) << 16 ) );
 376  
 
 377  4
         final long high = value4 & 0xff;
 378  
 
 379  4
         return (high << 24) + (0xffffffffL & low); 
 380  
     }
 381  
 
 382  
     /**
 383  
      * Writes a "long" value to an OutputStream. The value is
 384  
      * converted to the opposed endian system while writing.
 385  
      * @param output target OutputStream
 386  
      * @param value value to write
 387  
      * @throws IOException in case of an I/O problem
 388  
      */
 389  
     public static void writeSwappedLong(final OutputStream output, final long value)
 390  
         throws IOException
 391  
     {
 392  4
         output.write( (byte)( ( value >> 0 ) & 0xff ) );
 393  4
         output.write( (byte)( ( value >> 8 ) & 0xff ) );
 394  4
         output.write( (byte)( ( value >> 16 ) & 0xff ) );
 395  4
         output.write( (byte)( ( value >> 24 ) & 0xff ) );
 396  4
         output.write( (byte)( ( value >> 32 ) & 0xff ) );
 397  4
         output.write( (byte)( ( value >> 40 ) & 0xff ) );
 398  4
         output.write( (byte)( ( value >> 48 ) & 0xff ) );
 399  4
         output.write( (byte)( ( value >> 56 ) & 0xff ) );
 400  4
     }
 401  
 
 402  
     /**
 403  
      * Reads a "long" value from an InputStream. The value is
 404  
      * converted to the opposed endian system while reading.
 405  
      * @param input source InputStream
 406  
      * @return the value just read
 407  
      * @throws IOException in case of an I/O problem
 408  
      */
 409  
     public static long readSwappedLong(final InputStream input)
 410  
         throws IOException
 411  
     {
 412  10
         final byte[] bytes = new byte[8];
 413  74
         for ( int i=0; i<8; i++ ) {
 414  66
             bytes[i] = (byte) read( input );
 415  
         }
 416  8
         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(final OutputStream output, final float value)
 427  
         throws IOException
 428  
     {
 429  2
         writeSwappedInteger( output, Float.floatToIntBits( value ) );
 430  2
     }
 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(final InputStream input)
 440  
         throws IOException
 441  
     {
 442  4
         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(final OutputStream output, final double value)
 453  
         throws IOException
 454  
     {
 455  2
         writeSwappedLong( output, Double.doubleToLongBits( value ) );
 456  2
     }
 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(final InputStream input)
 466  
         throws IOException
 467  
     {
 468  6
         return Double.longBitsToDouble( readSwappedLong( input ) );
 469  
     }
 470  
 
 471  
     /**
 472  
      * Reads the next byte from the input stream.
 473  
      * @param input  the stream
 474  
      * @return the byte
 475  
      * @throws IOException if the end of file is reached
 476  
      */
 477  
     private static int read(final InputStream input)
 478  
         throws IOException
 479  
     {
 480  142
         final int value = input.read();
 481  
 
 482  142
         if( -1 == value ) {
 483  2
             throw new EOFException( "Unexpected EOF reached" );
 484  
         }
 485  
 
 486  140
         return value;
 487  
     }
 488  
 }