Class ByteUtils
- java.lang.Object
-
- org.hortonmachine.gears.io.dxfdwg.libs.dwg.utils.ByteUtils
-
public class ByteUtils extends Object
Clase que engloba mtodos para trabajar con bytes.- Author:
- Vicente Caballero Navarro
-
-
Field Summary
Fields Modifier and Type Field Description static char[]digitsA nibble->char mapping for printing out bytes.static intSIZE_BOOLstatic intSIZE_DOUBLEstatic intSIZE_INTstatic intSIZE_LONGstatic intSIZE_SHORT
-
Constructor Summary
Constructors Constructor Description ByteUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidbigIntegerToBytes(BigInteger n, byte[] data, int[] offset)Write the bytes representingninto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.static voidboolToBytes(boolean b, byte[] data, int[] offset)Write the bytes representingbinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.static BigIntegerbytesToBigInteger(byte[] data, int[] offset)Return theBigIntegerrepresented by the bytes indatastaring at offsetoffset[0].static booleanbytesToBool(byte[] data, int[] offset)Return thebooleanrepresented by the bytes indatastaring at offsetoffset[0].static doublebytesToDouble(byte[] data, int[] offset)Return thedoublerepresented by the bytes indatastaring at offsetoffset[0].static intbytesToInt(byte[] data, int[] offset)Return theintrepresented by the bytes indatastaring at offsetoffset[0].static voidbytesToInts(int[] dst, int dst_offset, byte[] src, int src_offset, int length)Convert an array ofbytess into an array ofints.static longbytesToLong(byte[] data, int[] offset)Return thelongrepresented by the bytes indatastaring at offsetoffset[0].static shortbytesToShort(byte[] data, int[] offset)Return theshortrepresented by the bytes indatastaring at offsetoffset[0].static StringbytesToString(byte[] data, int[] offset)Return theStringrepresented by the bytes indatastaring at offsetoffset[0].static intbyteToUnsignedInt(byte b)Convert abyteinto an unsigned integer.static voiddoubleToBytes(double d, byte[] data, int[] offset)Write the bytes representingdinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.static booleanequals(byte[] b1, byte[] b2)Determines whether two arrays ofbytes contain the same contents.static intgetUnsigned(byte b)DOCUMENT ME!static voidintsToBytes(byte[] dst, int dst_offset, int[] src, int src_offset, int length)Convert an array ofints into an array ofbytes.static voidintToBytes(int i, byte[] data, int[] offset)Write the bytes representingiinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.static voidlongToBytes(long l, byte[] data, int[] offset)Write the bytes representinglinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.static voidmemclr(byte[] array, int offset, int length)Fill the given array with zeros.static booleanmemcmp(byte[] a, int a_offset, byte[] b, int b_offset, int length)Compare the contents of one array ofbytesto another.static intmemcpy(byte[] dst, int dst_offset, byte[] src, int src_offset, int length)Copy contents of one array ofbytesinto another.static Stringprint_bytes(byte[] data)DOCUMENT ME!static Stringprint_bytes(byte[] data, int offset, int length)Produce aStringrepresentation for the specified array ofbytes.static Stringprint_bytes_exact(byte[] data, int offset, int length)DOCUMENT ME!static intround_up(int value, int multiple)Round a number up to a given multiple.static voidshortToBytes(short s, byte[] data, int[] offset)Write the bytes representingsinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.static voidstringToBytes(String s, byte[] data, int[] offset)Write the bytes representingsinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.static byte[]zero_pad(byte[] original, int block_size)Return a new array equal to original except zero-padded to an integral mulitple of blocks.
-
-
-
Field Detail
-
SIZE_BOOL
public static final int SIZE_BOOL
- See Also:
- Constant Field Values
-
SIZE_SHORT
public static final int SIZE_SHORT
- See Also:
- Constant Field Values
-
SIZE_INT
public static final int SIZE_INT
- See Also:
- Constant Field Values
-
SIZE_LONG
public static final int SIZE_LONG
- See Also:
- Constant Field Values
-
SIZE_DOUBLE
public static final int SIZE_DOUBLE
- See Also:
- Constant Field Values
-
digits
public static final char[] digits
A nibble->char mapping for printing out bytes.
-
-
Method Detail
-
bytesToInt
public static final int bytesToInt(byte[] data, int[] offset)Return theintrepresented by the bytes indatastaring at offsetoffset[0].- Parameters:
data- the array from which to readoffset- A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.- Returns:
- the value of the
intdecoded
-
intToBytes
public static final void intToBytes(int i, byte[] data, int[] offset)Write the bytes representingiinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.- Parameters:
i- theintto encodedata- The byte array to store into, ornull.offset- A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
-
bytesToShort
public static final short bytesToShort(byte[] data, int[] offset)Return theshortrepresented by the bytes indatastaring at offsetoffset[0].- Parameters:
data- the array from which to readoffset- A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.- Returns:
- the value of the
shortdecoded
-
shortToBytes
public static final void shortToBytes(short s, byte[] data, int[] offset)Write the bytes representingsinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.- Parameters:
s- theshortto encodedata- The byte array to store into, ornull.offset- A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
-
bytesToLong
public static final long bytesToLong(byte[] data, int[] offset)Return thelongrepresented by the bytes indatastaring at offsetoffset[0].- Parameters:
data- the array from which to readoffset- A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.- Returns:
- the value of the
longdecoded
-
longToBytes
public static final void longToBytes(long l, byte[] data, int[] offset)Write the bytes representinglinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.- Parameters:
l- thelongto encodedata- The byte array to store into, ornull.offset- A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
-
bytesToDouble
public static final double bytesToDouble(byte[] data, int[] offset)Return thedoublerepresented by the bytes indatastaring at offsetoffset[0].- Parameters:
data- the array from which to readoffset- A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.- Returns:
- the value of the
doubledecoded
-
doubleToBytes
public static final void doubleToBytes(double d, byte[] data, int[] offset)Write the bytes representingdinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.- Parameters:
d- thedoubleto encodedata- The byte array to store into, ornull.offset- A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
-
bytesToString
public static final String bytesToString(byte[] data, int[] offset)
Return theStringrepresented by the bytes indatastaring at offsetoffset[0]. This method relies on the user using the correspondingstringToBytesmethod to encode theString, so that it may properly retrieve theStringlength.- Parameters:
data- the array from which to readoffset- A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.- Returns:
- the value of the
Stringdecoded
-
stringToBytes
public static final void stringToBytes(String s, byte[] data, int[] offset)
Write the bytes representingsinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.- Parameters:
s- theStringto encodedata- The byte array to store into, ornull.offset- A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
-
bytesToBool
public static final boolean bytesToBool(byte[] data, int[] offset)Return thebooleanrepresented by the bytes indatastaring at offsetoffset[0].- Parameters:
data- the array from which to readoffset- A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.- Returns:
- the value of the
booleandecoded
-
boolToBytes
public static final void boolToBytes(boolean b, byte[] data, int[] offset)Write the bytes representingbinto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.- Parameters:
b- thebooleanto encodedata- The byte array to store into, ornull.offset- A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
-
bytesToBigInteger
public static final BigInteger bytesToBigInteger(byte[] data, int[] offset)
Return theBigIntegerrepresented by the bytes indatastaring at offsetoffset[0].- Parameters:
data- the array from which to readoffset- A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.- Returns:
- the
BigIntegerdecoded
-
bigIntegerToBytes
public static final void bigIntegerToBytes(BigInteger n, byte[] data, int[] offset)
Write the bytes representingninto the byte arraydata, starting at indexoffset [0], and incrementoffset [0]by the number of bytes written; ifdata == null, incrementoffset [0]by the number of bytes that would have been written otherwise.- Parameters:
n- theBigIntegerto encodedata- The byte array to store into, ornull.offset- A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
-
bytesToInts
public static final void bytesToInts(int[] dst, int dst_offset, byte[] src, int src_offset, int length)Convert an array ofbytess into an array ofints.- Parameters:
dst- the array to writedst_offset- the start offset indst, times 4. This measures the offset as ifdstwere an array ofbytes (rather thanints).src- the array to readsrc_offset- the start offset insrclength- the number ofbytes to copy.
-
intsToBytes
public static final void intsToBytes(byte[] dst, int dst_offset, int[] src, int src_offset, int length)Convert an array ofints into an array ofbytes.- Parameters:
dst- the array to writedst_offset- the start offset indstsrc- the array to readsrc_offset- the start offset insrc, times 4. This measures the offset as ifsrcwere an array ofbytes (rather thanints).length- the number ofbytes to copy.
-
byteToUnsignedInt
public static final int byteToUnsignedInt(byte b)
Convert abyteinto an unsigned integer.- Parameters:
b- thebyteto cast- Returns:
- a postiive
intwhose lowest byte contains the bits ofb.
-
memcpy
public static int memcpy(byte[] dst, int dst_offset, byte[] src, int src_offset, int length)Copy contents of one array ofbytesinto another. If either array isnull, simply return thelengthparameter directly.- Parameters:
dst- the array to write, ornulldst_offset- the start offset indstsrc- the array to read, ornullsrc_offset- the start offset insrclength- the number ofbytes to copy.- Returns:
- DOCUMENT ME!
-
memcmp
public static boolean memcmp(byte[] a, int a_offset, byte[] b, int b_offset, int length)Compare the contents of one array ofbytesto another.- Parameters:
a- the first arraya_offset- the start offset inab- the second arrayb_offset- the start offset inblength- the number ofbytes to compare.- Returns:
- DOCUMENT ME!
-
memclr
public static void memclr(byte[] array, int offset, int length)Fill the given array with zeros.- Parameters:
array- the array to clearoffset- the start offsetlength- the number ofbytes to clear.
-
round_up
public static int round_up(int value, int multiple)Round a number up to a given multiple.- Parameters:
value- the number to be roundedmultiple- the number to which to be rounded- Returns:
- the smallest
intgreater than or equal tovaluewhich dividesmultipleexactly.
-
zero_pad
public static byte[] zero_pad(byte[] original, int block_size)Return a new array equal to original except zero-padded to an integral mulitple of blocks. If the original is already an integral multiple of blocks, just return it.- Parameters:
original- the array ofbytes to be paddedblock_size- the size of the blocks- Returns:
- an array whose size divides
block_sizeexactly. The array is eitheroriginalitself, or a copy whose firstoriginal.lengthbytes are equal tooriginal.
-
equals
public static boolean equals(byte[] b1, byte[] b2)Determines whether two arrays ofbytes contain the same contents.- Parameters:
b1- The first arrayb2- The second array- Returns:
trueif both arrays arenull, both empty, or both of the same length with equal contents.
-
print_bytes
public static String print_bytes(byte[] data, int offset, int length)
Produce aStringrepresentation for the specified array ofbytes. Print eachbyteas two hexadecimal digits.- Parameters:
data- The array to printoffset- the start offset indatalength- the number ofbytes to print- Returns:
- DOCUMENT ME!
-
print_bytes_exact
public static String print_bytes_exact(byte[] data, int offset, int length)
DOCUMENT ME!- Parameters:
data- DOCUMENT ME!offset- DOCUMENT ME!length- DOCUMENT ME!- Returns:
- DOCUMENT ME!
-
print_bytes
public static String print_bytes(byte[] data)
DOCUMENT ME!- Parameters:
data- DOCUMENT ME!- Returns:
- DOCUMENT ME!
-
getUnsigned
public static int getUnsigned(byte b)
DOCUMENT ME!- Parameters:
b- DOCUMENT ME!- Returns:
- DOCUMENT ME!
-
-