Package org.docx4j.org.apache.poi.util
Class LittleEndian
java.lang.Object
org.docx4j.org.apache.poi.util.LittleEndian
- All Implemented Interfaces:
LittleEndianConsts
public class LittleEndian extends java.lang.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 classLittleEndian.BufferUnderrunExceptionException to handle buffer underruns -
Field Summary
Fields inherited from interface org.docx4j.org.apache.poi.util.LittleEndianConsts
BYTE_SIZE, DOUBLE_SIZE, INT_SIZE, LONG_SIZE, SHORT_SIZE -
Method Summary
Modifier and Type Method Description static byte[]getByteArray(byte[] data, int offset, int size)Copy a portion of a byte arraystatic doublegetDouble(byte[] data)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 doublegetDouble(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 floatgetFloat(byte[] data)get a float 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 floatstatic floatgetFloat(byte[] data, int offset)get a float 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 floatstatic intgetInt(byte[] data)get an int value from the beginning of a byte arraystatic intgetInt(byte[] data, int offset)get an int value from a byte arraystatic longgetLong(byte[] data)get a long value from a byte arraystatic longgetLong(byte[] data, int offset)get a long value from a byte arraystatic shortgetShort(byte[] data)get a short value from the beginning of a byte arraystatic shortgetShort(byte[] data, int offset)get a short value from a byte arraystatic short[]getShortArray(byte[] data, int offset, int size)Read short arraystatic shortgetUByte(byte[] data)get the unsigned value of a byte.static shortgetUByte(byte[] data, int offset)get the unsigned value of a byte.static longgetUInt(byte[] data)get an unsigned int value from a byte arraystatic longgetUInt(byte[] data, int offset)get an unsigned int value from a byte arraystatic intgetUnsignedByte(byte[] data, int offset)Deprecated.static intgetUShort(byte[] data)get an unsigned short value from the beginning of a byte arraystatic intgetUShort(byte[] data, int offset)get an unsigned short value from a byte arraystatic voidputByte(byte[] data, int offset, int value)executes:static voidputDouble(byte[] data, int offset, double value)put a double value into a byte arraystatic voidputDouble(double value, java.io.OutputStream outputStream)put a double value into a byte arraystatic voidputFloat(byte[] data, int offset, float value)put a float value into a byte arraystatic voidputFloat(float value, java.io.OutputStream outputStream)put a float value into a byte arraystatic voidputInt(byte[] data, int value)Deprecated.UseputInt(byte[], int, int)insteadstatic voidputInt(byte[] data, int offset, int value)put an int value into a byte arraystatic voidputInt(int value, java.io.OutputStream outputStream)Put int into output streamstatic voidputLong(byte[] data, int offset, long value)put a long value into a byte arraystatic voidputLong(long value, java.io.OutputStream outputStream)Put long into output streamstatic voidputShort(byte[] data, int offset, short value)put a short value into a byte arraystatic voidputShort(byte[] data, short value)Deprecated.UseputShort(byte[], int, short)insteadstatic voidputShort(java.io.OutputStream outputStream, short value)Put signed short into output streamstatic voidputShortArray(byte[] data, int startOffset, short[] value)Stores short array in bufferstatic voidputUByte(byte[] data, int offset, short value)put an unsigned byte value into a byte arraystatic voidputUInt(byte[] data, int offset, long value)put an unsigned int value into a byte arraystatic voidputUInt(byte[] data, long value)Deprecated.UseputUInt(byte[], int, long)insteadstatic voidputUInt(long value, java.io.OutputStream outputStream)Put unsigned int into output streamstatic voidputUShort(byte[] data, int offset, int value)put an unsigned short value into a byte arraystatic voidputUShort(int value, java.io.OutputStream outputStream)Put unsigned short into output streamstatic intreadInt(java.io.InputStream stream)get an int value from an InputStreamstatic longreadLong(java.io.InputStream stream)get a long value from an InputStreamstatic shortreadShort(java.io.InputStream stream)get a short value from an InputStreamstatic longreadUInt(java.io.InputStream stream)get an unsigned int value from an InputStreamstatic intreadUShort(java.io.InputStream stream)static intubyteToInt(byte b)Convert an 'unsigned' byte to an integer.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Method Details
-
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:
java.lang.IndexOutOfBoundsException- - if copying would cause access of data outside array bounds.
-
getDouble
public static double getDouble(byte[] data)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 array- Returns:
- the double (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
-
getFloat
public static float getFloat(byte[] data)get a float 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 float- Parameters:
data- the byte array- Returns:
- the double (64-bit) value
-
getFloat
public static float getFloat(byte[] data, int offset)get a float 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 float- Parameters:
data- the byte arrayoffset- a starting offset into the byte array- Returns:
- the double (64-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
-
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
-
getLong
public static long getLong(byte[] data)get a long value from a byte array- Parameters:
data- the byte array- Returns:
- the long (64-bit) value
-
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
-
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
-
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
-
getShortArray
public static short[] getShortArray(byte[] data, int offset, int size)Read short array- Parameters:
data- the original byte arrayoffset- Where to start copying from.size- Number of bytes to copy.- Throws:
java.lang.IndexOutOfBoundsException- - if read would cause access of data outside array bounds.
-
getUByte
public static short getUByte(byte[] data)get the unsigned value of a byte.- Parameters:
data- the byte array.- Returns:
- the unsigned value of the byte as a 16 bit short
-
getUByte
public static short getUByte(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 16 bit short
-
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
-
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
-
getUnsignedByte
@Deprecated public static int getUnsignedByte(byte[] data, int offset)Deprecated.UsegetUByte(byte[], int)insteadget 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
-
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
-
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
-
putByte
public static void putByte(byte[] data, int offset, int value)executes:data[offset] = (byte)value;Added for consistency with other put~() methods -
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(double value, java.io.OutputStream outputStream) throws java.io.IOExceptionput a double value into a byte array- Parameters:
value- the double (64-bit) valueoutputStream- output stream- Throws:
java.io.IOException- if an I/O error occurs
-
putFloat
public static void putFloat(byte[] data, int offset, float value)put a float value into a byte array- Parameters:
data- the byte arrayoffset- a starting offset into the byte arrayvalue- the float (32-bit) value
-
putFloat
public static void putFloat(float value, java.io.OutputStream outputStream) throws java.io.IOExceptionput a float value into a byte array- Parameters:
value- the float (32-bit) valueoutputStream- output stream- Throws:
java.io.IOException- if an I/O error occurs
-
putInt
@Deprecated public static void putInt(byte[] data, int value)Deprecated.UseputInt(byte[], int, int)insteadput an int value into beginning of a byte array- Parameters:
data- the byte arrayvalue- the int (32-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(int value, java.io.OutputStream outputStream) throws java.io.IOExceptionPut int into output stream- Parameters:
value- the int (32-bit) valueoutputStream- output stream- Throws:
java.io.IOException- if an I/O error occurs
-
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(long value, java.io.OutputStream outputStream) throws java.io.IOExceptionPut long into output stream- Parameters:
value- the long (64-bit) valueoutputStream- output stream- Throws:
java.io.IOException- if an I/O error occurs
-
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
-
putShort
@Deprecated public static void putShort(byte[] data, short value)Deprecated.UseputShort(byte[], int, short)insteadput a short value into beginning of a byte array- Parameters:
data- the byte arrayvalue- the short (16-bit) value
-
putShort
public static void putShort(java.io.OutputStream outputStream, short value) throws java.io.IOExceptionPut signed short into output stream- Parameters:
value- the short (16-bit) valueoutputStream- output stream- Throws:
java.io.IOException- if an I/O error occurs
-
putShortArray
public static void putShortArray(byte[] data, int startOffset, short[] value)Stores short array in buffer- Parameters:
data- the byte arraystartOffset- a starting offset into the byte arrayvalue- the short (16-bit) values
-
putUByte
public static void putUByte(byte[] data, int offset, short value)put an unsigned byte value into a byte array- Parameters:
data- the byte arrayoffset- a starting offset into the byte arrayvalue- the short (16-bit) value- Throws:
java.lang.ArrayIndexOutOfBoundsException- may be thrown
-
putUInt
public static void putUInt(byte[] data, int offset, long value)put an unsigned int value into a byte array- Parameters:
data- the byte arrayoffset- a starting offset into the byte arrayvalue- the int (32-bit) value- Throws:
java.lang.ArrayIndexOutOfBoundsException- may be thrown
-
putUInt
@Deprecated public static void putUInt(byte[] data, long value)Deprecated.UseputUInt(byte[], int, long)insteadput an unsigned int value into beginning of a byte array- Parameters:
data- the byte arrayvalue- the int (32-bit) value
-
putUInt
public static void putUInt(long value, java.io.OutputStream outputStream) throws java.io.IOExceptionPut unsigned int into output stream- Parameters:
value- the int (32-bit) valueoutputStream- output stream- Throws:
java.io.IOException- if an I/O error occurs
-
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:
java.lang.ArrayIndexOutOfBoundsException- may be thrown
-
putUShort
public static void putUShort(int value, java.io.OutputStream outputStream) throws java.io.IOExceptionPut unsigned short into output stream- Parameters:
value- the unsigned short (16-bit) valueoutputStream- output stream- Throws:
java.io.IOException- if an I/O error occurs
-
readInt
public static int readInt(java.io.InputStream stream) throws java.io.IOException, LittleEndian.BufferUnderrunExceptionget 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:
java.io.IOException- will be propagated back to the callerLittleEndian.BufferUnderrunException- if the stream cannot provide enough bytes
-
readUInt
public static long readUInt(java.io.InputStream stream) throws java.io.IOException, LittleEndian.BufferUnderrunExceptionget an unsigned int value from an InputStream- Parameters:
stream- the InputStream from which the int is to be read- Returns:
- the unsigned int (32-bit) value
- Throws:
java.io.IOException- will be propagated back to the callerLittleEndian.BufferUnderrunException- if the stream cannot provide enough bytes
-
readLong
public static long readLong(java.io.InputStream stream) throws java.io.IOException, LittleEndian.BufferUnderrunExceptionget 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:
java.io.IOException- will be propagated back to the callerLittleEndian.BufferUnderrunException- if the stream cannot provide enough bytes
-
readShort
public static short readShort(java.io.InputStream stream) throws java.io.IOException, LittleEndian.BufferUnderrunExceptionget 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:
java.io.IOException- will be propagated back to the callerLittleEndian.BufferUnderrunException- if the stream cannot provide enough bytes
-
readUShort
public static int readUShort(java.io.InputStream stream) throws java.io.IOException, LittleEndian.BufferUnderrunException- Throws:
java.io.IOExceptionLittleEndian.BufferUnderrunException
-
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
-
getUByte(byte[], int)instead