Hi!
I have a problem handling ObjectOutputStream/ObjectInputStream.
I created a Thread-Class that writes different objects on an ObjectOutputStream (getting the objects from a Vector). And there is a Thread that reads from the ObjectInputStream. However... if I send an object of let's say type "A", the object will be read by the ObjectInputStream-thread in the right way. But if I send the object of type "A" again (with different properties/values this time) the ObjectInputStream-thread will read the object with the old properties/values. Why??
Here are the two classes:
cheers
frag
patrick.metz@epost.de
I have a problem handling ObjectOutputStream/ObjectInputStream.
I created a Thread-Class that writes different objects on an ObjectOutputStream (getting the objects from a Vector). And there is a Thread that reads from the ObjectInputStream. However... if I send an object of let's say type "A", the object will be read by the ObjectInputStream-thread in the right way. But if I send the object of type "A" again (with different properties/values this time) the ObjectInputStream-thread will read the object with the old properties/values. Why??
Here are the two classes:
Code:
import java.io.*;
public class StreamReader extends Thread
{
Object obj;
VectorHandler vh;
ObjectInputStream in;
ExitHandler exit;
private static final int NORMALEXIT = 0;
private static final int STREAM = 2;
private static final int LOGINCANCEL = 3;
private static final int OBJECTERROR = 4;
public StreamReader(ObjectInputStream in, VectorHandler vh, ExitHandler exit){
this.vh = vh;
this.in = in;
this.exit = exit;
}
public void run()
{
// keep thread alive until exit-signal is caught
while( !exit.getStatus() )
{
try
{
obj = in.readObject();
if( obj instanceof ExitObj ){ // check for exit-object
exit.setStatus(true); // set exit-signal
exit.setReason( ((ExitObj)obj).getReason() );
System.out.println("DEBUG-INFO: exit-object was read by StreamReader!");
}
else
vh.putElement(obj); // put object in the vector
System.out.println("DEBUG-INFO: object was read by StreamReader!");
}
catch(IOException e){
if( !exit.getStatus() && exit.getReason() != LOGINCANCEL )
{
System.out.println("FATAL ERROR: IOException caught -> Could not write object-stream!\n" +
"REASON: Either a network failure occurred or client wasn't closed as it should.\n" +
"STATUS: Client disconnected. Trying to close socket...");
//e.printStackTrace();
exit.setStatus(true);
exit.setReason(STREAM);
}
}
catch(ClassNotFoundException cnfe){
System.out.println("FATAL ERROR: ClassNotFoundException caught, try to close socket...");
exit.setStatus(true);
exit.setReason(OBJECTERROR);
}
}
System.out.println("DEBUG-INFO: exiting thread StreamReader....");
}
}
import java.io.*;
public class StreamWriter extends Thread
{
private static final int STREAM = 2;
VectorHandler vh;
ObjectOutputStream out;
ExitHandler exit;
Object obj;
public StreamWriter(ObjectOutputStream out, VectorHandler vh, ExitHandler exit)
{
this.vh = vh;
this.out = out;
this.exit = exit;
}
public void run()
{
// keep thread alive until exit-signal is caught
while( !exit.getStatus() )
{
try
{
// read first element of vector and put it on the stream
obj = vh.getElement();
//out.writeObject(vh.getElement());
out.writeObject(obj);
out.flush();
System.out.println("DEBUG-INFO: object was written by StreamWriter!");
}
catch(IOException e){
System.out.println("FATAL ERROR: IOException caught -> Could not write object-stream!\n" +
"REASON: Either a network failure occurred or client wasn't closed as it should.\n" +
"STATUS: Client disconnected. Trying to close socket...");
//e.printStackTrace();
exit.setStatus(true);
exit.setReason(STREAM);
}
}
System.out.println("DEBUG-INFO: exiting thread StreamWriter....");
}
}
cheers
frag
patrick.metz@epost.de