0

I have a kdtree whose nodes consist of the following fields: public static class Node implements Serializable {

    public int discriminator;
    public double value; 

    public Node leftChild;
    public Node rightChild;
    public DataPoint object;
    }

where DataPoint definition:

public static class DataPoint implements Serializable { public Comparable X; public Comparable Y; public Comparable Z;

     public double latitude;
     public double longitude;

     public ArrayList<String> text = new ArrayList<String>(); 
     }

I want to serialize the tree, store in a file and deserialize while answering range queries. My understanding of this concept od serialization is not fine. From whatever I gathered,I have written the following functions, which is not working. Can anybody help?

 public static void serialize(final OutputStream out, kdtrees.Node node) throws IOException
 {
        if( node == null ) 
        {
            //out.write( "()".getBytes() );
            //write to an output leaf file
            out.write("#".getBytes());

        } 
        else 
        {
            //out.write( "(".getBytes() );
            ((ObjectOutputStream) out).writeObject(node);
            serialize( out, node.leftChild );
            serialize( out, node.rightChild );
            //out.write( ")".getBytes( ) );
        }
    }


 public static kdtrees.Node deserialize(final ObjectInputStream reader) throws IOException, ClassNotFoundException 
 {

                  StringTokenizer st = new StringTokenizer(reader.readObject().toString()); 
                  String curToken = st.nextToken().toString();
                  while(st.hasMoreTokens())
                  { 
                      if(curToken.equals("#".trim()))
                          return null;


                      kdtrees.Node e = (kdtrees.Node)reader.readObject();

                       e.leftChild = deserialize(reader);
                       e.rightChild = deserialize(reader);

                    return e;

                  }
               return null;   


    } 
Esha Ghosh
  • 147
  • 1
  • 2
  • 11

1 Answers1

1

Casting an arbitrary OutputStream to an ObjectOutputStream is just going to fail. Instead, use new ObjectOutputStream(outputStream) to create a wrapper around the OutputStream, and the same thing for the InputStreams.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Could you elaborate a little? Sorry I am new to Java. – Esha Ghosh Jan 05 '13 at 18:28
  • Um. Instead of `((ObjectOutputStream) out), write `new ObjectOutputStream(out)`. Same goes for the input streams. Also, there's no need to call `serialize(out, node.leftChild)` or `serialize(out, node.rightChild)`; the children will get automatically serialized with the parent node. – Louis Wasserman Jan 05 '13 at 18:45