/* File: VariableDatum.java CVS Info: $Id: VariableDatum.java,v 1.2 1998/01/27 18:44:34 mcgredo Exp $ Compiler: jdk 1.3 */ package mil.navy.nps.dis; // package we belong to import mil.navy.nps.util.*; // General-purpose utilities import mil.navy.nps.disEnumerations.*; // Enumerations for DIS import java.io.*; // input/output library import java.util.*; // utilities /** * Variable-length data. * *@version 1.0 *@author Don McGregor (http://www.npsnet.org/~mcgredo) * *
* * The VariableDatum is used in a variety of places, most notably * the DataPdu.
* * This inherits from PduElement, which means that it must know how * to serialize, deSerialize, clone, print its values, and determine * its length when serialized.
* *
sizeOf = 8 bytes
.
*/
public static final int sizeOf = 8; // size of an empty object WHEN WRITTEN TO THE WIRE, in bytes
private static final int BLOCK_LENGTH = 64; // size of blocks allocated, in bits
/**
*Default constructor--fills with zeros for all values.
*/
public VariableDatum() // default constructor
{
variableDatumID = 0;
variableDatumLength = 0;
variableDatumValue = null; // no value by default
return;
}
public Object clone()
{
VariableDatum newVariableDatum = new VariableDatum();
newVariableDatum.setVariableDatumID(variableDatumID);
newVariableDatum.setVariableDatumLength(variableDatumLength);
newVariableDatum.setVariableDatumValue(variableDatumValue);
return newVariableDatum;
}
/**
*@exception RuntimeException when IO error occurs.
*/
public void serialize(DataOutputStream outputStream)
{
int idx = 0;
int blockCount = this.blockCount(variableDatumLength); // number of 64-bit blocks we need
try
{
outputStream.writeInt(variableDatumID);
outputStream.writeInt(variableDatumLength);
if((variableDatumValue != null) && (variableDatumLength > 0))
{
for(idx = 0; idx < blockCount; idx++)
outputStream.writeLong(variableDatumValue[idx]);
}
}
catch(IOException ioException) // catch-all for all IO exceptions in the above code
{
throw new
RuntimeException("Exception in VariableDatum. Error serializing unit.");
}
return;
}
/**
*@exception RuntimeException when IO error occurs.
*/
public void deSerialize(DataInputStream inputStream)
{
int idx = 0;
int blockCount; // number of 64-bit blocks we need
// no need to call super, since there are no ivars in the superclass. The order we read & write
// here is important.
try
{
variableDatumID = inputStream.readInt();
variableDatumLength = inputStream.readInt();
if(variableDatumLength > 0)
{
blockCount = this.blockCount(variableDatumLength); // # of 64 bit blocks to hold the data
variableDatumValue = new long[blockCount]; // get a new array to hold the data
for(idx = 0; idx < blockCount; idx++) // read it all in
{
variableDatumValue[idx] = inputStream.readLong();
}
}
else
variableDatumValue = null; // set data to null if length = 0
}
catch(IOException ioException) // catch-all for all IO exceptions in the above code
{
throw new
RuntimeException("Exception in VariableDatum. Error deSerializing unit.");
}
return;
}
public int length()
{
// have to be careful about potentially dereferencing null here, when
// the size ivar is referenced.
if(variableDatumLength == 0)
return sizeOf;
else
return sizeOf + (8 * variableDatumValue.length);
}
public void printValues(int indentLevel, PrintStream printStream)
{
// print the values of the object out, with correct level of
// indentation on the page.
StringBuffer indent = ProtocolDataUnit.getPaddingOfLength(indentLevel);
int idx, superclassIndent = indentLevel;
printStream.println(indent + "variableDatumID: " + variableDatumID);
printStream.println(indent + "variableDatumLength: " + variableDatumLength);
for(idx = 0; idx < this.blockCount(variableDatumLength); idx++)
printStream.println("variableDatumValue " + idx + ": " + variableDatumValue[idx]);
return;
}
// utility method; just determines the number of 64-bit blocks required. This
// computation is done in three or four places, so it seem better to put it in
// one place, where it can be screwed up centrally.
// INPUT: number of bits
// RETURN: number of 64-bit blocks required
/**
*Returns the number of 64-bit blocks required to store the variableDatumValue.
*
*