Class BinaryFunctions


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

      Fields 
      Modifier and Type Field Description
      private static java.util.logging.Logger LOGGER  
      private static byte NUL  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private BinaryFunctions()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static 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​(java.io.RandomAccessFile raf, long pos, int length, java.lang.String exception)  
      static int indexOf0​(byte[] src, int start, java.lang.String message)
      Finds the index of the first 0 in the array starting at the given index.
      static int indexOf0​(byte[] src, java.lang.String message)
      Finds the index of the first 0 in the array starting at the given index.
      static void logByteBits​(java.lang.String msg, byte i)  
      static void logCharQuad​(java.lang.String msg, int i)  
      static void printCharQuad​(java.io.PrintWriter pw, java.lang.String msg, int i)  
      static byte[] quadsToByteArray​(int quad)
      Convert a quad into a byte array.
      static int read2Bytes​(java.lang.String name, java.io.InputStream in, java.lang.String exception, java.nio.ByteOrder byteOrder)  
      static int read3Bytes​(java.lang.String name, java.io.InputStream in, java.lang.String exception, java.nio.ByteOrder byteOrder)  
      static int read4Bytes​(java.lang.String name, java.io.InputStream in, java.lang.String exception, java.nio.ByteOrder byteOrder)  
      static long read8Bytes​(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 void readAndVerifyBytes​(java.io.InputStream in, byte[] expected, java.lang.String exception)  
      static void readAndVerifyBytes​(java.io.InputStream in, BinaryConstant expected, java.lang.String exception)  
      static byte readByte​(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 boolean searchQuad​(int quad, java.io.InputStream in)
      Consumes the InputStream (without closing it) searching for a quad.
      static long skipBytes​(java.io.InputStream in, long skip)  
      static long skipBytes​(java.io.InputStream in, long skip, java.lang.String exMessage)  
      static boolean startsWith​(byte[] buffer, byte[] search)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOGGER

        private static final java.util.logging.Logger LOGGER
    • Constructor Detail

      • BinaryFunctions

        private BinaryFunctions()
    • 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 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,
                                   java.lang.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​(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.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:
        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:
        ImagingException
        java.io.IOException
      • readAndVerifyBytes

        public static void readAndVerifyBytes​(java.io.InputStream in,
                                              byte[] expected,
                                              java.lang.String exception)
                                       throws ImagingException,
                                              java.io.IOException
        Throws:
        ImagingException
        java.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.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:
        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)