Class BinaryFunctions

java.lang.Object
org.apache.commons.imaging.common.BinaryFunctions

public final class BinaryFunctions extends Object
Convenience methods for various binary and I/O operations.
  • Field Details

  • Constructor Details

    • BinaryFunctions

      private BinaryFunctions()
  • Method Details

    • 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(RandomAccessFile raf, long pos, int length, String exception) throws IOException
      Throws:
      IOException
    • indexOf0

      public static int indexOf0(byte[] src, int start, String message) throws ImagingException
      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 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.
    • indexOf0

      public static int indexOf0(byte[] src, String message) throws ImagingException
      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 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

      public static void logByteBits(String msg, byte i)
    • logCharQuad

      public static void logCharQuad(String msg, int i)
    • printCharQuad

      public static void printCharQuad(PrintWriter pw, 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(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 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.
    • 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

      public static byte[] readBytes(InputStream in, int count) throws IOException
      Throws:
      IOException
    • readBytes

      public static byte[] readBytes(String name, InputStream in, int length) throws IOException
      Throws:
      IOException
    • readBytes

      public static byte[] readBytes(String name, InputStream in, int length, String exception) throws IOException
      Throws:
      IOException
    • remainingBytes

      public static byte[] remainingBytes(String name, byte[] bytes, int count)
    • searchQuad

      public static boolean searchQuad(int quad, InputStream in) throws IOException
      Consumes the InputStream (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, and false otherwise.

      Parameters:
      quad - a quad (the needle)
      in - an input stream (the haystack)
      Returns:
      true if it found the quad, and false otherwise
      Throws:
      IOException - if it fails to read from the given input stream
    • skipBytes

      public static long skipBytes(InputStream in, long skip) throws IOException
      Throws:
      IOException
    • skipBytes

      public static long skipBytes(InputStream in, long skip, String exMessage) throws IOException
      Throws:
      IOException
    • startsWith

      public static boolean startsWith(byte[] buffer, byte[] search)