Class BinaryFunctions
java.lang.Object
org.apache.commons.imaging.common.BinaryFunctions
Convenience methods for various binary and I/O operations.
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
charsToQuad
(char c1, char c2, char c3, char c4) static boolean
compareBytes
(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 int
Finds the index of the first 0 in the array starting at the given index.static int
Finds the index of the first 0 in the array starting at the given index.static void
logByteBits
(String msg, byte i) static void
logCharQuad
(String msg, int i) static void
printCharQuad
(PrintWriter pw, String msg, int i) static byte[]
quadsToByteArray
(int quad) Convert a quad into a byte array.static int
read2Bytes
(String name, InputStream in, String exception, ByteOrder byteOrder) static int
read3Bytes
(String name, InputStream in, String exception, ByteOrder byteOrder) static int
read4Bytes
(String name, InputStream in, String exception, ByteOrder byteOrder) static long
read8Bytes
(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 void
readAndVerifyBytes
(InputStream in, byte[] expected, String exception) static void
readAndVerifyBytes
(InputStream in, BinaryConstant expected, String exception) static byte
readByte
(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 boolean
searchQuad
(int quad, InputStream in) Consumes theInputStream
(without closing it) searching for a quad.static long
skipBytes
(InputStream in, long skip) static long
skipBytes
(InputStream in, long skip, String exMessage) static boolean
startsWith
(byte[] buffer, byte[] search)
-
Method Details
-
charsToQuad
-
compareBytes
-
copyOfRange
Copies 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.
-
copyOfStart
Copies 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.
-
getBytes
public static byte[] getBytes(RandomAccessFile raf, long pos, int length, String exception) throws IOException - Throws:
IOException
-
indexOf0
Finds the index of the first 0 in the array starting at the given index.- Parameters:
src
- the array to search for the object, may benull
start
- the index to start searching atmessage
- 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.
-
indexOf0
Finds the index of the first 0 in the array starting at the given index.- Parameters:
src
- the array to search for the object, may benull
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
-
quadsToByteArray
Convert a quad into a byte array.- Parameters:
quad
- quad- Returns:
- a byte array
-
read2Bytes
public static int read2Bytes(String name, InputStream in, String exception, ByteOrder byteOrder) throws IOException - Throws:
IOException
-
read3Bytes
public static int read3Bytes(String name, InputStream in, String exception, ByteOrder byteOrder) throws IOException - Throws:
IOException
-
read4Bytes
public static int read4Bytes(String name, InputStream in, String exception, ByteOrder byteOrder) throws IOException - Throws:
IOException
-
read8Bytes
public 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 purposesin
- a valid input streamexception
- 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.
-
readAndVerifyBytes
public static void readAndVerifyBytes(InputStream in, BinaryConstant expected, String exception) throws ImagingException, IOException - Throws:
ImagingException
IOException
-
readAndVerifyBytes
public static void readAndVerifyBytes(InputStream in, byte[] expected, String exception) throws ImagingException, IOException - Throws:
ImagingException
IOException
-
readByte
public static byte readByte(String name, InputStream in, String exceptionMessage) throws IOException - Throws:
IOException
-
readBytes
- Throws:
IOException
-
readBytes
- Throws:
IOException
-
readBytes
public static byte[] readBytes(String name, InputStream in, int length, String exception) throws IOException - Throws:
IOException
-
remainingBytes
-
searchQuad
Consumes 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
true
if it found the quad, andfalse
otherwise.- Parameters:
quad
- a quad (the needle)in
- an input stream (the haystack)- Returns:
true
if it found the quad, andfalse
otherwise- Throws:
IOException
- if it fails to read from the given input stream
-
skipBytes
- Throws:
IOException
-
skipBytes
- Throws:
IOException
-
startsWith
-