FileDocCategorySizeDatePackage
EndianUtils.javaAPI DocAndroid 1.5 API18340Wed May 06 22:42:46 BST 2009org.apache.commons.io

EndianUtils

public class EndianUtils extends Object
Utility code for dealing with different endian systems.

Different computer architectures adopt different conventions for byte ordering. In so-called "Little Endian" architectures (eg Intel), the low-order byte is stored in memory at the lowest address, and subsequent bytes at higher addresses. For "Big Endian" architectures (eg Motorola), the situation is reversed. This class helps you solve this incompatability.

Origin of code: Excalibur

author
Peter Donald
version
$Id: EndianUtils.java 539632 2007-05-18 23:37:59Z bayard $
see
org.apache.commons.io.input.SwappedDataInputStream

Fields Summary
Constructors Summary
public EndianUtils()
Instances should NOT be constructed in standard programming.

        super();
    
Methods Summary
private static intread(java.io.InputStream input)
Reads the next byte from the input stream.

param
input the stream
return
the byte
throws
IOException if the end of file is reached

        int value = input.read();

        if( -1 == value ) {
            throw new EOFException( "Unexpected EOF reached" );
        }

        return value;
    
public static doublereadSwappedDouble(byte[] data, int offset)
Reads a "double" value from a byte array at a given offset. The value is converted to the opposed endian system while reading.

param
data source byte array
param
offset starting offset in the byte array
return
the value read

        return Double.longBitsToDouble( readSwappedLong( data, offset ) );
    
public static doublereadSwappedDouble(java.io.InputStream input)
Reads a "double" value from an InputStream. The value is converted to the opposed endian system while reading.

param
input source InputStream
return
the value just read
throws
IOException in case of an I/O problem

        return Double.longBitsToDouble( readSwappedLong( input ) );
    
public static floatreadSwappedFloat(byte[] data, int offset)
Reads a "float" value from a byte array at a given offset. The value is converted to the opposed endian system while reading.

param
data source byte array
param
offset starting offset in the byte array
return
the value read

        return Float.intBitsToFloat( readSwappedInteger( data, offset ) );
    
public static floatreadSwappedFloat(java.io.InputStream input)
Reads a "float" value from an InputStream. The value is converted to the opposed endian system while reading.

param
input source InputStream
return
the value just read
throws
IOException in case of an I/O problem

        return Float.intBitsToFloat( readSwappedInteger( input ) );
    
public static intreadSwappedInteger(byte[] data, int offset)
Reads a "int" value from a byte array at a given offset. The value is converted to the opposed endian system while reading.

param
data source byte array
param
offset starting offset in the byte array
return
the value read

        return ( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
            ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
            ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
            ( ( data[ offset + 3 ] & 0xff ) << 24 ) );
    
public static intreadSwappedInteger(java.io.InputStream input)
Reads a "int" value from an InputStream. The value is converted to the opposed endian system while reading.

param
input source InputStream
return
the value just read
throws
IOException in case of an I/O problem

        int value1 = read( input );
        int value2 = read( input );
        int value3 = read( input );
        int value4 = read( input );

        return ( ( value1 & 0xff ) << 0 ) +
            ( ( value2 & 0xff ) << 8 ) +
            ( ( value3 & 0xff ) << 16 ) +
            ( ( value4 & 0xff ) << 24 );
    
public static longreadSwappedLong(byte[] data, int offset)
Reads a "long" value from a byte array at a given offset. The value is converted to the opposed endian system while reading.

param
data source byte array
param
offset starting offset in the byte array
return
the value read

        long low = 
            ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
            ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
            ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
            ( ( data[ offset + 3 ] & 0xff ) << 24 );
        long high = 
            ( ( data[ offset + 4 ] & 0xff ) << 0 ) +
            ( ( data[ offset + 5 ] & 0xff ) << 8 ) +
            ( ( data[ offset + 6 ] & 0xff ) << 16 ) +
            ( ( data[ offset + 7 ] & 0xff ) << 24 );
        return (high << 32) + (0xffffffffL & low); 
    
public static longreadSwappedLong(java.io.InputStream input)
Reads a "long" value from an InputStream. The value is converted to the opposed endian system while reading.

param
input source InputStream
return
the value just read
throws
IOException in case of an I/O problem

        byte[] bytes = new byte[8];
        for ( int i=0; i<8; i++ ) {
            bytes[i] = (byte) read( input );
        }
        return readSwappedLong( bytes, 0 );
    
public static shortreadSwappedShort(java.io.InputStream input)
Reads a "short" value from an InputStream. The value is converted to the opposed endian system while reading.

param
input source InputStream
return
the value just read
throws
IOException in case of an I/O problem

        return (short)( ( ( read( input ) & 0xff ) << 0 ) +
            ( ( read( input ) & 0xff ) << 8 ) );
    
public static shortreadSwappedShort(byte[] data, int offset)
Reads a "short" value from a byte array at a given offset. The value is converted to the opposed endian system while reading.

param
data source byte array
param
offset starting offset in the byte array
return
the value read

        return (short)( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
            ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
    
public static longreadSwappedUnsignedInteger(byte[] data, int offset)
Reads an unsigned integer (32-bit) value from a byte array at a given offset. The value is converted to the opposed endian system while reading.

param
data source byte array
param
offset starting offset in the byte array
return
the value read

        long low = ( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
                     ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
                     ( ( data[ offset + 2 ] & 0xff ) << 16 ) );

        long high = data[ offset + 3 ] & 0xff;

        return (high << 24) + (0xffffffffL & low); 
    
public static longreadSwappedUnsignedInteger(java.io.InputStream input)
Reads a unsigned integer (32-bit) from an InputStream. The value is converted to the opposed endian system while reading.

param
input source InputStream
return
the value just read
throws
IOException in case of an I/O problem

        int value1 = read( input );
        int value2 = read( input );
        int value3 = read( input );
        int value4 = read( input );

        long low = ( ( ( value1 & 0xff ) << 0 ) +
                     ( ( value2 & 0xff ) << 8 ) +
                     ( ( value3 & 0xff ) << 16 ) );

        long high = value4 & 0xff;

        return (high << 24) + (0xffffffffL & low); 
    
public static intreadSwappedUnsignedShort(java.io.InputStream input)
Reads a unsigned short (16-bit) from an InputStream. The value is converted to the opposed endian system while reading.

param
input source InputStream
return
the value just read
throws
IOException in case of an I/O problem

        int value1 = read( input );
        int value2 = read( input );

        return ( ( ( value1 & 0xff ) << 0 ) +
            ( ( value2 & 0xff ) << 8 ) );
    
public static intreadSwappedUnsignedShort(byte[] data, int offset)
Reads an unsigned short (16-bit) value from a byte array at a given offset. The value is converted to the opposed endian system while reading.

param
data source byte array
param
offset starting offset in the byte array
return
the value read

        return ( ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
            ( ( data[ offset + 1 ] & 0xff ) << 8 ) );
    
public static doubleswapDouble(double value)
Converts a "double" value between endian systems.

param
value value to convert
return
the converted value

        return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) );
    
public static floatswapFloat(float value)
Converts a "float" value between endian systems.

param
value value to convert
return
the converted value

        return Float.intBitsToFloat( swapInteger( Float.floatToIntBits( value ) ) );
    
public static intswapInteger(int value)
Converts a "int" value between endian systems.

param
value value to convert
return
the converted value

        return
            ( ( ( value >> 0 ) & 0xff ) << 24 ) +
            ( ( ( value >> 8 ) & 0xff ) << 16 ) +
            ( ( ( value >> 16 ) & 0xff ) << 8 ) +
            ( ( ( value >> 24 ) & 0xff ) << 0 );
    
public static longswapLong(long value)
Converts a "long" value between endian systems.

param
value value to convert
return
the converted value

        return
            ( ( ( value >> 0 ) & 0xff ) << 56 ) +
            ( ( ( value >> 8 ) & 0xff ) << 48 ) +
            ( ( ( value >> 16 ) & 0xff ) << 40 ) +
            ( ( ( value >> 24 ) & 0xff ) << 32 ) +
            ( ( ( value >> 32 ) & 0xff ) << 24 ) +
            ( ( ( value >> 40 ) & 0xff ) << 16 ) +
            ( ( ( value >> 48 ) & 0xff ) << 8 ) +
            ( ( ( value >> 56 ) & 0xff ) << 0 );
    
public static shortswapShort(short value)
Converts a "short" value between endian systems.

param
value value to convert
return
the converted value

        return (short) ( ( ( ( value >> 0 ) & 0xff ) << 8 ) +
            ( ( ( value >> 8 ) & 0xff ) << 0 ) );
    
public static voidwriteSwappedDouble(byte[] data, int offset, double value)
Writes a "double" value to a byte array at a given offset. The value is converted to the opposed endian system while writing.

param
data target byte array
param
offset starting offset in the byte array
param
value value to write

        writeSwappedLong( data, offset, Double.doubleToLongBits( value ) );
    
public static voidwriteSwappedDouble(java.io.OutputStream output, double value)
Writes a "double" value to an OutputStream. The value is converted to the opposed endian system while writing.

param
output target OutputStream
param
value value to write
throws
IOException in case of an I/O problem

        writeSwappedLong( output, Double.doubleToLongBits( value ) );
    
public static voidwriteSwappedFloat(byte[] data, int offset, float value)
Writes a "float" value to a byte array at a given offset. The value is converted to the opposed endian system while writing.

param
data target byte array
param
offset starting offset in the byte array
param
value value to write

        writeSwappedInteger( data, offset, Float.floatToIntBits( value ) );
    
public static voidwriteSwappedFloat(java.io.OutputStream output, float value)
Writes a "float" value to an OutputStream. The value is converted to the opposed endian system while writing.

param
output target OutputStream
param
value value to write
throws
IOException in case of an I/O problem

        writeSwappedInteger( output, Float.floatToIntBits( value ) );
    
public static voidwriteSwappedInteger(byte[] data, int offset, int value)
Writes a "int" value to a byte array at a given offset. The value is converted to the opposed endian system while writing.

param
data target byte array
param
offset starting offset in the byte array
param
value value to write

        data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
        data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
        data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
        data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
    
public static voidwriteSwappedInteger(java.io.OutputStream output, int value)
Writes a "int" value to an OutputStream. The value is converted to the opposed endian system while writing.

param
output target OutputStream
param
value value to write
throws
IOException in case of an I/O problem

        output.write( (byte)( ( value >> 0 ) & 0xff ) );
        output.write( (byte)( ( value >> 8 ) & 0xff ) );
        output.write( (byte)( ( value >> 16 ) & 0xff ) );
        output.write( (byte)( ( value >> 24 ) & 0xff ) );
    
public static voidwriteSwappedLong(byte[] data, int offset, long value)
Writes a "long" value to a byte array at a given offset. The value is converted to the opposed endian system while writing.

param
data target byte array
param
offset starting offset in the byte array
param
value value to write

        data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
        data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );
        data[ offset + 2 ] = (byte)( ( value >> 16 ) & 0xff );
        data[ offset + 3 ] = (byte)( ( value >> 24 ) & 0xff );
        data[ offset + 4 ] = (byte)( ( value >> 32 ) & 0xff );
        data[ offset + 5 ] = (byte)( ( value >> 40 ) & 0xff );
        data[ offset + 6 ] = (byte)( ( value >> 48 ) & 0xff );
        data[ offset + 7 ] = (byte)( ( value >> 56 ) & 0xff );
    
public static voidwriteSwappedLong(java.io.OutputStream output, long value)
Writes a "long" value to an OutputStream. The value is converted to the opposed endian system while writing.

param
output target OutputStream
param
value value to write
throws
IOException in case of an I/O problem

        output.write( (byte)( ( value >> 0 ) & 0xff ) );
        output.write( (byte)( ( value >> 8 ) & 0xff ) );
        output.write( (byte)( ( value >> 16 ) & 0xff ) );
        output.write( (byte)( ( value >> 24 ) & 0xff ) );
        output.write( (byte)( ( value >> 32 ) & 0xff ) );
        output.write( (byte)( ( value >> 40 ) & 0xff ) );
        output.write( (byte)( ( value >> 48 ) & 0xff ) );
        output.write( (byte)( ( value >> 56 ) & 0xff ) );
    
public static voidwriteSwappedShort(java.io.OutputStream output, short value)
Writes a "short" value to an OutputStream. The value is converted to the opposed endian system while writing.

param
output target OutputStream
param
value value to write
throws
IOException in case of an I/O problem

        output.write( (byte)( ( value >> 0 ) & 0xff ) );
        output.write( (byte)( ( value >> 8 ) & 0xff ) );
    
public static voidwriteSwappedShort(byte[] data, int offset, short value)
Writes a "short" value to a byte array at a given offset. The value is converted to the opposed endian system while writing.

param
data target byte array
param
offset starting offset in the byte array
param
value value to write

        data[ offset + 0 ] = (byte)( ( value >> 0 ) & 0xff );
        data[ offset + 1 ] = (byte)( ( value >> 8 ) & 0xff );