Class ByteArrayConverter


  • public final class ByteArrayConverter
    extends Object
    Class to convert byte arrays (e.g., Blobs) to and from other types of arrays.
    Author:
    Kiran Keshav, Paul Pavlidis
    • Constructor Detail

      • ByteArrayConverter

        public ByteArrayConverter()
    • Method Detail

      • booleanArrayToBytes

        public byte[] booleanArrayToBytes​(boolean[] boolarray)
      • byteArrayToBooleans

        public boolean[] byteArrayToBooleans​(byte[] barray)
      • doubleArrayToBytes

        public byte[] doubleArrayToBytes​(double[] darray)
      • byteArrayToDoubles

        public double[] byteArrayToDoubles​(byte[] barray)
      • doubleMatrixToBytes

        public byte[] doubleMatrixToBytes​(double[][] testm)
      • byteArrayToDoubleMatrix

        public double[][] byteArrayToDoubleMatrix​(byte[] barray)
        Convert a byte array to a double matrix, assuming it is square.
      • byteArrayToDoubleMatrix

        public double[][] byteArrayToDoubleMatrix​(byte[] barray,
                                                  int columns)
                                           throws IllegalArgumentException
        Convert a byte array to a double matrix.
        Parameters:
        columns - the number of columns in the matrix
        Throws:
        IllegalArgumentException
      • stringArrayToBytes

        public byte[] stringArrayToBytes​(String[] stringArray,
                                         Charset charset)
        Note that this method cannot differentiate between empty strings and null strings. A string that is empty will be returned as an empty string, not null, while a null string will be stored as an empty string.
        Parameters:
        charset - charset to use when converting strings to bytes
      • byteArrayToStrings

        public String[] byteArrayToStrings​(byte[] bytes,
                                           Charset charset)
        Convert a byte array into a array of Strings.

        It is assumed that separate strings are delimited by a '' (NUL) character. Note that this method cannot differentiate between empty strings and null strings. A string that is empty will be returned as an empty string, not null.

        Parameters:
        charset - charset to use when decoding bytes into strings
      • stringArrayToTabbedBytes

        public byte[] stringArrayToTabbedBytes​(String[] strings,
                                               Charset charset)
        Convert an array of strings to a byte array where the delimiter is a tab character.

        If the string contains actual \t characters, they are escaped as \\t. Note that those will be decoded as escaped tabs by byteArrayToTabbedStrings(byte[], Charset).

        This method does not distinguish between null and empty strings. Those will be decoded as empty strings by byteArrayToTabbedStrings(byte[], Charset).

      • byteArrayToTabbedStrings

        public String[] byteArrayToTabbedStrings​(byte[] bytes,
                                                 Charset charset)
      • byteArrayToTabbedString

        public String byteArrayToTabbedString​(byte[] bytes,
                                              Class<?> type,
                                              Charset charset)
        Convert a byte array to a tab-delimited string.
        Parameters:
        type - The Class of primitives the bytes are to be interpreted as. If this is String, then the bytes are directly interpreted as tab-delimited string (e.g., no extra tabs are added).
        charset - charset to use when decoding bytes into strings
        Throws:
        UnsupportedOperationException - if Class is a type that can't be converted by this.
      • charArrayToBytes

        public byte[] charArrayToBytes​(char[] carray)
      • byteArrayToChars

        public char[] byteArrayToChars​(byte[] barray)
      • floatArrayToBytes

        public byte[] floatArrayToBytes​(float[] darray)
      • byteArrayToFloats

        public float[] byteArrayToFloats​(byte[] barray)
      • intArrayToBytes

        public byte[] intArrayToBytes​(int[] iarray)
      • byteArrayToInts

        public int[] byteArrayToInts​(byte[] barray)
      • longArrayToBytes

        public byte[] longArrayToBytes​(long[] larray)
      • byteArrayToLongs

        public long[] byteArrayToLongs​(byte[] barray)
        Returns:
        long[] resulting from parse of the bytes.
      • objectArrayToBytes

        public <T> byte[] objectArrayToBytes​(T[] array,
                                             Charset charset)
        Convert an array of Objects into an array of bytes.
        Parameters:
        array - of objects to be converted to bytes.
        charset - charset to use when converting strings to bytes
        Throws:
        UnsupportedOperationException - if Objects are a type that can't be converted by this.
      • byteArrayToObjects

        public <T> T[] byteArrayToObjects​(byte[] barray,
                                          Class<T> type,
                                          Charset charset)
        Parameters:
        charset - charset to use when decoding strings from bytes
        Throws:
        UnsupportedOperationException - if type is a type that can't be converted by this.