Class BinaryFunctions
java.lang.Object
org.apache.commons.imaging.common.BinaryFunctions
Convenience methods for various binary and I/O operations.
- 
Method SummaryModifier and TypeMethodDescriptionstatic intcharsToQuad(char c1, char c2, char c3, char c4) static booleancompareBytes(byte[] a, int aStart, byte[] b, int bStart, int length) static byte[]copyOfRange(byte[] original, int from, int count) Copies the specified range of the specified array into a new array.static byte[]copyOfStart(byte[] original, int count) Copies the start of the specified array into a new array.static byte[]getBytes(RandomAccessFile raf, long pos, int length, String exception) static intFinds the index of the first 0 in the array starting at the given index.static intFinds the index of the first 0 in the array starting at the given index.static voidlogByteBits(String msg, byte i) static voidlogCharQuad(String msg, int i) static voidprintCharQuad(PrintWriter pw, String msg, int i) static byte[]quadsToByteArray(int quad) Convert a quad into a byte array.static intread2Bytes(String name, InputStream in, String exception, ByteOrder byteOrder) static intread3Bytes(String name, InputStream in, String exception, ByteOrder byteOrder) static intread4Bytes(String name, InputStream in, String exception, ByteOrder byteOrder) static longread8Bytes(String name, InputStream in, String exception, ByteOrder byteOrder) Reads eight bytes from the specified input stream, adjust for byte order, and return a long integer.static voidreadAndVerifyBytes(InputStream in, byte[] expected, String exception) static voidreadAndVerifyBytes(InputStream in, BinaryConstant expected, String exception) static bytereadByte(String name, InputStream in, String exceptionMessage) static byte[]readBytes(InputStream in, int count) static byte[]readBytes(String name, InputStream in, int length) static byte[]readBytes(String name, InputStream in, int length, String exception) static byte[]remainingBytes(String name, byte[] bytes, int count) static booleansearchQuad(int quad, InputStream in) Consumes theInputStream(without closing it) searching for a quad.static longskipBytes(InputStream in, long skip) static longskipBytes(InputStream in, long skip, String exMessage) static booleanstartsWith(byte[] buffer, byte[] search) 
- 
Method Details- 
charsToQuad
- 
compareBytes
- 
copyOfRangeCopies the specified range of the specified array into a new array.- Parameters:
- original- the array from which a range is to be copied.
- from- the initial index of the range to be copied, inclusive.
- count- the amount of bytes to copy.
- Returns:
- a new array containing the specified range from the original array, truncated or padded with zeros to obtain the required length.
 
- 
copyOfStartCopies the start of the specified array into a new array.- Parameters:
- original- the array from which a range is to be copied.
- count- the amount of bytes to copy.
- Returns:
- a new array containing the specified range from the original array, truncated or padded with zeros to obtain the required length.
 
- 
getBytespublic static byte[] getBytes(RandomAccessFile raf, long pos, int length, String exception) throws IOException - Throws:
- IOException
 
- 
indexOf0Finds the index of the first 0 in the array starting at the given index.- Parameters:
- src- the array to search for the object, may be- null
- start- the index to start searching at
- message- The ImagingException message if 0 is not found.
- Returns:
- the index of the value within the array,
- Throws:
- ImagingException- Thrown if 0 is not found.
 
- 
indexOf0Finds the index of the first 0 in the array starting at the given index.- Parameters:
- src- the array to search for the object, may be- null
- message- The ImagingException message if 0 is not found.
- Returns:
- the index of the value within the array,
- Throws:
- ImagingException- Thrown if 0 is not found.
 
- 
logByteBits
- 
logCharQuad
- 
printCharQuad
- 
quadsToByteArrayConvert a quad into a byte array.- Parameters:
- quad- quad
- Returns:
- a byte array
 
- 
read2Bytespublic static int read2Bytes(String name, InputStream in, String exception, ByteOrder byteOrder) throws IOException - Throws:
- IOException
 
- 
read3Bytespublic static int read3Bytes(String name, InputStream in, String exception, ByteOrder byteOrder) throws IOException - Throws:
- IOException
 
- 
read4Bytespublic static int read4Bytes(String name, InputStream in, String exception, ByteOrder byteOrder) throws IOException - Throws:
- IOException
 
- 
read8Bytespublic static long read8Bytes(String name, InputStream in, String exception, ByteOrder byteOrder) throws IOException Reads eight bytes from the specified input stream, adjust for byte order, and return a long integer.- Parameters:
- name- a descriptive identifier used for diagnostic purposes
- in- a valid input stream
- exception- application-defined message to be used for constructing an exception if an error condition is triggered.
- byteOrder- the order in which the InputStream marshals data
- Returns:
- a long integer interpreted from next 8 bytes in the InputStream
- Throws:
- IOException- in the event of a non-recoverable error, such as an attempt to read past the end of file.
 
- 
readAndVerifyBytespublic static void readAndVerifyBytes(InputStream in, BinaryConstant expected, String exception) throws ImagingException, IOException - Throws:
- ImagingException
- IOException
 
- 
readAndVerifyBytespublic static void readAndVerifyBytes(InputStream in, byte[] expected, String exception) throws ImagingException, IOException - Throws:
- ImagingException
- IOException
 
- 
readBytepublic static byte readByte(String name, InputStream in, String exceptionMessage) throws IOException - Throws:
- IOException
 
- 
readBytes- Throws:
- IOException
 
- 
readBytes- Throws:
- IOException
 
- 
readBytespublic static byte[] readBytes(String name, InputStream in, int length, String exception) throws IOException - Throws:
- IOException
 
- 
remainingBytes
- 
searchQuadConsumes theInputStream(without closing it) searching for a quad. It will stop either when the quad is found, or when there are no more bytes in the input stream.Returns trueif it found the quad, andfalseotherwise.- Parameters:
- quad- a quad (the needle)
- in- an input stream (the haystack)
- Returns:
- trueif it found the quad, and- falseotherwise
- Throws:
- IOException- if it fails to read from the given input stream
 
- 
skipBytes- Throws:
- IOException
 
- 
skipBytes- Throws:
- IOException
 
- 
startsWith
 
-