loci.formats
Class DataTools

java.lang.Object
  extended by loci.formats.DataTools

public final class DataTools
extends Object

A utility class with convenience methods for reading, writing and decoding words.

Source code:
Trac, SVN

Author:
Curtis Rueden ctrueden at wisc.edu, Chris Allan callan at blackcat.ca, Melissa Linkert linkert at wisc.edu

Field Summary
static int COBOL
           
static long COBOL_EPOCH
           
static int UNIX
          Timestamp formats.
static long UNIX_EPOCH
          Milliseconds until UNIX epoch.
 
Method Summary
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 String convertDate(long stamp, int format)
          Converts the given timestamp into an ISO 8061 date.
static Object makeDataArray(byte[] b, int bpp, boolean fp, boolean little)
          Convert a byte array to the appropriate primitive type array.
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 short read2SignedBytes(DataInput in, boolean little)
          Reads 2 signed bytes [-32768, 32767].
static int read2UnsignedBytes(DataInput in, boolean little)
          Reads 2 unsigned bytes [0, 65535].
static int read4SignedBytes(DataInput in, boolean little)
          Reads 4 signed bytes [-2147483648, 2147483647].
static long read4UnsignedBytes(DataInput in, boolean little)
          Reads 4 unsigned bytes [0, 4294967296].
static long read8SignedBytes(DataInput in, boolean little)
          Reads 8 signed bytes [-9223372036854775808, 9223372036854775807].
static double readDouble(DataInput in, boolean little)
          Reads 8 bytes in double precision IEEE format.
static float readFloat(DataInput in, boolean little)
          Reads 4 bytes in single precision IEEE format.
static byte readSignedByte(DataInput in)
          Reads 1 signed byte [-128, 127].
static short readUnsignedByte(DataInput in)
          Reads 1 unsigned byte [0, 255].
static boolean samePrefix(String s1, String s2)
          Check if two filenames have the same prefix.
static String stripString(String toStrip)
          Remove null bytes from a string.
static char swap(char x)
           
static int swap(int x)
           
static long swap(long x)
           
static short swap(short x)
           
static void writeInt(DataOutput out, int v, boolean little)
          Writes an integer to the given data output destination.
static void writeShort(DataOutput out, int v, boolean little)
          Writes a short to the given data output destination.
static void writeString(DataOutput out, String s)
          Writes a string to the given data output destination.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UNIX

public static final int UNIX
Timestamp formats.

See Also:
Constant Field Values

COBOL

public static final int COBOL
See Also:
Constant Field Values

UNIX_EPOCH

public static final long UNIX_EPOCH
Milliseconds until UNIX epoch.

See Also:
Constant Field Values

COBOL_EPOCH

public static final long COBOL_EPOCH
See Also:
Constant Field Values
Method Detail

readSignedByte

public static byte readSignedByte(DataInput in)
                           throws IOException
Reads 1 signed byte [-128, 127].

Throws:
IOException

readUnsignedByte

public static short readUnsignedByte(DataInput in)
                              throws IOException
Reads 1 unsigned byte [0, 255].

Throws:
IOException

read2SignedBytes

public static short read2SignedBytes(DataInput in,
                                     boolean little)
                              throws IOException
Reads 2 signed bytes [-32768, 32767].

Throws:
IOException

read2UnsignedBytes

public static int read2UnsignedBytes(DataInput in,
                                     boolean little)
                              throws IOException
Reads 2 unsigned bytes [0, 65535].

Throws:
IOException

read4SignedBytes

public static int read4SignedBytes(DataInput in,
                                   boolean little)
                            throws IOException
Reads 4 signed bytes [-2147483648, 2147483647].

Throws:
IOException

read4UnsignedBytes

public static long read4UnsignedBytes(DataInput in,
                                      boolean little)
                               throws IOException
Reads 4 unsigned bytes [0, 4294967296].

Throws:
IOException

read8SignedBytes

public static long read8SignedBytes(DataInput in,
                                    boolean little)
                             throws IOException
Reads 8 signed bytes [-9223372036854775808, 9223372036854775807].

Throws:
IOException

readFloat

public static float readFloat(DataInput in,
                              boolean little)
                       throws IOException
Reads 4 bytes in single precision IEEE format.

Throws:
IOException

readDouble

public static double readDouble(DataInput in,
                                boolean little)
                         throws IOException
Reads 8 bytes in double precision IEEE format.

Throws:
IOException

writeString

public static void writeString(DataOutput out,
                               String s)
                        throws IOException
Writes a string to the given data output destination.

Throws:
IOException

writeInt

public static void writeInt(DataOutput out,
                            int v,
                            boolean little)
                     throws IOException
Writes an integer to the given data output destination.

Throws:
IOException

writeShort

public static void writeShort(DataOutput out,
                              int v,
                              boolean little)
                       throws IOException
Writes a short to the given data output destination.

Throws:
IOException

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 2 bytes in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 2 bytes in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 4 bytes in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 4 bytes in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 8 bytes in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 in the array, the MSBs are all assumed to be zero (regardless of endianness).


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 8 bytes to be translated, the MSBs are all assumed to be zero (regardless of endianness).


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 to be translated, the MSBs are all assumed to be zero (regardless of endianness).


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 in the array, the MSBs are all assumed to be zero (regardless of endianness).


makeDataArray

public static Object makeDataArray(byte[] b,
                                   int bpp,
                                   boolean fp,
                                   boolean little)
Convert a byte array to the appropriate 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.

swap

public static short swap(short x)

swap

public static char swap(char x)

swap

public static int swap(int x)

swap

public static long swap(long x)

stripString

public static String stripString(String toStrip)
Remove null bytes from a string.


samePrefix

public static boolean samePrefix(String s1,
                                 String s2)
Check if two filenames have the same prefix.


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.


convertDate

public static String convertDate(long stamp,
                                 int format)
Converts the given timestamp into an ISO 8061 date.