Hello,I’m developing a server which is using Netty.The goal is to be able to send objects back and forth.So I configured the server with using ObjectDecoderInputStream and ObjectEncoderOutputStream.
so I have a client also using Netty to communicate with the server. The client work well.
Whenever I use Java’s socket to receive an Serializable Object from the server. Something strange happened, I got an exception below:
java.io.StreamCorruptedException: Unsupported version: 0
at org.jboss.netty.handler.codec.serialization.CompactObjectInputStream.readStreamHeader(CompactObjectInputStream.java:38)
at java.io.ObjectInputStream.<init>(Unknown Source)
at org.jboss.netty.handler.codec.serialization.CompactObjectInputStream.<init>(CompactObjectInputStream.java:30)
at org.jboss.netty.handler.codec.serialization.ObjectDecoderInputStream.readObject(ObjectDecoderInputStream.java:115)
at com.bvd.main.Client.readResponse(Client.java:139)
at com.bvd.main.Client.getFile(Client.java:80)
at com.bvd.main.Client.main(Client.java:163)
Sending an object from the client:
public static void writeRequest(Message requestMsg,
OutputStream outputStream) {
byte[] bytes = null;
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
try {
ObjectEncoderOutputStream objectOutputStream = new ObjectEncoderOutputStream(
byteOutputStream, 8192);
objectOutputStream.writeObject(requestMsg);
objectOutputStream.flush();
bytes = byteOutputStream.toByteArray();
byteOutputStream.close();
objectOutputStream.close();
outputStream.write(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and the code that the client received an object:
public static Object readResponse(InputStream inputStream) {
int count = 0;
Object object = null;
try {
byte[] lenBuffer = new byte[4];
while ((count = inputStream.read(lenBuffer)) < 4) {
}
System.out.println(ByteBuffer.wrap(lenBuffer).getInt());
int infolen = ByteBuffer.wrap(lenBuffer).getInt();
byte[] objBuffer = new byte[infolen];
while ((count = inputStream.read(objBuffer)) < infolen) {
}
byte[] totalBuf = new byte[4 + infolen];
System.arraycopy(lenBuffer, 0, totalBuf, 0, lenBuffer.length);
System.arraycopy(objBuffer, 0, totalBuf, lenBuffer.length,
objBuffer.length);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
totalBuf);
System.out.println(byteArrayInputStream.available());
ObjectDecoderInputStream objectDecoderInputStream = new ObjectDecoderInputStream(
new BufferedInputStream(byteArrayInputStream));
object = objectDecoderInputStream.readObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object;
}
What confuse me more is when I set the length of an object’s content being sent(the object carries part of a file that have been converted to byte[])on the server of 2858(byte), the client above works well; and when I change the length 2859 or bigger, the StreamCorruptedException occurs above.
ps: sending objects work well, the server received successfully and decode it correctly, all the questions are about objects receiving from the server.
Sorry for my poor English, any help is appreciated. Thanks.
in both of your read loops your are going to get bogus data if you end up making more than one read call:
every time you call read, it will write to the beginning of the
byte[]. so each time you loop around, you are overwriting part of the data in the buffer, not reading an entire buffer’s worth of data. you need to keep track of the current offset (based on the amount of data you have read so far) and pass that into threadread()call. also, you should be accumulating to count, not assigning to it.