Class BinaryFunctions
- java.lang.Object
-
- org.apache.commons.imaging.common.BinaryFunctions
-
public final class BinaryFunctions extends java.lang.ObjectConvenience methods for various binary and I/O operations.
-
-
Constructor Summary
Constructors Modifier Constructor Description privateBinaryFunctions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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(java.io.RandomAccessFile raf, long pos, int length, java.lang.String exception)static intindexOf0(byte[] src, int start, java.lang.String message)Finds the index of the first 0 in the array starting at the given index.static intindexOf0(byte[] src, java.lang.String message)Finds the index of the first 0 in the array starting at the given index.static voidlogByteBits(java.lang.String msg, byte i)static voidlogCharQuad(java.lang.String msg, int i)static voidprintCharQuad(java.io.PrintWriter pw, java.lang.String msg, int i)static byte[]quadsToByteArray(int quad)Convert a quad into a byte array.static intread2Bytes(java.lang.String name, java.io.InputStream in, java.lang.String exception, java.nio.ByteOrder byteOrder)static intread3Bytes(java.lang.String name, java.io.InputStream in, java.lang.String exception, java.nio.ByteOrder byteOrder)static intread4Bytes(java.lang.String name, java.io.InputStream in, java.lang.String exception, java.nio.ByteOrder byteOrder)static longread8Bytes(java.lang.String name, java.io.InputStream in, java.lang.String exception, java.nio.ByteOrder byteOrder)Reads eight bytes from the specified input stream, adjust for byte order, and return a long integer.static voidreadAndVerifyBytes(java.io.InputStream in, byte[] expected, java.lang.String exception)static voidreadAndVerifyBytes(java.io.InputStream in, BinaryConstant expected, java.lang.String exception)static bytereadByte(java.lang.String name, java.io.InputStream in, java.lang.String exceptionMessage)static byte[]readBytes(java.io.InputStream in, int count)static byte[]readBytes(java.lang.String name, java.io.InputStream in, int length)static byte[]readBytes(java.lang.String name, java.io.InputStream in, int length, java.lang.String exception)static byte[]remainingBytes(java.lang.String name, byte[] bytes, int count)static booleansearchQuad(int quad, java.io.InputStream in)Consumes theInputStream(without closing it) searching for a quad.static longskipBytes(java.io.InputStream in, long skip)static longskipBytes(java.io.InputStream in, long skip, java.lang.String exMessage)static booleanstartsWith(byte[] buffer, byte[] search)
-
-
-
Field Detail
-
NUL
private static final byte NUL
- See Also:
- Constant Field Values
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
-
Method Detail
-
charsToQuad
public static int charsToQuad(char c1, char c2, char c3, char c4)
-
compareBytes
public static boolean compareBytes(byte[] a, int aStart, byte[] b, int bStart, int length)
-
copyOfRange
public static byte[] copyOfRange(byte[] original, int from, int count)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
public static byte[] copyOfStart(byte[] original, int count)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(java.io.RandomAccessFile raf, long pos, int length, java.lang.String exception) throws java.io.IOException- Throws:
java.io.IOException
-
indexOf0
public static int indexOf0(byte[] src, int start, java.lang.String message) throws ImagingExceptionFinds the index of the first 0 in the array starting at the given index.- Parameters:
src- the array to search for the object, may benullstart- 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
public static int indexOf0(byte[] src, java.lang.String message) throws ImagingExceptionFinds the index of the first 0 in the array starting at the given index.- Parameters:
src- the array to search for the object, may benullmessage- 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
public static void logByteBits(java.lang.String msg, byte i)
-
logCharQuad
public static void logCharQuad(java.lang.String msg, int i)
-
printCharQuad
public static void printCharQuad(java.io.PrintWriter pw, java.lang.String msg, int i)
-
quadsToByteArray
public static byte[] quadsToByteArray(int quad)
Convert a quad into a byte array.- Parameters:
quad- quad- Returns:
- a byte array
-
read2Bytes
public static int read2Bytes(java.lang.String name, java.io.InputStream in, java.lang.String exception, java.nio.ByteOrder byteOrder) throws java.io.IOException- Throws:
java.io.IOException
-
read3Bytes
public static int read3Bytes(java.lang.String name, java.io.InputStream in, java.lang.String exception, java.nio.ByteOrder byteOrder) throws java.io.IOException- Throws:
java.io.IOException
-
read4Bytes
public static int read4Bytes(java.lang.String name, java.io.InputStream in, java.lang.String exception, java.nio.ByteOrder byteOrder) throws java.io.IOException- Throws:
java.io.IOException
-
read8Bytes
public static long read8Bytes(java.lang.String name, java.io.InputStream in, java.lang.String exception, java.nio.ByteOrder byteOrder) throws java.io.IOExceptionReads 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:
java.io.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(java.io.InputStream in, BinaryConstant expected, java.lang.String exception) throws ImagingException, java.io.IOException- Throws:
ImagingExceptionjava.io.IOException
-
readAndVerifyBytes
public static void readAndVerifyBytes(java.io.InputStream in, byte[] expected, java.lang.String exception) throws ImagingException, java.io.IOException- Throws:
ImagingExceptionjava.io.IOException
-
readByte
public static byte readByte(java.lang.String name, java.io.InputStream in, java.lang.String exceptionMessage) throws java.io.IOException- Throws:
java.io.IOException
-
readBytes
public static byte[] readBytes(java.io.InputStream in, int count) throws java.io.IOException- Throws:
java.io.IOException
-
readBytes
public static byte[] readBytes(java.lang.String name, java.io.InputStream in, int length) throws java.io.IOException- Throws:
java.io.IOException
-
readBytes
public static byte[] readBytes(java.lang.String name, java.io.InputStream in, int length, java.lang.String exception) throws java.io.IOException- Throws:
java.io.IOException
-
remainingBytes
public static byte[] remainingBytes(java.lang.String name, byte[] bytes, int count)
-
searchQuad
public static boolean searchQuad(int quad, java.io.InputStream in) throws java.io.IOExceptionConsumes 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, andfalseotherwise- Throws:
java.io.IOException- if it fails to read from the given input stream
-
skipBytes
public static long skipBytes(java.io.InputStream in, long skip) throws java.io.IOException- Throws:
java.io.IOException
-
skipBytes
public static long skipBytes(java.io.InputStream in, long skip, java.lang.String exMessage) throws java.io.IOException- Throws:
java.io.IOException
-
startsWith
public static boolean startsWith(byte[] buffer, byte[] search)
-
-