ilex.util.ByteUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opendcs Show documentation
Show all versions of opendcs Show documentation
A collection of software for aggregatting and processing environmental data such as from NOAA GOES satellites.
The newest version!
/*
* $Id$
*
* $Source$
*
* $State$
*
* $Log$
* Revision 1.1.1.1 2014/05/19 15:28:59 mmaloney
* OPENDCS 6.0 Initial Checkin
*
* Revision 1.4 2011/09/10 10:23:34 mmaloney
* added getFloat method
*
* Revision 1.3 2009/10/30 15:31:25 mjmaloney
* Added toHexAsciiString -- useful for debugging output.
*
* Revision 1.2 2008/09/05 12:53:53 mjmaloney
* LRGS 7 dev
*
* Revision 1.1 2008/04/04 18:21:09 cvs
* Added legacy code to repository
*
* Revision 1.11 2004/08/30 14:50:24 mjmaloney
* Javadocs
*
* Revision 1.10 2003/12/20 00:32:50 mjmaloney
* Implemented TimeoutInputStream.
*
* Revision 1.9 2003/08/19 15:51:37 mjmaloney
* dev
*
* Revision 1.8 2003/05/16 20:12:38 mjmaloney
* Added EnvExpander. This is preferrable to ShellExpander because
* it is platform independent.
*
* Revision 1.7 2003/04/09 15:16:11 mjmaloney
* dev.
*
* Revision 1.6 2003/03/27 21:17:55 mjmaloney
* drgs dev
*
* Revision 1.5 2001/04/12 12:26:18 mike
* test checkin from boss
*
* Revision 1.4 2000/03/12 22:41:34 mike
* Added PasswordFile & PasswordFileEntry classes.
*
* Revision 1.3 1999/11/16 14:46:32 mike
* Added getCString function - retrieve a null-terminated C string from a byte
* array.
*
* Revision 1.2 1999/10/26 12:44:27 mike
* Fixed sign extension problems for getting integers from bytes.
*
* Revision 1.1 1999/10/20 21:10:04 mike
* Initial implementation
*/
package ilex.util;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
/**
* This class contains several static methods for manipulating arrays of
* bytes.
*/
public class ByteUtil
{
/**
* Convert 4 bytes from the passed array to an integer. Start at the
* specified offset. Assume big-endian encoding.
* @param b the byte array
* @param offset the offset
* @return integer constructed from bytes
*/
public static final int getInt4_BigEndian( byte[] b, int offset )
{
return
((int)b[offset+3] & 0xff)
| (((int)b[offset+2] & 0xff) << 8)
| (((int)b[offset+1] & 0xff) << 16)
| (((int)b[offset+0] & 0xff) << 24);
}
public static final long getInt8_BigEndian(byte[] b, int offset)
{
return
((long)b[offset+7] & 0xff)
| (((long)b[offset+6] & 0xff) << 8)
| (((long)b[offset+5] & 0xff) << 16)
| (((long)b[offset+4] & 0xff) << 24)
| (((long)b[offset+3] & 0xff) << 32)
| (((long)b[offset+2] & 0xff) << 40)
| (((long)b[offset+1] & 0xff) << 48)
| (((long)b[offset+0] & 0xff) << 56);
}
/**
* Convert 4 bytes from the passed array to an integer. Start at the
* specified offset. Assume little-endian encoding.
* @param b the byte array
* @param offset the offset
* @return integer constructed from bytes
*/
public static final int getInt4_LittleEndian( byte[] b, int offset )
{
return
((int)b[offset+0] & 0xff)
| (((int)b[offset+1] & 0xff) << 8)
| (((int)b[offset+2] & 0xff) << 16)
| (((int)b[offset+3] & 0xff) << 24);
}
/**
* Convert 2 bytes from the passed array to an integer. Start at the
* specified offset. Assume little-endian encoding.
* @param b the byte array
* @param offset the offset
* @return integer constructed from bytes
*/
public static final int getInt2_LittleEndian( byte[] b, int offset )
{
return
((int)b[offset+0] & 0xff)
| (((int)b[offset+1] & 0xff) << 8);
}
/**
* Convert 2 bytes from the passed array to an integer. Start at the
* specified offset. Assume big-endian encoding.
* @param b the byte array
* @param offset the offset
* @return integer constructed from bytes
*/
public static final int getInt2_BigEndian( byte[] b, int offset )
{
return
((int)b[offset+1] & 0xff)
| (((int)b[offset+0] & 0xff) << 8);
}
/**
* Encode a 4-byte integer and place it in a byte array at the
* specified offset. Assume big-endian encoding.
* @param i the integer
* @param b the byte array
* @param offset at which to start
*/
public static final void putInt4_BigEndian( int i, byte[] b, int offset )
{
b[offset ] = (byte)((i>>24) & 0xff);
b[offset+1] = (byte)((i>>16) & 0xff);
b[offset+2] = (byte)((i>>8 ) & 0xff);
b[offset+3] = (byte)(i & 0xff);
}
/**
* Encode a 4-byte integer and place it in a byte array at the
* specified offset. Assume little-endian encoding.
* @param i the integer
* @param b the byte array
* @param offset at which to start
*/
public static final void putInt4_LittleEndian( int i, byte[] b, int offset )
{
b[offset+3] = (byte)((i>>24) & 0xff);
b[offset+2] = (byte)((i>>16) & 0xff);
b[offset+1] = (byte)((i>>8 ) & 0xff);
b[offset ] = (byte)(i & 0xff);
}
/**
* Encode a 2-byte integer and place it in a byte array at the
* specified offset. Assume little-endian encoding.
* @param i the integer
* @param b the byte array
* @param offset at which to start
*/
public static final void putInt2_LittleEndian( int i, byte[] b, int offset )
{
b[offset ] = (byte)(i & 0xff);
b[offset+1] = (byte)((i>>8 ) & 0xff);
}
/**
* Pull a null terminated C-style string out of a byte array.
* @param b byte array
* @param offset offset at which to start
* @return String
*/
public static final String getCString( byte[] b, int offset )
{
StringBuffer ret = new StringBuffer();
for(int i = offset; i < b.length && b[i] != (byte)0; i++)
ret.append((char)b[i]);
return new String(ret);
}
/**
* Encode a string into the buffer as a C-style null-terminated array of
* bytes.
* @param s the string to encode
* @param b the byte buffer to encode it into
* @param offset the starting point within the buffer
* @param padLength The length of the area within the buffer. The string
* will be padded to the end of the field with null bytes.
*/
public static final void putCString(String s, byte[] b, int offset, int padLength)
{
if (s == null)
s = "";
int strlen = s.length();
if (strlen >= padLength)
strlen = padLength-1; // Leave at least 1 char for a null byte
for (int i=0; i (int)' ' && x <= (int)'~')
ret.append(" " + (char)x);
else
{
ret.append(' ');
ret.append(toHexChar(x / 16));
ret.append(toHexChar(x % 16));
}
}
return ret.toString();
}
/**
* Converts hex char representation of an int in the range 0...15.
* @param x the int.
* @return single hex char.
*/
public static final char toHexChar( int x )
{
x %= 16;
return x < 10 ? (char)((int)'0' + x) : (char)((int)'A' + (x-10));
}
/**
* Converts hex char to in in the range 0...15
* @param c the hex char
* @return the int
*/
public static final int fromHexChar( char c )
{
if (Character.isDigit(c))
return (int)c - (int)'0';
else switch(c)
{
case 'a': case 'A': return 10;
case 'b': case 'B': return 11;
case 'c': case 'C': return 12;
case 'd': case 'D': return 13;
case 'e': case 'E': return 14;
case 'f': case 'F': return 15;
default: return -1;
}
}
/**
* @param c the char
* @return true if the character is a hex char.
*/
public static boolean isHexChar( byte c )
{
int i = fromHexChar((char)c);
return i != -1;
}
/**
* Converts a hex string into a byte array.
* If the string has an odd length, it is parsed as if a trailing zero
* were added.
* @param hex the hex string
* @return the byte array
*/
public static final byte[] fromHexString( String hex )
{
int len = hex.length();
if (len == 0)
return null;
if (len % 2 == 1) // Odd length, add a zero to the beginning.
{
hex = '0' + hex;
len++;
}
byte ret[] = new byte[len/2]; // Each byte is 2 hex chars
int j=0;
for(int i = 0; i= zeroDigit && c <= nineDigit)
value = (value * 10) + (c - zeroDigit);
else
throw new NumberFormatException("Illegal character '"
+ (char)c + "' in integer field.");
}
return value * sign;
}
/**
* Convert 4 bytes from the passed array to an integer. Start at the
* specified offset. Assume big-endian encoding.
* @param b the byte array
* @param offset the offset
* @return integer constructed from bytes
*/
public static final float getFloat4( byte[] b, int offset )
{
DataInputStream dis = new DataInputStream(
new ByteArrayInputStream(b, offset, 4));
try { return dis.readFloat(); }
catch(Exception ex) { return (float)0.0; }
}
/**
* Return true if two byte arrays are the same, false otherwise.
* Tolerates either being null. Returns true if both are null.
* @param a1
* @param a2
* @return
*/
public static boolean equals(byte[] a1, byte[] a2)
{
if (a1 == null)
return a2 == null;
else if (a2 == null)
return false;
if (a1.length != a2.length)
return false;
for(int i=0; i