Package org.scijava.util
Class Bytes
- java.lang.Object
-
- org.scijava.util.Bytes
-
public final class Bytes extends Object
Useful methods for reading, writing, decoding and convertingbytes andbytearrays.- Author:
- Curtis Rueden, Melissa Linkert, Chris Allan
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]fromDouble(double value, boolean little)Translates thedoublevalue into an array of eightbytes.static byte[]fromDoubles(double[] values, boolean little)Translates an array ofdoublevalues into an array ofbytevalues.static byte[]fromFloat(float value, boolean little)Translates thefloatvalue into an array of fourbytes.static byte[]fromFloats(float[] values, boolean little)Translates an array offloatvalues into an array ofbytevalues.static byte[]fromInt(int value, boolean little)Translates theintvalue into an array of fourbytes.static byte[]fromInts(int[] values, boolean little)Translates an array ofintvalues into an array ofbytevalues.static byte[]fromLong(long value, boolean little)Translates thelongvalue into an array of eightbytes.static byte[]fromLongs(long[] values, boolean little)Translates an array oflongvalues into an array ofbytevalues.static byte[]fromShort(short value, boolean little)Translates theshortvalue into an array of twobytes.static byte[]fromShorts(short[] values, boolean little)Translates an array ofshortvalues into an array ofbytevalues.static ObjectmakeArray(byte[] b, int bpp, boolean fp, boolean little)Converts abytearray to the appropriate 1D primitive type array.static ObjectmakeArray2D(byte[] b, int bpp, boolean fp, boolean little, int height)Converts abytearray 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 givendoublearray so that the minimum value maps to 0.0 and the maximum value maps to 1.0.static float[]normalize(float[] data)Normalize the givenfloatarray so that the minimum value maps to 0.0 and the maximum value maps to 1.0.static charswap(char x)static doubleswap(double x)static floatswap(float x)static intswap(int x)static longswap(long x)static shortswap(short x)static doubletoDouble(byte[] bytes, boolean little)Translates up to the first 8 bytes of abytearray to adouble.static doubletoDouble(byte[] bytes, int off, boolean little)Translates up to the first 8 bytes of abytearray beyond the given offset to adouble.static doubletoDouble(byte[] bytes, int off, int len, boolean little)Translates up to the firstlenbytes of abytearray beyond the given offset to adouble.static doubletoDouble(short[] bytes, boolean little)Translates up to the first 8 bytes of abytearray to adouble.static doubletoDouble(short[] bytes, int off, boolean little)Translates up to the first 8 bytes of abytearray beyond the given offset to adouble.static doubletoDouble(short[] bytes, int off, int len, boolean little)Translates up to the firstlenbytes of abytearray beyond the given offset to adouble.static floattoFloat(byte[] bytes, boolean little)Translates up to the first 4 bytes of abytearray to afloat.static floattoFloat(byte[] bytes, int off, boolean little)Translates up to the first 4 bytes of abytearray beyond a given offset to afloat.static floattoFloat(byte[] bytes, int off, int len, boolean little)Translates up to the firstlenbytes of abytearray beyond the given offset to afloat.static floattoFloat(short[] bytes, boolean little)Translates up to the first 4 bytes of abytearray to afloat.static floattoFloat(short[] bytes, int off, boolean little)Translates up to the first 4 bytes of abytearray beyond a given offset to afloat.static floattoFloat(short[] bytes, int off, int len, boolean little)Translates up to the firstlenbytes of abytearray beyond a given offset to afloat.static inttoInt(byte[] bytes, boolean little)Translates up to the first 4 bytes of abytearray to anint.static inttoInt(byte[] bytes, int off, boolean little)Translates up to the first 4 bytes of abytearray beyond the given offset to anint.static inttoInt(byte[] bytes, int off, int len, boolean little)Translates up to the firstlenbytes of abytearray beyond the given offset to anint.static inttoInt(short[] bytes, boolean little)Translates up to the first 4 bytes of abytearray to anint.static inttoInt(short[] bytes, int off, boolean little)Translates up to the first 4 bytes of abytearray beyond the given offset to anint.static inttoInt(short[] bytes, int off, int len, boolean little)Translates up to the firstlenbytes of abytearray beyond the given offset to anint.static longtoLong(byte[] bytes, boolean little)Translates up to the first 8 bytes of abytearray to along.static longtoLong(byte[] bytes, int off, boolean little)Translates up to the first 8 bytes of abytearray beyond the given offset to along.static longtoLong(byte[] bytes, int off, int len, boolean little)Translates up to the firstlenbytes of abytearray beyond the given offset to along.static longtoLong(short[] bytes, boolean little)Translates up to the first 8 bytes of abytearray to along.static longtoLong(short[] bytes, int off, boolean little)Translates up to the first 8 bytes of abytearray beyond the given offset to along.static longtoLong(short[] bytes, int off, int len, boolean little)Translates up to the firstlenbytes of abytearray beyond the given offset to along.static shorttoShort(byte[] bytes, boolean little)Translates up to the first 2 bytes of abytearray to ashort.static shorttoShort(byte[] bytes, int off, boolean little)Translates up to the first 2 bytes of abytearray beyond the given offset to ashort.static shorttoShort(byte[] bytes, int off, int len, boolean little)Translates up to the firstlenbytes of abytearray beyond the given offset to ashort.static shorttoShort(short[] bytes, boolean little)Translates up to the first 2 bytes of abytearray to ashort.static shorttoShort(short[] bytes, int off, boolean little)Translates up to the first 2 bytes of abytearray beyond the given offset to ashort.static shorttoShort(short[] bytes, int off, int len, boolean little)Translates up to the firstlenbytes of abytearray beyond the given offset to ashort.static voidunpack(long value, byte[] buf, int ndx, int nBytes, boolean little)TranslatesnBytesof the givenlongand places the result in the givenbytearray.
-
-
-
Method Detail
-
toShort
public static short toShort(byte[] bytes, int off, int len, boolean little)Translates up to the firstlenbytes of abytearray beyond the given offset to ashort. If there are fewer thanlenbytes 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 abytearray beyond the given offset to ashort. 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 abytearray to ashort. 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 firstlenbytes of abytearray beyond the given offset to ashort. If there are fewer thanlenbytes 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 abytearray beyond the given offset to ashort. 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 abytearray to ashort. 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 firstlenbytes of abytearray beyond the given offset to anint. If there are fewer thanlenbytes 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 abytearray beyond the given offset to anint. 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 abytearray to anint. 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 firstlenbytes of abytearray beyond the given offset to anint. If there are fewer thanlenbytes 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 abytearray beyond the given offset to anint. 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 abytearray to anint. 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 firstlenbytes of abytearray beyond the given offset to afloat. If there are fewer thanlenbytes 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 abytearray beyond a given offset to afloat. 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 abytearray to afloat. 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 firstlenbytes of abytearray beyond a given offset to afloat. If there are fewer thanlenbytes 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 abytearray beyond a given offset to afloat. 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 abytearray to afloat. 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 firstlenbytes of abytearray beyond the given offset to along. If there are fewer thanlenbytes 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 abytearray beyond the given offset to along. 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 abytearray to along. 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 firstlenbytes of abytearray beyond the given offset to along. If there are fewer thanlenbytes 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 abytearray beyond the given offset to along. 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 abytearray to along. 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 firstlenbytes of abytearray beyond the given offset to adouble. If there are fewer thanlenbytes 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 abytearray beyond the given offset to adouble. 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 abytearray to adouble. 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 firstlenbytes of abytearray beyond the given offset to adouble. If there are fewer thanlenbytes 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 abytearray beyond the given offset to adouble. 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 abytearray to adouble. 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 theshortvalue into an array of twobytes.
-
fromInt
public static byte[] fromInt(int value, boolean little)Translates theintvalue into an array of fourbytes.
-
fromFloat
public static byte[] fromFloat(float value, boolean little)Translates thefloatvalue into an array of fourbytes.
-
fromLong
public static byte[] fromLong(long value, boolean little)Translates thelongvalue into an array of eightbytes.
-
fromDouble
public static byte[] fromDouble(double value, boolean little)Translates thedoublevalue into an array of eightbytes.
-
fromShorts
public static byte[] fromShorts(short[] values, boolean little)Translates an array ofshortvalues into an array ofbytevalues.
-
fromInts
public static byte[] fromInts(int[] values, boolean little)Translates an array ofintvalues into an array ofbytevalues.
-
fromFloats
public static byte[] fromFloats(float[] values, boolean little)Translates an array offloatvalues into an array ofbytevalues.
-
fromLongs
public static byte[] fromLongs(long[] values, boolean little)Translates an array oflongvalues into an array ofbytevalues.
-
fromDoubles
public static byte[] fromDoubles(double[] values, boolean little)Translates an array ofdoublevalues into an array ofbytevalues.
-
unpack
public static void unpack(long value, byte[] buf, int ndx, int nBytes, boolean little)TranslatesnBytesof the givenlongand places the result in the givenbytearray.- Throws:
IllegalArgumentException- if the specified indices fall outside the buffer
-
makeArray
public static Object makeArray(byte[] b, int bpp, boolean fp, boolean little)
Converts abytearray 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 typeshort).fp- If set and bpp == 4 or bpp == 8, then returnfloats ordoubles.little- Whetherbytearray is in little-endian order.
-
makeArray2D
public static Object makeArray2D(byte[] b, int bpp, boolean fp, boolean little, int height)
Converts abytearray 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 typeshort).fp- If set and bpp == 4 or bpp == 8, then returnfloats ordoubles.little- Whetherbytearray 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 inputbytearray 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 givenfloatarray 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 givendoublearray 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)
-
-