ds.ov2.util
Class Convert_serializable

java.lang.Object
  extended by ds.ov2.util.Convert_serializable

public class Convert_serializable
extends Object

Compatibility checking and data conversion by (ab-)using the APDU_Serializable interface. Converts single objects and arrays that are compatible according to is_compatible_with. Contains also the utility methods to convert to and from byte arrays.

This is a static class.

CPP Preprocessing
no cpp preprocessing needed
Execution Environment:
host
Author:
Hendrik Tews
Version:
$Revision: 1.13 $
Last Commit:
$Date: 2009-04-09 10:42:16 $ by $Author: tews $

Constructor Summary
private Convert_serializable()
          Static class, object creation disabled.
 
Method Summary
static int array_from_bytes(byte[] buf, int index, APDU_Serializable[] s)
          Deserializes an array of APDU_Serializable's.
static void array_from(PrintWriter out, String line_start, APDU_Serializable[] host, APDU_Serializable[] card)
          Converts a card type array into a host type array, possibly verbosely.
static byte[] array_to_bytes(APDU_Serializable[] s)
          Serialize an array of APDU_Serializable's into a byte array.
static int array_to_bytes(APDU_Serializable[] s, byte[] buf, int index)
          Serialize an array of APDU_Serializable's into a byte array.
static void array_to(PrintWriter out, String line_start, APDU_Serializable[] host, APDU_Serializable[] card)
          Converts a host type array into a card type array, possibly verbosely.
static void check_compatibility(APDU_Serializable[] host, APDU_Serializable[] card)
          Compatibility check for arrays.
private static void convert(APDU_Serializable src, APDU_Serializable dest)
          Converts src into dest on the assumption that they are compatible and that especially the sizes are equal.
static void from(APDU_Serializable dest, APDU_Serializable src)
          Converts card type src to host type dest.
private static void output(PrintWriter out, String line_start, APDU_Serializable[] a, int i)
          Output serialized data of array element.
static void to(APDU_Serializable src, APDU_Serializable dest)
          Converts host type src to card type dest.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Convert_serializable

private Convert_serializable()
Static class, object creation disabled.

Method Detail

convert

private static void convert(APDU_Serializable src,
                            APDU_Serializable dest)
Converts src into dest on the assumption that they are compatible and that especially the sizes are equal.

Parameters:
src - source
dest - destination

to

public static void to(APDU_Serializable src,
                      APDU_Serializable dest)
               throws IllegalArgumentException
Converts host type src to card type dest. Note that in place of to(x, y) one cannot use from(y, x) in general, because compatibility need not be symmetric, see compatibility checks.

Parameters:
src - source, host type
dest - destination, card type
Throws:
IllegalArgumentException - if dest.is_compatible_with(src) fails

from

public static void from(APDU_Serializable dest,
                        APDU_Serializable src)
                 throws IllegalArgumentException
Converts card type src to host type dest. Note that in place of from(x, y) one cannot use to(y, x) in general, because compatibility need not be symmetric, see compatibility checks.

Parameters:
dest - destination, host type
src - source, card type
Throws:
IllegalArgumentException - if src.is_compatible_with(dest) fails

check_compatibility

public static void check_compatibility(APDU_Serializable[] host,
                                       APDU_Serializable[] card)
                                throws IllegalArgumentException,
                                       NullPointerException
Compatibility check for arrays. Checks that card[i].is_compatible_with(host[i]) for all array elements. Checks also that the arrays have the same size, of course. Null references for the arrays or empty arrays are permissable, as long as the other argument has no elements.

Parameters:
host - host type array
card - card type array
Throws:
IllegalArgumentException - if the compatibility check fails
NullPointerException - if one of the arrays contains a null reference

output

private static void output(PrintWriter out,
                           String line_start,
                           APDU_Serializable[] a,
                           int i)
Output serialized data of array element. Outputs the byte representation of a[i] to out. The format string line_start must contain at most one integer conversion. It is printed with the index before the data.

Parameters:
out - the channel to output to
line_start - format string to be printed with i before the data
a -
i -

array_to

public static void array_to(PrintWriter out,
                            String line_start,
                            APDU_Serializable[] host,
                            APDU_Serializable[] card)
                     throws IllegalArgumentException,
                            NullPointerException
Converts a host type array into a card type array, possibly verbosely. Prints all data to out if out is not null. If out is null then line_start is not used. Otherwise it must be a format string with at most one integer conversion, which is printed with the current index to out before the data.

Note that in place of array_to one cannot use array_from in general, because compatibility need not be symmetric, see compatibility checks.

Parameters:
out - if not null, channel to print all serialized data to
line_start - not used if out is null, otherwise a format string to be printed with the array index before the serialized data
host - source array of host objects
card - destination array of card objects
Throws:
IllegalArgumentException - if the compatibility check fails
NullPointerException - if one of the arrays contains a null reference

array_from

public static void array_from(PrintWriter out,
                              String line_start,
                              APDU_Serializable[] host,
                              APDU_Serializable[] card)
                       throws IllegalArgumentException,
                              NullPointerException
Converts a card type array into a host type array, possibly verbosely. Prints all data to out if out is not null. If out is null then line_start is not used. Otherwise it must be a format string with at most one integer conversion, which is printed with the current index to out before the data.

Note that in place of array_from one cannot use array_to in general, because compatibility need not be symmetric, see compatibility checks.

Parameters:
out - if not null, channel to print all serialized data to
line_start - not used if out is null, otherwise a format string to be printed with the array index before the serialized data
host - destination array of host objects
card - source array of card objects
Throws:
IllegalArgumentException - if the compatibility check fails
NullPointerException - if one of the arrays contains a null reference

array_to_bytes

public static int array_to_bytes(APDU_Serializable[] s,
                                 byte[] buf,
                                 int index)
Serialize an array of APDU_Serializable's into a byte array.

Asserts that the data of s fits into buf at index.

Parameters:
s - the array to serialize, can be null, if non-null all references in the array must be non-null too
buf - the byte array to serialize into
index - starting index in buf
Returns:
the first unused index off buf; this could equal buf.length (if the complete buffer is used) or the original index argument (if s is null)
Throws:
NullPointerException - if one of the references in the array s is null

array_to_bytes

public static byte[] array_to_bytes(APDU_Serializable[] s)
Serialize an array of APDU_Serializable's into a byte array.

Parameters:
s - the array to serialize, can be null, if non-null all references in the array must be non-null too
Returns:
a fresh byte array with the serialized data
Throws:
NullPointerException - if one of the references in the array s is null

array_from_bytes

public static int array_from_bytes(byte[] buf,
                                   int index,
                                   APDU_Serializable[] s)
Deserializes an array of APDU_Serializable's. The data is taken starting from index index.

Parameters:
buf - data to deserialize
index - starting index of data
s - APDU_Serializables to deserialize, can be null, if non-null all references in the array must be non-null too
Returns:
the first unused index in buf; this could equal buf.length (if the complete buffer is used) or the original index argument (if s is null)
Throws:
NullPointerException - if one of the references in s is null