BinaryWriter/BinaryReader

He everyone

  I am writing an application that uses point data from meteorological
observing stations. The data is extracted from a database using the
AddePointDataReader class before being converted into a visad FieldImpl of
the form:

(Time -> (IDN -> (Latitude, Longitude, Date, Time, Temperature, etc))

where IDN is the id number of the station from where the observation is
made. Essentially a field of times, where the range for each time, is a
field of station based observations.

  I would like to be able to write this field to a local file to allow it to
be archived seperately from ADDE database, along with any changes that a
user may have made to the field. I have chosen to do this using BinaryWriter
and reading the field back using BinaryReader, as they seem to offer a
simple, efficient means to do this.

  My code looks something like this:

        /**
         * Write field to file
        */
        void write(FieldImpl field, String filename)
        {
                File file = new File(filename);
                FileOutputStream oStream = new FileOutputStream(file);
                BinaryWriter writer = new BinaryWriter(oStream);
                writer.save(field);
                writer.close();
        }

        /**
         * Read field from file
         */
        FieldImpl read(String filename)
        {
                File file = new File(filename);
                FileInputStream iStream = new FileInputStream(file);
                BinaryReader reader = new BinaryReader(iStream);
                field = (FieldImpl)reader.getData();
                reader.close();

                return field;
        }


        Initially this gets me exactly what I want, the field is written to 
file,
and when read back in is exactly as it was before. The problem is I can only
read it once. If I attempt to read the file again (or any other file
containing this field structure) I get the following stack trace:

Error: java.lang.IndexOutOfBoundsException: Index: 9, Size: 9
java.lang.IndexOutOfBoundsException: Index: 9, Size: 9
        at java.util.ArrayList.RangeCheck(ArrayList.java:508)
        at java.util.ArrayList.get(ArrayList.java:320)
        at
visad.data.visad.BinaryObjectCache.get(BinaryObjectCache.java:118)
        at visad.data.visad.object.BinarySetType.read(BinarySetType.java:53)
        at
visad.data.visad.object.BinaryMathType.read(BinaryMathType.java:70)
        at visad.data.visad.BinaryReader.getData(BinaryReader.java:235)
        at visad.data.visad.object.BinaryGeneric.read(BinaryGeneric.java:52)
        at
visad.data.visad.object.BinaryRealType.read(BinaryRealType.java:108)
        at
visad.data.visad.object.BinaryMathType.read(BinaryMathType.java:67)
        at visad.data.visad.BinaryReader.getData(BinaryReader.java:235)
        at TestBinaryIO.read(TestBinaryIO.java:117)
        at TestBinaryIO.main(TestBinaryIO.java:152)

        I can't reproduce this error using a more simple gridded FlatField, and 
the
error also occurs if I have previously used a field of this form when
extracted directly from AddePointDataReader, without having made any
previous reference to BinaryWriter or BinaryReader. It seems to be specific
to the data field structure that I'm trying to store.

        Could anyone explain to me what might be happening? Or alternatively
suggest another, equally simple way of storing a field of this structure to
a local file.

thanks,

Rob