Package loci.common

Class DataTools


  • public final class DataTools
    extends Object
    A utility class with convenience methods for reading, writing and decoding words.
    Author:
    Curtis Rueden ctrueden at wisc.edu, Chris Allan callan at blackcat.ca, Melissa Linkert melissa at glencoesoftware.com
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static byte[] allocate​(int... sizes)
      Allocates a 1-dimensional byte array matching the product of the given sizes.
      static double bytesToDouble​(byte[] bytes, boolean little)
      Translates up to the first 8 bytes of a byte array to a double.
      static double bytesToDouble​(byte[] bytes, int off, boolean little)
      Translates up to the first 8 bytes of a byte array beyond the given offset to a double.
      static double bytesToDouble​(byte[] bytes, int off, int len, boolean little)
      Translates up to the first len bytes of a byte array beyond the given offset to a double.
      static double bytesToDouble​(short[] bytes, boolean little)
      Translates up to the first 8 bytes of a byte array to a double.
      static double bytesToDouble​(short[] bytes, int off, boolean little)
      Translates up to the first 8 bytes of a byte array beyond the given offset to a double.
      static double bytesToDouble​(short[] bytes, int off, int len, boolean little)
      Translates up to the first len bytes of a byte array beyond the given offset to a double.
      static float bytesToFloat​(byte[] bytes, boolean little)
      Translates up to the first 4 bytes of a byte array to a float.
      static float bytesToFloat​(byte[] bytes, int off, boolean little)
      Translates up to the first 4 bytes of a byte array beyond a given offset to a float.
      static float bytesToFloat​(byte[] bytes, int off, int len, boolean little)
      Translates up to the first len bytes of a byte array beyond the given offset to a float.
      static float bytesToFloat​(short[] bytes, boolean little)
      Translates up to the first 4 bytes of a byte array to a float.
      static float bytesToFloat​(short[] bytes, int off, boolean little)
      Translates up to the first 4 bytes of a byte array beyond a given offset to a float.
      static float bytesToFloat​(short[] bytes, int off, int len, boolean little)
      Translates up to the first len bytes of a byte array beyond a given offset to a float.
      static String bytesToHex​(byte[] b)
      Translates the given byte array into a String of hexadecimal digits.
      static int bytesToInt​(byte[] bytes, boolean little)
      Translates up to the first 4 bytes of a byte array to an int.
      static int bytesToInt​(byte[] bytes, int off, boolean little)
      Translates up to the first 4 bytes of a byte array beyond the given offset to an int.
      static int bytesToInt​(byte[] bytes, int off, int len, boolean little)
      Translates up to the first len bytes of a byte array beyond the given offset to an int.
      static int bytesToInt​(short[] bytes, boolean little)
      Translates up to the first 4 bytes of a byte array to an int.
      static int bytesToInt​(short[] bytes, int off, boolean little)
      Translates up to the first 4 bytes of a byte array beyond the given offset to an int.
      static int bytesToInt​(short[] bytes, int off, int len, boolean little)
      Translates up to the first len bytes of a byte array beyond the given offset to an int.
      static long bytesToLong​(byte[] bytes, boolean little)
      Translates up to the first 8 bytes of a byte array to a long.
      static long bytesToLong​(byte[] bytes, int off, boolean little)
      Translates up to the first 8 bytes of a byte array beyond the given offset to a long.
      static long bytesToLong​(byte[] bytes, int off, int len, boolean little)
      Translates up to the first len bytes of a byte array beyond the given offset to a long.
      static long bytesToLong​(short[] bytes, boolean little)
      Translates up to the first 8 bytes of a byte array to a long.
      static long bytesToLong​(short[] bytes, int off, boolean little)
      Translates up to the first 8 bytes of a byte array beyond the given offset to a long.
      static long bytesToLong​(short[] bytes, int off, int len, boolean little)
      Translates up to the first len bytes of a byte array beyond the given offset to a long.
      static short bytesToShort​(byte[] bytes, boolean little)
      Translates up to the first 2 bytes of a byte array to a short.
      static short bytesToShort​(byte[] bytes, int off, boolean little)
      Translates up to the first 2 bytes of a byte array beyond the given offset to a short.
      static short bytesToShort​(byte[] bytes, int off, int len, boolean little)
      Translates up to the first len bytes of a byte array beyond the given offset to a short.
      static short bytesToShort​(short[] bytes, boolean little)
      Translates up to the first 2 bytes of a byte array to a short.
      static short bytesToShort​(short[] bytes, int off, boolean little)
      Translates up to the first 2 bytes of a byte array byond the given offset to a short.
      static short bytesToShort​(short[] bytes, int off, int len, boolean little)
      Translates up to the first len bytes of a byte array byond the given offset to a short.
      static boolean containsValue​(int[] array, int value)
      Returns true if the given value is contained in the given array.
      static byte[] doublesToBytes​(double[] values, boolean little)
      Translates an array of double values into an array of byte values.
      static byte[] doubleToBytes​(double value, boolean little)
      Translates the double value into an array of eight bytes.
      static byte[] floatsToBytes​(float[] values, boolean little)
      Translates an array of float values into an array of byte values.
      static byte[] floatToBytes​(float value, boolean little)
      Translates the float value into an array of four bytes.
      static int indexOf​(int[] array, int value)
      Returns the index of the first occurrence of the given value in the given array.
      static int indexOf​(Object[] array, Object value)
      Returns the index of the first occurrence of the given value in the given Object array.
      static byte[] intsToBytes​(int[] values, boolean little)
      Translates an array of int values into an array of byte values.
      static byte[] intToBytes​(int value, boolean little)
      Translates the int value into an array of four bytes.
      static byte[] longsToBytes​(long[] values, boolean little)
      Translates an array of long values into an array of byte values.
      static byte[] longToBytes​(long value, boolean little)
      Translates the long value into an array of eight bytes.
      static Object makeDataArray​(byte[] b, int bpp, boolean fp, boolean little)
      Convert a byte array to the appropriate 1D primitive type array.
      static Object makeDataArray2D​(byte[] b, int bpp, boolean fp, boolean little, int height)
      Convert a byte array to the appropriate 2D primitive type array.
      static byte[] makeSigned​(byte[] b)  
      static int[] makeSigned​(int[] i)  
      static short[] makeSigned​(short[] s)  
      static double[] normalizeDoubles​(double[] data)
      Normalize the given double array so that the minimum value maps to 0.0 and the maximum value maps to 1.0.
      static float[] normalizeFloats​(float[] data)
      Normalize the given float array so that the minimum value maps to 0.0 and the maximum value maps to 1.0.
      static Byte parseByte​(String value)
      Parses the input string into a byte
      static Double parseDouble​(String value)
      Parses the input string into a double accounting for the locale decimal separator
      static Float parseFloat​(String value)
      Parses the input string into a float accounting for the locale decimal separator
      static Integer parseInteger​(String value)
      Parses the input string into an integer
      static Long parseLong​(String value)
      Parses the input string into a long
      static Short parseShort​(String value)
      Parses the input string into a short
      static String readFile​(String id)
      Reads the contents of the given file into a string.
      static int safeMultiply32​(int... sizes)
      Checks that the product of the given sizes does not exceed the 32-bit integer limit (i.e., Integer.MAX_VALUE).
      static long safeMultiply64​(long... sizes)
      Checks that the product of the given sizes does not exceed the 64-bit integer limit (i.e., Long.MAX_VALUE).
      static boolean samePrefix​(String s1, String s2)
      Check if two filenames have the same prefix.
      static String sanitize​(String s)
      Remove unprintable characters from the given string.
      static String sanitizeDouble​(String value)
      Deprecated.
      static byte[] shortsToBytes​(short[] values, boolean little)
      Translates an array of short values into an array of byte values.
      static byte[] shortToBytes​(short value, boolean little)
      Translates the short value into an array of two bytes.
      static String stripString​(String toStrip)
      Remove null bytes from a string.
      static char swap​(char x)
      Reverse the order of bytes in the given char.
      static double swap​(double x)
      Reverse the order of bytes in the given double.
      static float swap​(float x)
      Reverse the order of bytes in the given float.
      static int swap​(int x)
      Reverse the order of bytes in the given int.
      static long swap​(long x)
      Reverse the order of bytes in the given long.
      static short swap​(short x)
      Reverse the order of bytes in the given short.
      static void unpackBytes​(long value, byte[] buf, int ndx, int nBytes, boolean little)
      Translates nBytes of the given long and places the result in the given byte array.
    • Method Detail

      • readFile

        public static String readFile​(String id)
                               throws IOException
        Reads the contents of the given file into a string.
        Parameters:
        id - name of the file to read this can be any name supported by Location, not necessarily a file on disk
        Returns:
        the complete contents of the specified file
        Throws:
        IOException - if the file cannot be read or is larger than 2GB
        See Also:
        Location.getMappedId(String)
      • bytesToShort

        public static short bytesToShort​(byte[] bytes,
                                         int off,
                                         int len,
                                         boolean little)
        Translates up to the first len bytes of a byte array beyond the given offset to a short. If there are fewer than len bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to use for translation
        off - offset to the first byte in the array
        len - number of bytes to use
        little - true if the bytes are provided in little-endian order
        Returns:
        the short value that results from concatenating the specified bytes
      • bytesToShort

        public static short bytesToShort​(byte[] bytes,
                                         int off,
                                         boolean little)
        Translates up to the first 2 bytes of a byte array beyond the given offset to a short. If there are fewer than 2 bytes available the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to use for translation
        off - offset to the first byte in the array
        little - true if the bytes are provided in little-endian order
        Returns:
        the short value that results from concatenating the specified bytes
      • bytesToShort

        public static short bytesToShort​(byte[] bytes,
                                         boolean little)
        Translates up to the first 2 bytes of a byte array to a short. If there are fewer than 2 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to use for translation
        little - true if the bytes are provided in little-endian order
        Returns:
        the short value that results from concatenating the specified bytes
      • bytesToShort

        public static short bytesToShort​(short[] bytes,
                                         int off,
                                         int len,
                                         boolean little)
        Translates up to the first len bytes of a byte array byond the given offset to a short. If there are fewer than len bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to use for translation
        off - offset to the first byte in the array
        len - number of bytes to use
        little - true if the bytes are provided in little-endian order
        Returns:
        the short value that results from concatenating the specified bytes
      • bytesToShort

        public static short bytesToShort​(short[] bytes,
                                         int off,
                                         boolean little)
        Translates up to the first 2 bytes of a byte array byond the given offset to a short. If there are fewer than 2 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a short
        off - offset into array of bytes; should be non-negative and less than the length of the array
        little - true if the bytes are provided in little-endian order
        Returns:
        the short value that results from concatenating the specified bytes
      • bytesToShort

        public static short bytesToShort​(short[] bytes,
                                         boolean little)
        Translates up to the first 2 bytes of a byte array to a short. If there are fewer than 2 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a short
        little - true if the bytes are provided in little-endian order
        Returns:
        the short value that results from concatenating the specified bytes
      • bytesToInt

        public static int bytesToInt​(byte[] bytes,
                                     int off,
                                     int len,
                                     boolean little)
        Translates up to the first len bytes of a byte array beyond the given offset to an int. If there are fewer than len bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a int
        off - offset to the first byte in the array
        len - number of bytes to use
        little - true if the bytes are provided in little-endian order
        Returns:
        the int value that results from concatenating the specified bytes
      • bytesToInt

        public static int bytesToInt​(byte[] bytes,
                                     int off,
                                     boolean little)
        Translates up to the first 4 bytes of a byte array beyond the given offset to an int. If there are fewer than 4 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a int
        off - offset to the first byte in the array
        little - true if the bytes are provided in little-endian order
        Returns:
        the int value that results from concatenating the specified bytes
      • bytesToInt

        public static int bytesToInt​(byte[] bytes,
                                     boolean little)
        Translates up to the first 4 bytes of a byte array to an int. If there are fewer than 4 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a int
        little - true if the bytes are provided in little-endian order
        Returns:
        the int value that results from concatenating the specified bytes
      • bytesToInt

        public static int bytesToInt​(short[] bytes,
                                     int off,
                                     int len,
                                     boolean little)
        Translates up to the first len bytes of a byte array beyond the given offset to an int. If there are fewer than len bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a int
        off - offset to the first byte in the array
        len - number of bytes to use
        little - true if the bytes are provided in little-endian order
        Returns:
        the int value that results from concatenating the specified bytes
      • bytesToInt

        public static int bytesToInt​(short[] bytes,
                                     int off,
                                     boolean little)
        Translates up to the first 4 bytes of a byte array beyond the given offset to an int. If there are fewer than 4 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a int
        off - offset to the first byte in the array
        little - true if the bytes are provided in little-endian order
        Returns:
        the int value that results from concatenating the specified bytes
      • bytesToInt

        public static int bytesToInt​(short[] bytes,
                                     boolean little)
        Translates up to the first 4 bytes of a byte array to an int. If there are fewer than 4 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a int
        little - true if the bytes are provided in little-endian order
        Returns:
        the int value that results from concatenating the specified bytes
      • bytesToFloat

        public static float bytesToFloat​(byte[] bytes,
                                         int off,
                                         int len,
                                         boolean little)
        Translates up to the first len bytes of a byte array beyond the given offset to a float. If there are fewer than len bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a float
        off - offset to the first byte in the array
        len - number of bytes to use
        little - true if the bytes are provided in little-endian order
        Returns:
        the float value that results from concatenating the specified bytes
      • bytesToFloat

        public static float bytesToFloat​(byte[] bytes,
                                         int off,
                                         boolean little)
        Translates up to the first 4 bytes of a byte array beyond a given offset to a float. If there are fewer than 4 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a float
        off - offset to the first byte in the array
        little - true if the bytes are provided in little-endian order
        Returns:
        the float value that results from concatenating the specified bytes
      • bytesToFloat

        public static float bytesToFloat​(byte[] bytes,
                                         boolean little)
        Translates up to the first 4 bytes of a byte array to a float. If there are fewer than 4 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a float
        little - true if the bytes are provided in little-endian order
        Returns:
        the float value that results from concatenating the specified bytes
      • bytesToFloat

        public static float bytesToFloat​(short[] bytes,
                                         int off,
                                         int len,
                                         boolean little)
        Translates up to the first len bytes of a byte array beyond a given offset to a float. If there are fewer than len bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a float
        off - offset to the first byte in the array
        len - number of bytes to use
        little - true if the bytes are provided in little-endian order
        Returns:
        the float value that results from concatenating the specified bytes
      • bytesToFloat

        public static float bytesToFloat​(short[] bytes,
                                         int off,
                                         boolean little)
        Translates up to the first 4 bytes of a byte array beyond a given offset to a float. If there are fewer than 4 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a float
        off - offset to the first byte in the array
        little - true if the bytes are provided in little-endian order
        Returns:
        the float value that results from concatenating the specified bytes
      • bytesToFloat

        public static float bytesToFloat​(short[] bytes,
                                         boolean little)
        Translates up to the first 4 bytes of a byte array to a float. If there are fewer than 4 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a float
        little - true if the bytes are provided in little-endian order
        Returns:
        the float value that results from concatenating the specified bytes
      • bytesToLong

        public static long bytesToLong​(byte[] bytes,
                                       int off,
                                       int len,
                                       boolean little)
        Translates up to the first len bytes of a byte array beyond the given offset to a long. If there are fewer than len bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a long
        off - offset to the first byte in the array
        len - number of bytes to use
        little - true if the bytes are provided in little-endian order
        Returns:
        the long value that results from concatenating the specified bytes
      • bytesToLong

        public static long bytesToLong​(byte[] bytes,
                                       int off,
                                       boolean little)
        Translates up to the first 8 bytes of a byte array beyond the given offset to a long. If there are fewer than 8 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a long
        off - offset to the first byte in the array
        little - true if the bytes are provided in little-endian order
        Returns:
        the long value that results from concatenating the specified bytes
      • bytesToLong

        public static long bytesToLong​(byte[] bytes,
                                       boolean little)
        Translates up to the first 8 bytes of a byte array to a long. If there are fewer than 8 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a long
        little - true if the bytes are provided in little-endian order
        Returns:
        the long value that results from concatenating the specified bytes
      • bytesToLong

        public static long bytesToLong​(short[] bytes,
                                       int off,
                                       int len,
                                       boolean little)
        Translates up to the first len bytes of a byte array beyond the given offset to a long. If there are fewer than len bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a long
        off - offset to the first byte in the array
        len - number of bytes to use
        little - true if the bytes are provided in little-endian order
        Returns:
        the long value that results from concatenating the specified bytes
      • bytesToLong

        public static long bytesToLong​(short[] bytes,
                                       int off,
                                       boolean little)
        Translates up to the first 8 bytes of a byte array beyond the given offset to a long. If there are fewer than 8 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a long
        off - offset to the first byte in the array
        little - true if the bytes are provided in little-endian order
        Returns:
        the long value that results from concatenating the specified bytes
      • bytesToLong

        public static long bytesToLong​(short[] bytes,
                                       boolean little)
        Translates up to the first 8 bytes of a byte array to a long. If there are fewer than 8 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a long
        little - true if the bytes are provided in little-endian order
        Returns:
        the long value that results from concatenating the specified bytes
      • bytesToDouble

        public static double bytesToDouble​(byte[] bytes,
                                           int off,
                                           int len,
                                           boolean little)
        Translates up to the first len bytes of a byte array beyond the given offset to a double. If there are fewer than len bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a double
        off - offset to the first byte in the array
        len - number of bytes to use
        little - true if the bytes are provided in little-endian order
        Returns:
        the double value that results from concatenating the specified bytes
      • bytesToDouble

        public static double bytesToDouble​(byte[] bytes,
                                           int off,
                                           boolean little)
        Translates up to the first 8 bytes of a byte array beyond the given offset to a double. If there are fewer than 8 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a double
        off - offset to the first byte in the array
        little - true if the bytes are provided in little-endian order
        Returns:
        the double value that results from concatenating the specified bytes
      • bytesToDouble

        public static double bytesToDouble​(byte[] bytes,
                                           boolean little)
        Translates up to the first 8 bytes of a byte array to a double. If there are fewer than 8 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of bytes to be translated to a double
        little - true if the bytes are provided in little-endian order
        Returns:
        the double value that results from concatenating the specified bytes
      • bytesToDouble

        public static double bytesToDouble​(short[] bytes,
                                           int off,
                                           int len,
                                           boolean little)
        Translates up to the first len bytes of a byte array beyond the given offset to a double. If there are fewer than len bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a double
        off - offset to the first byte in the array
        len - number of bytes to use
        little - true if the bytes are provided in little-endian order
        Returns:
        the double value that results from concatenating the specified bytes
      • bytesToDouble

        public static double bytesToDouble​(short[] bytes,
                                           int off,
                                           boolean little)
        Translates up to the first 8 bytes of a byte array beyond the given offset to a double. If there are fewer than 8 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a double
        off - offset to the first byte in the array
        little - true if the bytes are provided in little-endian order
        Returns:
        the double value that results from concatenating the specified bytes
      • bytesToDouble

        public static double bytesToDouble​(short[] bytes,
                                           boolean little)
        Translates up to the first 8 bytes of a byte array to a double. If there are fewer than 8 bytes available, the MSBs are all assumed to be zero (regardless of endianness).
        Parameters:
        bytes - array of unsigned bytes to be translated to a double
        little - true if the bytes are provided in little-endian order
        Returns:
        the double value that results from concatenating the specified bytes
      • bytesToHex

        public static String bytesToHex​(byte[] b)
        Translates the given byte array into a String of hexadecimal digits.
        Parameters:
        b - the array of bytes to parse
        Returns:
        a string of length 2 * b.length representing the hexadecimal digits of each byte in b concatenated
      • parseShort

        public static Short parseShort​(String value)
        Parses the input string into a short
        Parameters:
        value - a String representation of a short value
        Returns:
        null if the string could not be parsed
      • parseByte

        public static Byte parseByte​(String value)
        Parses the input string into a byte
        Parameters:
        value - a String representation of a byte value
        Returns:
        null if the string could not be parsed
      • parseInteger

        public static Integer parseInteger​(String value)
        Parses the input string into an integer
        Parameters:
        value - a String representation of an int value
        Returns:
        null if the string could not be parsed
      • parseLong

        public static Long parseLong​(String value)
        Parses the input string into a long
        Parameters:
        value - a String representation of a long value
        Returns:
        null if the string could not be parsed
      • parseFloat

        public static Float parseFloat​(String value)
        Parses the input string into a float accounting for the locale decimal separator
        Parameters:
        value - a String representation of a float value
        Returns:
        null if the string could not be parsed
      • parseDouble

        public static Double parseDouble​(String value)
        Parses the input string into a double accounting for the locale decimal separator
        Parameters:
        value - a String representation of a double value
        Returns:
        null if the string could not be parsed
      • sanitizeDouble

        @Deprecated
        public static String sanitizeDouble​(String value)
        Deprecated.
        Normalizes the decimal separator for the user's locale.
        Parameters:
        value - the String representation of a double value, which may contain invalid characters that prevent parsing
        Returns:
        see above
      • shortToBytes

        public static byte[] shortToBytes​(short value,
                                          boolean little)
        Translates the short value into an array of two bytes.
        Parameters:
        value - the short to be split into bytes
        little - true if the returned bytes should be in little-endian order
        Returns:
        byte array with length 2
      • intToBytes

        public static byte[] intToBytes​(int value,
                                        boolean little)
        Translates the int value into an array of four bytes.
        Parameters:
        value - the int to be split into bytes
        little - true if the returned bytes should be in little-endian order
        Returns:
        byte array with length 4
      • floatToBytes

        public static byte[] floatToBytes​(float value,
                                          boolean little)
        Translates the float value into an array of four bytes.
        Parameters:
        value - the float to be split into bytes
        little - true if the returned bytes should be in little-endian order
        Returns:
        byte array with length 4
      • longToBytes

        public static byte[] longToBytes​(long value,
                                         boolean little)
        Translates the long value into an array of eight bytes.
        Parameters:
        value - the long to be split into bytes
        little - true if the returned bytes should be in little-endian order
        Returns:
        byte array with length 8
      • doubleToBytes

        public static byte[] doubleToBytes​(double value,
                                           boolean little)
        Translates the double value into an array of eight bytes.
        Parameters:
        value - the double to be split into bytes
        little - true if the returned bytes should be in little-endian order
        Returns:
        byte array with length 8
      • shortsToBytes

        public static byte[] shortsToBytes​(short[] values,
                                           boolean little)
        Translates an array of short values into an array of byte values.
        Parameters:
        values - the shorts to be split into bytes
        little - true if the returned bytes should be in little-endian order
        Returns:
        byte array with length 2 * values.length
      • intsToBytes

        public static byte[] intsToBytes​(int[] values,
                                         boolean little)
        Translates an array of int values into an array of byte values.
        Parameters:
        values - the ints to be split into bytes
        little - true if the returned bytes should be in little-endian order
        Returns:
        byte array with length 4 * values.length
      • floatsToBytes

        public static byte[] floatsToBytes​(float[] values,
                                           boolean little)
        Translates an array of float values into an array of byte values.
        Parameters:
        values - the floats to be split into bytes
        little - true if the returned bytes should be in little-endian order
        Returns:
        byte array with length 4 * values.length
      • longsToBytes

        public static byte[] longsToBytes​(long[] values,
                                          boolean little)
        Translates an array of long values into an array of byte values.
        Parameters:
        values - the longs to be split into bytes
        little - true if the returned bytes should be in little-endian order
        Returns:
        byte array with length 8 * values.length
      • doublesToBytes

        public static byte[] doublesToBytes​(double[] values,
                                            boolean little)
        Translates an array of double values into an array of byte values.
        Parameters:
        values - the doubles to be split into bytes
        little - true if the returned bytes should be in little-endian order
        Returns:
        byte array with length 8 * values.length
      • unpackBytes

        public static void unpackBytes​(long value,
                                       byte[] buf,
                                       int ndx,
                                       int nBytes,
                                       boolean little)
        Translates nBytes of the given long and places the result in the given byte array.
        Parameters:
        value - the long to be split into bytes
        buf - the byte array in which to store the unpacked bytes
        ndx - the offset to the first byte in the array
        nBytes - the number of unpacked bytes
        little - true if the unpacked bytes should be in little-endian order
        Throws:
        IllegalArgumentException - if the specified indices fall outside the buffer
      • makeDataArray

        public static Object makeDataArray​(byte[] b,
                                           int bpp,
                                           boolean fp,
                                           boolean little)
        Convert a byte array to the appropriate 1D primitive type array.
        Parameters:
        b - Byte array to convert.
        bpp - Denotes the number of bytes in the returned primitive type (e.g. if bpp == 2, we should return an array of type short).
        fp - If set and bpp == 4 or bpp == 8, then return floats or doubles.
        little - Whether byte array is in little-endian order.
        Returns:
        an array of primitives
      • makeDataArray2D

        public static Object makeDataArray2D​(byte[] b,
                                             int bpp,
                                             boolean fp,
                                             boolean little,
                                             int height)
        Convert a byte array to the appropriate 2D primitive type array.
        Parameters:
        b - Byte array to convert.
        bpp - Denotes the number of bytes in the returned primitive type (e.g. if bpp == 2, we should return an array of type short).
        fp - If set and bpp == 4 or bpp == 8, then return floats or doubles.
        little - Whether byte array is in little-endian order.
        height - The height of the output primitive array (2nd dim length).
        Returns:
        a 2D primitive array of appropriate type, dimensioned [height][b.length / (bpp * height)]
        Throws:
        IllegalArgumentException - if input byte array does not divide evenly into height pieces
      • swap

        public static short swap​(short x)
        Reverse the order of bytes in the given short.
        Parameters:
        x - short value to byte swap
        Returns:
        short value resulting from byte swapping x
      • swap

        public static char swap​(char x)
        Reverse the order of bytes in the given char.
        Parameters:
        x - char value to byte swap
        Returns:
        char value resulting from byte swapping x
      • swap

        public static int swap​(int x)
        Reverse the order of bytes in the given int.
        Parameters:
        x - int value to byte swap
        Returns:
        int value resulting from byte swapping x
      • swap

        public static long swap​(long x)
        Reverse the order of bytes in the given long.
        Parameters:
        x - long value to byte swap
        Returns:
        long value resulting from byte swapping x
      • swap

        public static float swap​(float x)
        Reverse the order of bytes in the given float.
        Parameters:
        x - float value to byte swap
        Returns:
        float value resulting from byte swapping x
      • swap

        public static double swap​(double x)
        Reverse the order of bytes in the given double.
        Parameters:
        x - double value to byte swap
        Returns:
        double value resulting from byte swapping x
      • stripString

        public static String stripString​(String toStrip)
        Remove null bytes from a string.
        Parameters:
        toStrip - String from which to remove null bytes
        Returns:
        a String copy of toStrip with all null (0) bytes removed
      • samePrefix

        public static boolean samePrefix​(String s1,
                                         String s2)
        Check if two filenames have the same prefix. The order in which the filenames are supplied does not matter. Examples: s1 = /tmp/path.txt s2 = /home/anonymous/path.png returns true s1 = /tmp/path1.txt s2 = /tmp/path246.txt returns false s1 = /tmp/path.txt s2 = /tmp/path246.txt returns true
        Parameters:
        s1 - first String filename to compare
        s2 - second String filename to compare
        Returns:
        true if the relative filename without suffix for either input filename is a substring of the other input filenames' relative filename without suffix
      • sanitize

        public static String sanitize​(String s)
        Remove unprintable characters from the given string.
        Parameters:
        s - String from which to remove unprintable characters
        Returns:
        a String copy of s with tabs, newlines, and control characters removed
        See Also:
        Character.isISOControl(char)
      • normalizeFloats

        public static float[] normalizeFloats​(float[] data)
        Normalize the given float array so that the minimum value maps to 0.0 and the maximum value maps to 1.0.
        Parameters:
        data - array of float values to normalize
        Returns:
        array of float values in the range [0.0, 1.0]
      • normalizeDoubles

        public static double[] normalizeDoubles​(double[] data)
        Normalize the given double array so that the minimum value maps to 0.0 and the maximum value maps to 1.0.
        Parameters:
        data - array of double values to normalize
        Returns:
        array of double values in the range [0.0, 1.0]
      • allocate

        public static byte[] allocate​(int... sizes)
                               throws IllegalArgumentException
        Allocates a 1-dimensional byte array matching the product of the given sizes.
        Parameters:
        sizes - list of sizes from which to allocate the array
        Returns:
        a byte array of the appropriate size
        Throws:
        IllegalArgumentException - if the total size exceeds 2GB, which is the maximum size of an array in Java; or if any size argument is zero or negative
      • safeMultiply32

        public static int safeMultiply32​(int... sizes)
                                  throws IllegalArgumentException
        Checks that the product of the given sizes does not exceed the 32-bit integer limit (i.e., Integer.MAX_VALUE).
        Parameters:
        sizes - list of sizes from which to compute the product
        Returns:
        the product of the given sizes
        Throws:
        IllegalArgumentException - if the total size exceeds 2GiB, which is the maximum size of an int in Java; or if any size argument is zero or negative
      • safeMultiply64

        public static long safeMultiply64​(long... sizes)
                                   throws IllegalArgumentException
        Checks that the product of the given sizes does not exceed the 64-bit integer limit (i.e., Long.MAX_VALUE).
        Parameters:
        sizes - list of sizes from which to compute the product
        Returns:
        the product of the given sizes
        Throws:
        IllegalArgumentException - if the total size exceeds 8EiB, which is the maximum size of a long in Java; or if any size argument is zero or negative
      • containsValue

        public static boolean containsValue​(int[] array,
                                            int value)
        Returns true if the given value is contained in the given array.
        Parameters:
        array - an array of ints to search
        value - the int for which to search
        Returns:
        true if array contains at least one occurence of value
        See Also:
        indexOf(int[], int)
      • indexOf

        public static int indexOf​(int[] array,
                                  int value)
        Returns the index of the first occurrence of the given value in the given array. If the value is not in the array, returns -1.
        Parameters:
        array - an array of ints to search
        value - the int for which to search
        Returns:
        the index of the first occurence of value in array, or -1 if value is not found
      • indexOf

        public static int indexOf​(Object[] array,
                                  Object value)
        Returns the index of the first occurrence of the given value in the given Object array. If the value is not in the array, returns -1.
        Parameters:
        array - an array of Objects to search
        value - the Object for which to search
        Returns:
        the index of the first occurence of value in array, or -1 if value is not found
      • makeSigned

        public static byte[] makeSigned​(byte[] b)
      • makeSigned

        public static short[] makeSigned​(short[] s)
      • makeSigned

        public static int[] makeSigned​(int[] i)