org.scijava.util
Class Bytes

java.lang.Object
  extended by org.scijava.util.Bytes

public final class Bytes
extends Object

Useful methods for reading, writing, decoding and converting bytes and byte arrays.

Author:
Curtis Rueden, Melissa Linkert, Chris Allan

Method Summary
static byte[] fromDouble(double value, boolean little)
          Translates the double value into an array of eight bytes.
static byte[] fromDoubles(double[] values, boolean little)
          Translates an array of double values into an array of byte values.
static byte[] fromFloat(float value, boolean little)
          Translates the float value into an array of four bytes.
static byte[] fromFloats(float[] values, boolean little)
          Translates an array of float values into an array of byte values.
static byte[] fromInt(int value, boolean little)
          Translates the int value into an array of four bytes.
static byte[] fromInts(int[] values, boolean little)
          Translates an array of int values into an array of byte values.
static byte[] fromLong(long value, boolean little)
          Translates the long value into an array of eight bytes.
static byte[] fromLongs(long[] values, boolean little)
          Translates an array of long values into an array of byte values.
static byte[] fromShort(short value, boolean little)
          Translates the short value into an array of two bytes.
static byte[] fromShorts(short[] values, boolean little)
          Translates an array of short values into an array of byte values.
static Object makeArray(byte[] b, int bpp, boolean fp, boolean little)
          Converts a byte array to the appropriate 1D primitive type array.
static Object makeArray2D(byte[] b, int bpp, boolean fp, boolean little, int height)
          Converts 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[] normalize(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[] normalize(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 char swap(char x)
           
static double swap(double x)
           
static float swap(float x)
           
static int swap(int x)
           
static long swap(long x)
           
static short swap(short x)
           
static double toDouble(byte[] bytes, boolean little)
          Translates up to the first 8 bytes of a byte array to a double.
static double toDouble(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 toDouble(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 toDouble(short[] bytes, boolean little)
          Translates up to the first 8 bytes of a byte array to a double.
static double toDouble(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 toDouble(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 toFloat(byte[] bytes, boolean little)
          Translates up to the first 4 bytes of a byte array to a float.
static float toFloat(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 toFloat(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 toFloat(short[] bytes, boolean little)
          Translates up to the first 4 bytes of a byte array to a float.
static float toFloat(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 toFloat(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 int toInt(byte[] bytes, boolean little)
          Translates up to the first 4 bytes of a byte array to an int.
static int toInt(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 toInt(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 toInt(short[] bytes, boolean little)
          Translates up to the first 4 bytes of a byte array to an int.
static int toInt(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 toInt(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 toLong(byte[] bytes, boolean little)
          Translates up to the first 8 bytes of a byte array to a long.
static long toLong(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 toLong(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 toLong(short[] bytes, boolean little)
          Translates up to the first 8 bytes of a byte array to a long.
static long toLong(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 toLong(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 toShort(byte[] bytes, boolean little)
          Translates up to the first 2 bytes of a byte array to a short.
static short toShort(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 toShort(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 toShort(short[] bytes, boolean little)
          Translates up to the first 2 bytes of a byte array to a short.
static short toShort(short[] 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 toShort(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 short.
static void unpack(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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toShort

public static short toShort(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).


toShort

public static short toShort(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).


toShort

public static short toShort(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).


toShort

public static short toShort(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 short. If there are fewer than len bytes available, the MSBs are all assumed to be zero (regardless of endianness).


toShort

public static short toShort(short[] 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).


toShort

public static short toShort(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).


toInt

public static int toInt(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).


toInt

public static int toInt(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).


toInt

public static int toInt(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).


toInt

public static int toInt(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).


toInt

public static int toInt(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).


toInt

public static int toInt(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).


toFloat

public static float toFloat(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).


toFloat

public static float toFloat(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).


toFloat

public static float toFloat(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).


toFloat

public static float toFloat(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).


toFloat

public static float toFloat(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).


toFloat

public static float toFloat(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).


toLong

public static long toLong(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).


toLong

public static long toLong(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).


toLong

public static long toLong(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).


toLong

public static long toLong(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).


toLong

public static long toLong(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).


toLong

public static long toLong(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).


toDouble

public static double toDouble(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).


toDouble

public static double toDouble(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).


toDouble

public static double toDouble(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).


toDouble

public static double toDouble(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).


toDouble

public static double toDouble(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).


toDouble

public static double toDouble(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).


fromShort

public static byte[] fromShort(short value,
                               boolean little)
Translates the short value into an array of two bytes.


fromInt

public static byte[] fromInt(int value,
                             boolean little)
Translates the int value into an array of four bytes.


fromFloat

public static byte[] fromFloat(float value,
                               boolean little)
Translates the float value into an array of four bytes.


fromLong

public static byte[] fromLong(long value,
                              boolean little)
Translates the long value into an array of eight bytes.


fromDouble

public static byte[] fromDouble(double value,
                                boolean little)
Translates the double value into an array of eight bytes.


fromShorts

public static byte[] fromShorts(short[] values,
                                boolean little)
Translates an array of short values into an array of byte values.


fromInts

public static byte[] fromInts(int[] values,
                              boolean little)
Translates an array of int values into an array of byte values.


fromFloats

public static byte[] fromFloats(float[] values,
                                boolean little)
Translates an array of float values into an array of byte values.


fromLongs

public static byte[] fromLongs(long[] values,
                               boolean little)
Translates an array of long values into an array of byte values.


fromDoubles

public static byte[] fromDoubles(double[] values,
                                 boolean little)
Translates an array of double values into an array of byte values.


unpack

public static void unpack(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.

Throws:
IllegalArgumentException - if the specified indices fall outside the buffer

makeArray

public static Object makeArray(byte[] b,
                               int bpp,
                               boolean fp,
                               boolean little)
Converts 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.

makeArray2D

public static Object makeArray2D(byte[] b,
                                 int bpp,
                                 boolean fp,
                                 boolean little,
                                 int height)
Converts 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)

swap

public static char swap(char x)

swap

public static int swap(int x)

swap

public static long swap(long x)

swap

public static float swap(float x)

swap

public static double swap(double x)

normalize

public static float[] normalize(float[] data)
Normalize the given float array so that the minimum value maps to 0.0 and the maximum value maps to 1.0.


normalize

public static double[] normalize(double[] data)
Normalize the given double array so that the minimum value maps to 0.0 and the maximum value maps to 1.0.


makeSigned

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

makeSigned

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

makeSigned

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


Copyright © 2009–2015 SciJava. All rights reserved.