package com.itpwebsolutions.util; import java.util.Enumeration; import java.util.StringTokenizer; /** * Useful conversion methods. */ public class Conversion { /** * Stores an integer in a byte array (natural byte ordering) * * @param val the integer to store in byte array * @param array the byte array * @param offset offset in byte array in which to store integer */ public static void toByteArray( int val, byte[] array, int offset ) { // yes we could do this with a simple loop, but we're going for absolute // minimum cycles for something so low-level array[ offset++ ] = ( byte )( val >> 24 ); array[ offset++ ] = ( byte )( val >> 16 ); array[ offset++ ] = ( byte )( val >> 8 ); array[ offset ] = ( byte )( val ); } /** * Stores a long in a byte array (natural byte ordering) * * @param val the long to store in byte array * @param array the byte array * @param offset offset in byte array in which to store long */ public static void toByteArray( long val, byte[] array, int offset ) { // yes we could do this with a simple loop, but we're going for absolute // minimum cycles for something so low-level array[ offset++ ] = ( byte )( val >> 56 ); array[ offset++ ] = ( byte )( val >> 48 ); array[ offset++ ] = ( byte )( val >> 40 ); array[ offset++ ] = ( byte )( val >> 32 ); array[ offset++ ] = ( byte )( val >> 24 ); array[ offset++ ] = ( byte )( val >> 16 ); array[ offset++ ] = ( byte )( val >> 8 ); array[ offset ] = ( byte )( val ); } /** * Returns an integer stored in a byte array. * * @param array the byte array to grab the integer from * @param offset offset in byte array in which to retrieve integer * * @return the resulting integer */ public static int toInt( byte[] array, int offset ) { // yes we could do this with a simple loop, but we're going for absolute // minimum cycles for something so low-level return ( ( ( int )array[ offset++ ] & 255 ) << 24 ) + ( ( ( int )array[ offset++ ] & 255 ) << 16 ) + ( ( ( int )array[ offset++ ] & 255 ) << 8 ) + ( ( ( int )array[ offset ] & 255 ) ); } /** * Returns a long stored in a byte array. * * @param array the byte array to grab the long from * @param offset offset in byte array in which to retrieve long * * @return the resulting integer */ public static int toLong( byte[] array, int offset ) { // yes we could do this with a simple loop, but we're going for absolute // minimum cycles for something so low-level return ( ( ( int )array[ offset++ ] & 255 ) << 56 ) + ( ( ( int )array[ offset++ ] & 255 ) << 48 ) + ( ( ( int )array[ offset++ ] & 255 ) << 40 ) + ( ( ( int )array[ offset++ ] & 255 ) << 32 ) + ( ( ( int )array[ offset++ ] & 255 ) << 24 ) + ( ( ( int )array[ offset++ ] & 255 ) << 16 ) + ( ( ( int )array[ offset++ ] & 255 ) << 8 ) + ( ( ( int )array[ offset ] & 255 ) ); } /** * Enumerates an array * @param array the array to be enumerated * @returns an Enumeration of the array */ public static Enumeration enumerate( Object[] array ) { return new ArrayEnumerator( array ); } /** * Splits a String into substrings, using the given delimiter * @param source the source string * @param delim the delimiter which separates sections * @return an array of split tokens from the source string */ public static String[] split( String source, String delim ) { if( source == null ) return null; StringTokenizer token = new StringTokenizer( source, delim ); String[] tokens = new String[ token.countTokens() ]; for( int i = 0; token.hasMoreTokens(); i++ ) tokens[ i ] = token.nextToken(); return tokens; } /** * Converts a string to an int, returns 0 if it cannot be parsed * @param str the string * @return the int value of the String, 0 if unparsable */ public static int val( String str ) { try { return Integer.parseInt( str ); } catch( NumberFormatException x ) { } return 0; } // private ///////////////////////////////////////////////////////////////// private static class ArrayEnumerator implements Enumeration { Object[] objArray; int position; public ArrayEnumerator( Object[] array ) { objArray = array; } public boolean hasMoreElements() { if( objArray == null ) return false; return position < objArray.length; } public Object nextElement() { return objArray[ position++ ]; } } }