/** * Convert a short into a byte array. */ public static byte[] convertToByteArray( short n ) { n = (short) ( n ^ ( (short) 0x8000 ) ); // flip MSB because "short" is signed byte[] key = new byte[ 2 ]; pack2( key, 0, n ); return key; }
/** * Convert a long into a byte array. */ public static byte[] convertToByteArray( long n ) { n = ( n ^ 0x8000000000000000L ); // flip MSB because "long" is signed byte[] key = new byte[8]; pack8( key, 0, n ); return key; }
/** * Test static methods */ public static void main( String[] args ) { byte[] buf; buf = convertToByteArray( 5 ); System.out.println( "int value of 5 is: " + convertToInt( buf ) ); buf = convertToByteArray( -1 ); System.out.println( "int value of -1 is: " + convertToInt( buf ) ); buf = convertToByteArray( 22111000 ); System.out.println( "int value of 22111000 is: " + convertToInt( buf ) ); buf = convertToByteArray( 5L ); System.out.println( "long value of 5 is: " + convertToLong( buf ) ); buf = convertToByteArray( -1L ); System.out.println( "long value of -1 is: " + convertToLong( buf ) ); buf = convertToByteArray( 1112223334445556667L ); System.out.println( "long value of 1112223334445556667 is: " + convertToLong( buf ) ); }
/** * Serialize the content of an object into a byte array. * * @param obj Object to serialize * @return a byte array representing the object's state */ public byte[] serialize( Object obj ) throws IOException { Long number = (Long) obj; return Conversion.convertToByteArray( number.longValue() ); }
/** * Convert a byte array into an integer (signed 32-bit) value. */ public static int convertToInt( byte[] buf ) { int value = unpack4( buf, 0 ); value = ( value ^ 0x80000000 ); // flip MSB because "int" is signed return value; }
/** * Deserialize the content of an object from a byte array. * * @param serialized Byte array representation of the object * @return deserialized object */ public Object deserialize( byte[] serialized ) throws IOException { int number = Conversion.convertToInt( serialized ); return Integer.valueOf( number ); }
/** * Deserialize the content of an object from a byte array. * * @param serialized Byte array representation of the object * @return deserialized object */ public Object deserialize( byte[] serialized ) throws IOException { long number = Conversion.convertToLong( serialized ); return new Long( number ); }
/** * Test static methods */ public static void main( String[] args ) { byte[] buf; buf = convertToByteArray( (int) 5 ); System.out.println( "int value of 5 is: " + convertToInt( buf ) ); buf = convertToByteArray( (int) -1 ); System.out.println( "int value of -1 is: " + convertToInt( buf ) ); buf = convertToByteArray( (int) 22111000 ); System.out.println( "int value of 22111000 is: " + convertToInt( buf ) ); buf = convertToByteArray( (long) 5L ); System.out.println( "long value of 5 is: " + convertToLong( buf ) ); buf = convertToByteArray( (long) -1L ); System.out.println( "long value of -1 is: " + convertToLong( buf ) ); buf = convertToByteArray( (long) 1112223334445556667L ); System.out.println( "long value of 1112223334445556667 is: " + convertToLong( buf ) ); }
/** * Serialize the content of an object into a byte array. * * @param obj Object to serialize * @return a byte array representing the object's state */ public byte[] serialize( Object obj ) throws IOException { Integer number = (Integer) obj; return Conversion.convertToByteArray( number.intValue() ); }
/** * Convert a byte array into an integer (signed 32-bit) value. */ public static int convertToInt( byte[] buf ) { int value = unpack4( buf, 0 ); value = ( value ^ 0x80000000 ); // flip MSB because "int" is signed return value; }
/** * Deserialize the content of an object from a byte array. * * @param serialized Byte array representation of the object * @return deserialized object */ public Object deserialize( byte[] serialized ) throws IOException { int number = Conversion.convertToInt( serialized ); return new Integer( number ); }
/** * Deserialize the content of an object from a byte array. * * @param serialized Byte array representation of the object * @return deserialized object */ public Object deserialize( byte[] serialized ) throws IOException { long number = Conversion.convertToLong( serialized ); return Long.valueOf( number ); }
/** * Test static methods */ public static void main( String[] args ) { byte[] buf; buf = convertToByteArray( 5 ); System.out.println( "int value of 5 is: " + convertToInt( buf ) ); buf = convertToByteArray( -1 ); System.out.println( "int value of -1 is: " + convertToInt( buf ) ); buf = convertToByteArray( 22111000 ); System.out.println( "int value of 22111000 is: " + convertToInt( buf ) ); buf = convertToByteArray( 5L ); System.out.println( "long value of 5 is: " + convertToLong( buf ) ); buf = convertToByteArray( -1L ); System.out.println( "long value of -1 is: " + convertToLong( buf ) ); buf = convertToByteArray( 1112223334445556667L ); System.out.println( "long value of 1112223334445556667 is: " + convertToLong( buf ) ); }
/** * Convert an int into a byte array. */ public static byte[] convertToByteArray( int n ) { n = (n ^ 0x80000000); // flip MSB because "int" is signed byte[] key = new byte[4]; pack4(key, 0, n); return key; }
/** * Serialize the content of an object into a byte array. * * @param obj Object to serialize * @return a byte array representing the object's state */ public byte[] serialize( Object obj ) throws IOException { Long number = (Long) obj; return Conversion.convertToByteArray( number.longValue() ); }
/** * Convert a byte array into an integer (signed 32-bit) value. */ public static int convertToInt( byte[] buf ) { int value = unpack4( buf, 0 ); value = ( value ^ 0x80000000 ); // flip MSB because "int" is signed return value; }
/** * Deserialize the content of an object from a byte array. * * @param serialized Byte array representation of the object * @return deserialized object */ public Object deserialize( byte[] serialized ) throws IOException { int number = Conversion.convertToInt( serialized ); return Integer.valueOf( number ); }
/** * Deserialize the content of an object from a byte array. * * @param serialized Byte array representation of the object * @return deserialized object */ public Object deserialize( byte[] serialized ) throws IOException { long number = Conversion.convertToLong( serialized ); return Long.valueOf( number ); }
/** * Convert a long into a byte array. */ public static byte[] convertToByteArray( long n ) { n = (n ^ 0x8000000000000000L); // flip MSB because "long" is signed byte[] key = new byte[8]; pack8( key, 0, n ); return key; }