FileDocCategorySizeDatePackage
CompactIntList.javaAPI DocExample3658Sat Jan 24 10:44:32 GMT 2004je3.serialization

CompactIntList

public class CompactIntList extends je3.classes.IntList implements Externalizable
This subclass of IntList assumes that most of the integers it contains are less than 32,000. It implements Externalizable so that it can define a compact serialization format that takes advantage of this fact.

Fields Summary
static final byte
version
This version number is here in case a later revision of this class wants to modify the externalization format, but still retain compatibility with externalized objects written by this version
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)
A main() method to prove that it works

	CompactIntList list = new CompactIntList();
	for(int i = 0; i < 100; i++) list.add((int)(Math.random()*40000));
	CompactIntList copy = (CompactIntList)Serializer.deepclone(list);
	if (list.equals(copy)) System.out.println("equal copies");
	Serializer.store(list, new File("compactintlist.ser"));
    
public voidreadExternal(java.io.ObjectInput in)
This Externalizable method is responsible for completely restoring the state of the object. A no-arg constructor will be called to re-create the object, and this method must read the state written by writeExternal() to restore the object's state.

	// Start by reading and verifying the version number.
	byte v = in.readByte();
	if (v != version)
	    throw new IOException("CompactIntList: unknown version number");

	// Read the number of array elements, and make array that big
	int newsize = in.readInt();
	setCapacity(newsize);  // A protected method inherited from IntList
	this.size = newsize;   // Save this size.

	// Now read that many values from the stream
	for(int i = 0; i < newsize; i++) {
	    short n = in.readShort();
	    if (n != Short.MIN_VALUE) data[i] = n;
	    else data[i] = in.readInt();
	}
    
public voidwriteExternal(java.io.ObjectOutput out)
This method from the Externalizable interface is responsible for saving the complete state of the object to the specified stream. It can write anything it wants as long as readExternal() can read it.


                                           
          
        trim();  // Compact the list to its current size

	out.writeByte(version);  // Start with our version number.
	out.writeInt(size);      // Output the number of array elements
	for(int i = 0; i < size; i++) {  // Now loop through the array
	    int n = data[i];             // The array element to write
	    if ((n <= Short.MAX_VALUE) && (n > Short.MIN_VALUE)) {
		// If n fits in a short and is not Short.MIN_VALUE, then write
		// it out as a short, saving ourselves two bytes
		out.writeShort(n);
	    }
	    else {
		// Otherwise write out the special value Short.MIN_VALUE to
		// signal that the number does not fit in a short, and then
		// output the number using a full 4 bytes, for 6 bytes total
		out.writeShort(Short.MIN_VALUE);
		out.writeInt(n);
	    }
	}