Package loci.poi.util
Class LittleEndian
- java.lang.Object
-
- loci.poi.util.LittleEndian
-
- All Implemented Interfaces:
LittleEndianConsts
public class LittleEndian extends Object implements LittleEndianConsts
a utility class for handling little-endian numbers, which the 80x86 world is replete with. The methods are all static, and input/output is from/to byte arrays, or from InputStreams.- Author:
- Marc Johnson (mjohnson at apache dot org), Andrew Oliver (acoliver at apache dot org)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LittleEndian.BufferUnderrunException
Exception to handle buffer underruns
-
Field Summary
-
Fields inherited from interface loci.poi.util.LittleEndianConsts
BYTE_SIZE, DOUBLE_SIZE, INT_SIZE, LONG_SIZE, SHORT_SIZE
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]
getByteArray(byte[] data, int offset, int size)
Copy a portion of a byte arraystatic double
getDouble(byte[] data)
get a double value from the beginning of a byte arraystatic double
getDouble(byte[] data, int offset)
get a double value from a byte array, reads it in little endian format then converts the resulting revolting IEEE 754 (curse them) floating point number to a happy java doublestatic int
getInt(byte[] data)
get an int value from the beginning of a byte arraystatic int
getInt(byte[] data, int offset)
get an int value from a byte arraystatic long
getLong(byte[] data)
get a long value from the beginning of a byte arraystatic long
getLong(byte[] data, int offset)
get a long value from a byte arraystatic short
getShort(byte[] data)
get a short value from the beginning of a byte arraystatic short
getShort(byte[] data, int offset)
get a short value from a byte arraystatic short[]
getShortArray(byte[] data, int offset)
get a short array from a byte array.static short[]
getSimpleShortArray(byte[] data, int offset, int size)
get a short array from a byte array.static long
getUInt(byte[] data)
get an unsigned int value from a byte arraystatic long
getUInt(byte[] data, int offset)
get an unsigned int value from a byte arraystatic long
getULong(byte[] data, int offset)
Gets an unsigned int value (8 bytes) from a byte array.static int
getUnsignedByte(byte[] data)
get the unsigned value of a byte.static int
getUnsignedByte(byte[] data, int offset)
get the unsigned value of a byte.static int
getUShort(byte[] data)
get an unsigned short value from the beginning of a byte arraystatic int
getUShort(byte[] data, int offset)
get an unsigned short value from a byte arraystatic void
putDouble(byte[] data, double value)
put a double value into beginning of a byte arraystatic void
putDouble(byte[] data, int offset, double value)
put a double value into a byte arraystatic void
putInt(byte[] data, int value)
put an int value into beginning of a byte arraystatic void
putInt(byte[] data, int offset, int value)
put an int value into a byte arraystatic void
putLong(byte[] data, int offset, long value)
put a long value into a byte arraystatic void
putLong(byte[] data, long value)
put a long value into beginning of a byte arraystatic void
putShort(byte[] data, int offset, short value)
put a short value into a byte arraystatic void
putShort(byte[] data, short value)
put a short value into beginning of a byte arraystatic void
putShortArray(byte[] data, int offset, short[] value)
put a array of shorts into a byte arraystatic void
putUShort(byte[] data, int offset, int value)
put an unsigned short value into a byte arraystatic byte[]
readFromStream(InputStream stream, int size)
Read the appropriate number of bytes from the stream and return them to the caller.static int
readInt(InputStream stream)
get an int value from an InputStreamstatic long
readLong(InputStream stream)
get a long value from an InputStreamstatic short
readShort(InputStream stream)
get a short value from an InputStreamstatic int
ubyteToInt(byte b)
Convert an 'unsigned' byte to an integer.
-
-
-
Method Detail
-
getShort
public static short getShort(byte[] data, int offset)
get a short value from a byte array- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte array- Returns:
- the short (16-bit) value
-
getUShort
public static int getUShort(byte[] data, int offset)
get an unsigned short value from a byte array- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte array- Returns:
- the unsigned short (16-bit) value in an integer
-
getSimpleShortArray
public static short[] getSimpleShortArray(byte[] data, int offset, int size)
get a short array from a byte array.- Parameters:
data
- Description of the Parameteroffset
- Description of the Parametersize
- Description of the Parameter- Returns:
- The simpleShortArray value
-
getShortArray
public static short[] getShortArray(byte[] data, int offset)
get a short array from a byte array. The short array is assumed to start with a word describing the length of the array.- Parameters:
data
- Description of the Parameteroffset
- Description of the Parameter- Returns:
- The shortArray value
-
getShort
public static short getShort(byte[] data)
get a short value from the beginning of a byte array- Parameters:
data
- the byte array- Returns:
- the short (16-bit) value
-
getUShort
public static int getUShort(byte[] data)
get an unsigned short value from the beginning of a byte array- Parameters:
data
- the byte array- Returns:
- the unsigned short (16-bit) value in an int
-
getInt
public static int getInt(byte[] data, int offset)
get an int value from a byte array- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte array- Returns:
- the int (32-bit) value
-
getInt
public static int getInt(byte[] data)
get an int value from the beginning of a byte array- Parameters:
data
- the byte array- Returns:
- the int (32-bit) value
-
getUInt
public static long getUInt(byte[] data, int offset)
get an unsigned int value from a byte array- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte array- Returns:
- the unsigned int (32-bit) value in a long
-
getUInt
public static long getUInt(byte[] data)
get an unsigned int value from a byte array- Parameters:
data
- the byte array- Returns:
- the unsigned int (32-bit) value in a long
-
getLong
public static long getLong(byte[] data, int offset)
get a long value from a byte array- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte array- Returns:
- the long (64-bit) value
-
getLong
public static long getLong(byte[] data)
get a long value from the beginning of a byte array- Parameters:
data
- the byte array- Returns:
- the long (64-bit) value
-
getDouble
public static double getDouble(byte[] data, int offset)
get a double value from a byte array, reads it in little endian format then converts the resulting revolting IEEE 754 (curse them) floating point number to a happy java double- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte array- Returns:
- the double (64-bit) value
-
getDouble
public static double getDouble(byte[] data)
get a double value from the beginning of a byte array- Parameters:
data
- the byte array- Returns:
- the double (64-bit) value
-
putShort
public static void putShort(byte[] data, int offset, short value)
put a short value into a byte array- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte arrayvalue
- the short (16-bit) value
-
putShortArray
public static void putShortArray(byte[] data, int offset, short[] value)
put a array of shorts into a byte array- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte arrayvalue
- the short array
-
putUShort
public static void putUShort(byte[] data, int offset, int value)
put an unsigned short value into a byte array- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte arrayvalue
- the short (16-bit) value- Throws:
ArrayIndexOutOfBoundsException
- may be thrown
-
putShort
public static void putShort(byte[] data, short value)
put a short value into beginning of a byte array- Parameters:
data
- the byte arrayvalue
- the short (16-bit) value
-
putInt
public static void putInt(byte[] data, int offset, int value)
put an int value into a byte array- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte arrayvalue
- the int (32-bit) value
-
putInt
public static void putInt(byte[] data, int value)
put an int value into beginning of a byte array- Parameters:
data
- the byte arrayvalue
- the int (32-bit) value
-
putLong
public static void putLong(byte[] data, int offset, long value)
put a long value into a byte array- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte arrayvalue
- the long (64-bit) value
-
putLong
public static void putLong(byte[] data, long value)
put a long value into beginning of a byte array- Parameters:
data
- the byte arrayvalue
- the long (64-bit) value
-
putDouble
public static void putDouble(byte[] data, int offset, double value)
put a double value into a byte array- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte arrayvalue
- the double (64-bit) value
-
putDouble
public static void putDouble(byte[] data, double value)
put a double value into beginning of a byte array- Parameters:
data
- the byte arrayvalue
- the double (64-bit) value
-
readShort
public static short readShort(InputStream stream) throws IOException, LittleEndian.BufferUnderrunException
get a short value from an InputStream- Parameters:
stream
- the InputStream from which the short is to be read- Returns:
- the short (16-bit) value
- Throws:
IOException
- will be propagated back to the callerLittleEndian.BufferUnderrunException
- if the stream cannot provide enough bytes
-
readInt
public static int readInt(InputStream stream) throws IOException, LittleEndian.BufferUnderrunException
get an int value from an InputStream- Parameters:
stream
- the InputStream from which the int is to be read- Returns:
- the int (32-bit) value
- Throws:
IOException
- will be propagated back to the callerLittleEndian.BufferUnderrunException
- if the stream cannot provide enough bytes
-
readLong
public static long readLong(InputStream stream) throws IOException, LittleEndian.BufferUnderrunException
get a long value from an InputStream- Parameters:
stream
- the InputStream from which the long is to be read- Returns:
- the long (64-bit) value
- Throws:
IOException
- will be propagated back to the callerLittleEndian.BufferUnderrunException
- if the stream cannot provide enough bytes
-
readFromStream
public static byte[] readFromStream(InputStream stream, int size) throws IOException, LittleEndian.BufferUnderrunException
Read the appropriate number of bytes from the stream and return them to the caller.However, for the purposes of the POI project, this risk is deemed negligible. It is, however, so noted.
- Parameters:
stream
- the InputStream we're reading fromsize
- the number of bytes to read; in 99.99% of cases, this will be SHORT_SIZE, INT_SIZE, or LONG_SIZE -- but it doesn't have to be.- Returns:
- the byte array containing the required number of bytes. The array will contain all zero's on end of stream
- Throws:
IOException
- will be propagated back to the callerLittleEndian.BufferUnderrunException
- if the stream cannot provide enough bytes
-
ubyteToInt
public static int ubyteToInt(byte b)
Convert an 'unsigned' byte to an integer. ie, don't carry across the sign.- Parameters:
b
- Description of the Parameter- Returns:
- Description of the Return Value
-
getUnsignedByte
public static int getUnsignedByte(byte[] data, int offset)
get the unsigned value of a byte.- Parameters:
data
- the byte array.offset
- a starting offset into the byte array.- Returns:
- the unsigned value of the byte as a 32 bit integer
-
getUnsignedByte
public static int getUnsignedByte(byte[] data)
get the unsigned value of a byte.- Parameters:
data
- the byte array- Returns:
- the unsigned value of the byte as a 32 bit integer
-
getByteArray
public static byte[] getByteArray(byte[] data, int offset, int size)
Copy a portion of a byte array- Parameters:
data
- the original byte arrayoffset
- Where to start copying from.size
- Number of bytes to copy.- Returns:
- The byteArray value
- Throws:
IndexOutOfBoundsException
- - if copying would cause access of data outside array bounds.
-
getULong
public static long getULong(byte[] data, int offset)
Gets an unsigned int value (8 bytes) from a byte array.
- Parameters:
data
- the byte arrayoffset
- a starting offset into the byte array- Returns:
- the unsigned int (32-bit) value in a long
-
-