Package ubic.basecode.io
Class ByteArrayConverter
- java.lang.Object
-
- ubic.basecode.io.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 Summary
Constructors Constructor Description ByteArrayConverter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]booleanArrayToBytes(boolean[] boolarray)boolean[]byteArrayToBooleans(byte[] barray)char[]byteArrayToChars(byte[] barray)double[][]byteArrayToDoubleMatrix(byte[] barray)Convert a byte array to a double matrix, assuming it is square.double[][]byteArrayToDoubleMatrix(byte[] barray, int columns)Convert a byte array to a double matrix.double[]byteArrayToDoubles(byte[] barray)float[]byteArrayToFloats(byte[] barray)int[]byteArrayToInts(byte[] barray)long[]byteArrayToLongs(byte[] barray)<T> T[]byteArrayToObjects(byte[] barray, Class<T> type, Charset charset)String[]byteArrayToStrings(byte[] bytes, Charset charset)Convert a byte array into a array of Strings.StringbyteArrayToTabbedString(byte[] bytes, Class<?> type, Charset charset)Convert a byte array to a tab-delimited string.String[]byteArrayToTabbedStrings(byte[] bytes, Charset charset)byte[]charArrayToBytes(char[] carray)byte[]doubleArrayToBytes(double[] darray)byte[]doubleMatrixToBytes(double[][] testm)byte[]floatArrayToBytes(float[] darray)byte[]intArrayToBytes(int[] iarray)byte[]longArrayToBytes(long[] larray)<T> byte[]objectArrayToBytes(T[] array, Charset charset)Convert an array of Objects into an array of bytes.byte[]stringArrayToBytes(String[] stringArray, Charset charset)Note that this method cannot differentiate between empty strings and null strings.byte[]stringArrayToTabbedBytes(String[] strings, Charset charset)Convert an array of strings to a byte array where the delimiter is a tab character.
-
-
-
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 IllegalArgumentExceptionConvert 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
\tcharacters, they are escaped as\\t. Note that those will be decoded as escaped tabs bybyteArrayToTabbedStrings(byte[], Charset).This method does not distinguish between null and empty strings. Those will be decoded as empty strings by
byteArrayToTabbedStrings(byte[], 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.
-
-