C
Chris
Hi,
I'm trying to extend a client/server program so that I can send/receive
files. It uses serializable Message objects in order to send messages.
The Message class has a private byte array field which holds the file
being sent. Before sending and receiving a message, the Message object
either gets converted to a byte array or a byte array is converted back
to a Message object. The code for that class:-
import java.io.*;
public class ConvertData{
//Convert a message object into a byte array
public static byte[] messageToBytes (Object object) throws
IOException{
java.io.ByteArrayOutputStream bs = new
java.io.ByteArrayOutputStream();
java.i
bjectOutputStream out = new java.i
bjectOutputStream (bs);
out.writeObject(object);
out.flush();
out.close ();
byte [] bytes = bs.toByteArray();
System.out.println("Bytes sending = " + bytes.length);
return bytes;
}
//Convert a byte array into a message object
public static Object bytesToMessage (byte bytes[]) throws IOException,
ClassNotFoundException{
try{
System.out.println("Bytes received = " + bytes.length);
Object object;
java.i
bjectInputStream in;
java.io.ByteArrayInputStream bs;
bs = new java.io.ByteArrayInputStream (bytes);
in = new java.i
bjectInputStream(bs);
object = in.readObject();
in.close ();
bs.close ();
return object;
}
catch(StreamCorruptedException sce){
System.out.println("Stream corrupted Exception ");
sce.printStackTrace();
Object o = new Object();
return o;
}
catch(java.lang.ClassCastException cce){
System.out.println("Class Cast Exception ");
cce.printStackTrace();
Object o = new Object();
return o;
}
}
}
I run my own protocol whereby I create the Message object with the file
and obtain the file size. I then send a warning to the server
(ClientThread helper class) to accept a larger file (default is 2048
bytes). The server gets ready to accept the file and sends back an
acknowledgement. The client then sends the file after receiving the
acknowledgement. I've tried various size byte arrays in the Message
object and anything over 1225 bytes I get the following exception
thrown on the server side:-
java.io.StreamCorruptedException
at
java.i
bjectInputStream.readObject0(ObjectInputStream.java:1326)
at
java.i
bjectInputStream.defaultReadFields(ObjectInputStream.java:1912)
at
java.i
bjectInputStream.readSerialData(ObjectInputStream.java:1836)
at
java.i
bjectInputStream.readOrdinaryObject(ObjectInputStream.java:1713)
at
java.i
bjectInputStream.readObject0(ObjectInputStream.java:1299)
at
java.i
bjectInputStream.readObject(ObjectInputStream.java:339)
at ConvertData.bytesToMessage(ConvertData.java:39)
at ClientThread.run(Server.java:459)
at java.lang.Thread.run(Thread.java:595)
java.lang.ClassCastException: java.lang.Object
Does anyone have an idea as to what may be the cause?
Kind regards,
Chris
I'm trying to extend a client/server program so that I can send/receive
files. It uses serializable Message objects in order to send messages.
The Message class has a private byte array field which holds the file
being sent. Before sending and receiving a message, the Message object
either gets converted to a byte array or a byte array is converted back
to a Message object. The code for that class:-
import java.io.*;
public class ConvertData{
//Convert a message object into a byte array
public static byte[] messageToBytes (Object object) throws
IOException{
java.io.ByteArrayOutputStream bs = new
java.io.ByteArrayOutputStream();
java.i
out.writeObject(object);
out.flush();
out.close ();
byte [] bytes = bs.toByteArray();
System.out.println("Bytes sending = " + bytes.length);
return bytes;
}
//Convert a byte array into a message object
public static Object bytesToMessage (byte bytes[]) throws IOException,
ClassNotFoundException{
try{
System.out.println("Bytes received = " + bytes.length);
Object object;
java.i
java.io.ByteArrayInputStream bs;
bs = new java.io.ByteArrayInputStream (bytes);
in = new java.i
object = in.readObject();
in.close ();
bs.close ();
return object;
}
catch(StreamCorruptedException sce){
System.out.println("Stream corrupted Exception ");
sce.printStackTrace();
Object o = new Object();
return o;
}
catch(java.lang.ClassCastException cce){
System.out.println("Class Cast Exception ");
cce.printStackTrace();
Object o = new Object();
return o;
}
}
}
I run my own protocol whereby I create the Message object with the file
and obtain the file size. I then send a warning to the server
(ClientThread helper class) to accept a larger file (default is 2048
bytes). The server gets ready to accept the file and sends back an
acknowledgement. The client then sends the file after receiving the
acknowledgement. I've tried various size byte arrays in the Message
object and anything over 1225 bytes I get the following exception
thrown on the server side:-
java.io.StreamCorruptedException
at
java.i
at
java.i
at
java.i
at
java.i
at
java.i
at
java.i
at ConvertData.bytesToMessage(ConvertData.java:39)
at ClientThread.run(Server.java:459)
at java.lang.Thread.run(Thread.java:595)
java.lang.ClassCastException: java.lang.Object
Does anyone have an idea as to what may be the cause?
Kind regards,
Chris