public final class DataTools extends Object
Modifier and Type | Method and 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.
Use
parseDouble(String) instead |
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.
|
public static String readFile(String id) throws IOException
id
- name of the file to read
this can be any name supported by Location,
not necessarily a file on diskIOException
- if the file cannot be read or is larger than 2GBLocation.getMappedId(String)
public static short bytesToShort(byte[] bytes, int off, int len, boolean little)
bytes
- array of bytes to use for translationoff
- offset to the first byte in the arraylen
- number of bytes to uselittle
- true if the bytes are provided in little-endian orderpublic static short bytesToShort(byte[] bytes, int off, boolean little)
bytes
- array of bytes to use for translationoff
- offset to the first byte in the arraylittle
- true if the bytes are provided in little-endian orderpublic static short bytesToShort(byte[] bytes, boolean little)
bytes
- array of bytes to use for translationlittle
- true if the bytes are provided in little-endian orderpublic static short bytesToShort(short[] bytes, int off, int len, boolean little)
bytes
- array of unsigned bytes to use for translationoff
- offset to the first byte in the arraylen
- number of bytes to uselittle
- true if the bytes are provided in little-endian orderpublic static short bytesToShort(short[] bytes, int off, boolean little)
bytes
- array of unsigned bytes to be translated to a shortoff
- offset into array of bytes; should be non-negative and less than
the length of the arraylittle
- true if the bytes are provided in little-endian orderpublic static short bytesToShort(short[] bytes, boolean little)
bytes
- array of unsigned bytes to be translated to a shortlittle
- true if the bytes are provided in little-endian orderpublic static int bytesToInt(byte[] bytes, int off, int len, boolean little)
bytes
- array of bytes to be translated to a intoff
- offset to the first byte in the arraylen
- number of bytes to uselittle
- true if the bytes are provided in little-endian orderpublic static int bytesToInt(byte[] bytes, int off, boolean little)
bytes
- array of bytes to be translated to a intoff
- offset to the first byte in the arraylittle
- true if the bytes are provided in little-endian orderpublic static int bytesToInt(byte[] bytes, boolean little)
bytes
- array of bytes to be translated to a intlittle
- true if the bytes are provided in little-endian orderpublic static int bytesToInt(short[] bytes, int off, int len, boolean little)
bytes
- array of unsigned bytes to be translated to a intoff
- offset to the first byte in the arraylen
- number of bytes to uselittle
- true if the bytes are provided in little-endian orderpublic static int bytesToInt(short[] bytes, int off, boolean little)
bytes
- array of unsigned bytes to be translated to a intoff
- offset to the first byte in the arraylittle
- true if the bytes are provided in little-endian orderpublic static int bytesToInt(short[] bytes, boolean little)
bytes
- array of unsigned bytes to be translated to a intlittle
- true if the bytes are provided in little-endian orderpublic static float bytesToFloat(byte[] bytes, int off, int len, boolean little)
bytes
- array of bytes to be translated to a floatoff
- offset to the first byte in the arraylen
- number of bytes to uselittle
- true if the bytes are provided in little-endian orderpublic static float bytesToFloat(byte[] bytes, int off, boolean little)
bytes
- array of bytes to be translated to a floatoff
- offset to the first byte in the arraylittle
- true if the bytes are provided in little-endian orderpublic static float bytesToFloat(byte[] bytes, boolean little)
bytes
- array of bytes to be translated to a floatlittle
- true if the bytes are provided in little-endian orderpublic static float bytesToFloat(short[] bytes, int off, int len, boolean little)
bytes
- array of unsigned bytes to be translated to a floatoff
- offset to the first byte in the arraylen
- number of bytes to uselittle
- true if the bytes are provided in little-endian orderpublic static float bytesToFloat(short[] bytes, int off, boolean little)
bytes
- array of unsigned bytes to be translated to a floatoff
- offset to the first byte in the arraylittle
- true if the bytes are provided in little-endian orderpublic static float bytesToFloat(short[] bytes, boolean little)
bytes
- array of unsigned bytes to be translated to a floatlittle
- true if the bytes are provided in little-endian orderpublic static long bytesToLong(byte[] bytes, int off, int len, boolean little)
bytes
- array of bytes to be translated to a longoff
- offset to the first byte in the arraylen
- number of bytes to uselittle
- true if the bytes are provided in little-endian orderpublic static long bytesToLong(byte[] bytes, int off, boolean little)
bytes
- array of bytes to be translated to a longoff
- offset to the first byte in the arraylittle
- true if the bytes are provided in little-endian orderpublic static long bytesToLong(byte[] bytes, boolean little)
bytes
- array of bytes to be translated to a longlittle
- true if the bytes are provided in little-endian orderpublic static long bytesToLong(short[] bytes, int off, int len, boolean little)
bytes
- array of unsigned bytes to be translated to a longoff
- offset to the first byte in the arraylen
- number of bytes to uselittle
- true if the bytes are provided in little-endian orderpublic static long bytesToLong(short[] bytes, int off, boolean little)
bytes
- array of unsigned bytes to be translated to a longoff
- offset to the first byte in the arraylittle
- true if the bytes are provided in little-endian orderpublic static long bytesToLong(short[] bytes, boolean little)
bytes
- array of unsigned bytes to be translated to a longlittle
- true if the bytes are provided in little-endian orderpublic static double bytesToDouble(byte[] bytes, int off, int len, boolean little)
bytes
- array of bytes to be translated to a doubleoff
- offset to the first byte in the arraylen
- number of bytes to uselittle
- true if the bytes are provided in little-endian orderpublic static double bytesToDouble(byte[] bytes, int off, boolean little)
bytes
- array of bytes to be translated to a doubleoff
- offset to the first byte in the arraylittle
- true if the bytes are provided in little-endian orderpublic static double bytesToDouble(byte[] bytes, boolean little)
bytes
- array of bytes to be translated to a doublelittle
- true if the bytes are provided in little-endian orderpublic static double bytesToDouble(short[] bytes, int off, int len, boolean little)
bytes
- array of unsigned bytes to be translated to a doubleoff
- offset to the first byte in the arraylen
- number of bytes to uselittle
- true if the bytes are provided in little-endian orderpublic static double bytesToDouble(short[] bytes, int off, boolean little)
bytes
- array of unsigned bytes to be translated to a doubleoff
- offset to the first byte in the arraylittle
- true if the bytes are provided in little-endian orderpublic static double bytesToDouble(short[] bytes, boolean little)
bytes
- array of unsigned bytes to be translated to a doublelittle
- true if the bytes are provided in little-endian orderpublic static String bytesToHex(byte[] b)
b
- the array of bytes to parse2 * b.length
representing the
hexadecimal digits of each byte in b
concatenatedpublic static Short parseShort(String value)
value
- a String representation of a short valuenull
if the string could not be parsedpublic static Byte parseByte(String value)
value
- a String representation of a byte valuenull
if the string could not be parsedpublic static Integer parseInteger(String value)
value
- a String representation of an int valuenull
if the string could not be parsedpublic static Long parseLong(String value)
value
- a String representation of a long valuenull
if the string could not be parsedpublic static Float parseFloat(String value)
value
- a String representation of a float valuenull
if the string could not be parsedpublic static Double parseDouble(String value)
value
- a String representation of a double valuenull
if the string could not be parsed@Deprecated public static String sanitizeDouble(String value)
parseDouble(String)
insteadvalue
- the String representation of a double value,
which may contain invalid characters that prevent parsingpublic static byte[] shortToBytes(short value, boolean little)
value
- the short to be split into byteslittle
- true if the returned bytes should be in little-endian orderpublic static byte[] intToBytes(int value, boolean little)
value
- the int to be split into byteslittle
- true if the returned bytes should be in little-endian orderpublic static byte[] floatToBytes(float value, boolean little)
value
- the float to be split into byteslittle
- true if the returned bytes should be in little-endian orderpublic static byte[] longToBytes(long value, boolean little)
value
- the long to be split into byteslittle
- true if the returned bytes should be in little-endian orderpublic static byte[] doubleToBytes(double value, boolean little)
value
- the double to be split into byteslittle
- true if the returned bytes should be in little-endian orderpublic static byte[] shortsToBytes(short[] values, boolean little)
values
- the shorts to be split into byteslittle
- true if the returned bytes should be in little-endian order2 * values.length
public static byte[] intsToBytes(int[] values, boolean little)
values
- the ints to be split into byteslittle
- true if the returned bytes should be in little-endian order4 * values.length
public static byte[] floatsToBytes(float[] values, boolean little)
values
- the floats to be split into byteslittle
- true if the returned bytes should be in little-endian order4 * values.length
public static byte[] longsToBytes(long[] values, boolean little)
values
- the longs to be split into byteslittle
- true if the returned bytes should be in little-endian order8 * values.length
public static byte[] doublesToBytes(double[] values, boolean little)
values
- the doubles to be split into byteslittle
- true if the returned bytes should be in little-endian order8 * values.length
public static void unpackBytes(long value, byte[] buf, int ndx, int nBytes, boolean little)
value
- the long to be split into bytesbuf
- the byte array in which to store the unpacked bytesndx
- the offset to the first byte in the arraynBytes
- the number of unpacked byteslittle
- true if the unpacked bytes should be in little-endian orderIllegalArgumentException
- if the specified indices fall outside the bufferpublic static Object makeDataArray(byte[] b, int bpp, boolean fp, boolean little)
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.public static Object makeDataArray2D(byte[] b, int bpp, boolean fp, boolean little, int height)
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).IllegalArgumentException
- if input byte array does not divide
evenly into height piecespublic static short swap(short x)
x
- short value to byte swapx
public static char swap(char x)
x
- char value to byte swapx
public static int swap(int x)
x
- int value to byte swapx
public static long swap(long x)
x
- long value to byte swapx
public static float swap(float x)
x
- float value to byte swapx
public static double swap(double x)
x
- double value to byte swapx
public static String stripString(String toStrip)
toStrip
- String from which to remove null bytestoStrip
with all
null (0) bytes removedpublic static boolean samePrefix(String s1, String s2)
s1
- first String filename to compares2
- second String filename to comparepublic static String sanitize(String s)
s
- String from which to remove unprintable characterss
with tabs, newlines,
and control characters removedCharacter.isISOControl(char)
public static float[] normalizeFloats(float[] data)
data
- array of float
values to normalizefloat
values in the range
[0.0, 1.0]
public static double[] normalizeDoubles(double[] data)
data
- array of double
values to normalizedouble
values in the range
[0.0, 1.0]
public static byte[] allocate(int... sizes) throws IllegalArgumentException
sizes
- list of sizes from which to allocate the arrayIllegalArgumentException
- if the total size exceeds 2GB, which is
the maximum size of an array in Java; or if any size argument is
zero or negativepublic static int safeMultiply32(int... sizes) throws IllegalArgumentException
Integer.MAX_VALUE
).sizes
- list of sizes from which to compute the productIllegalArgumentException
- if the total size exceeds 2GiB, which is
the maximum size of an int in Java; or if any size argument is
zero or negativepublic static long safeMultiply64(long... sizes) throws IllegalArgumentException
Long.MAX_VALUE
).sizes
- list of sizes from which to compute the productIllegalArgumentException
- if the total size exceeds 8EiB, which is
the maximum size of a long in Java; or if any size argument is
zero or negativepublic static boolean containsValue(int[] array, int value)
array
- an array of ints to searchvalue
- the int for which to searcharray
contains at least one occurence of
value
indexOf(int[], int)
public static int indexOf(int[] array, int value)
array
- an array of ints to searchvalue
- the int for which to searchvalue
in
array
, or -1 if value
is not foundpublic static int indexOf(Object[] array, Object value)
array
- an array of Objects to searchvalue
- the Object for which to searchvalue
in
array
, or -1 if value
is not foundpublic static byte[] makeSigned(byte[] b)
public static short[] makeSigned(short[] s)
public static int[] makeSigned(int[] i)
Copyright © 2005–2024 Open Microscopy Environment. All rights reserved.