Click to See Complete Forum and Search --> : java.io.StreamCorruptedException when reading from a socket


George2
November 7th, 2006, 08:53 AM
Hello everyone,


When reading from a socket, I got error,

java.io.StreamCorruptedException unexpected end of block data

I only get it very randomly and very hard to reproduce. Does anyone encounter this before, any ideas?

Should it be caused by server side (output stream is not flushed)?

BTW: I program both client and server.


thanks in advance,
George

tbear
November 7th, 2006, 11:49 PM
Can you try doing a stacktrace print as well as printing the exceptions string value , need to see if its throwing a reason for the exception.

dlorde
November 8th, 2006, 05:50 AM
If you Google for it ('StreamCorruptedException unexpected end of block data'), you'll find quite a few instances around the place. There may be some useful information there, I don't know...

90% of code written today is getting around other people's mistakes...
A . Kay

George2
November 9th, 2006, 03:24 AM
Hi tbear,


Can you try doing a stacktrace print as well as printing the exceptions string value , need to see if its throwing a reason for the exception.

What reason do you mean, do you mean this "unexpected end of block data"?


regards,
George

George2
November 9th, 2006, 03:25 AM
Hi dude,


If you Google for it ('StreamCorruptedException unexpected end of block data'), you'll find quite a few instances around the place. There may be some useful information there, I don't know...

90% of code written today is getting around other people's mistakes...
A . Kay

I have found several solutions. But not sure which one should I follow. Is it a programming error or a JVM/network issue?


regards,
George

tbear
November 9th, 2006, 03:38 AM
Hi tbear,




What reason do you mean, do you mean this "unexpected end of block data"?


regards,
George
Thats the shorter version , can you print stacktrace and the relevant code snippet, need more info. than just the above exception string, what exactly are you trying to read (Objects ???). If you really want people to help you out you need to give them more information.

George2
November 9th, 2006, 03:57 AM
Hi tbear,


Thats the shorter version , can you print stacktrace and the relevant code snippet, need more info. than just the above exception string, what exactly are you trying to read (Objects ???). If you really want people to help you out you need to give them more information.

Do you mean that even if I can not reproduce the issue consistently, I need to post the related code snippet which throws the exception? Thank you.


regards,
George

dlorde
November 9th, 2006, 05:20 AM
Is it a programming error or a JVM/network issue?How would I know? No doubt some detective work is required...

It's easy to cry "bug" when the truth is that you've got a complex system and sometimes it takes a while to get all the components to co-exist peacefully...
D. Vargas

tbear
November 9th, 2006, 05:39 AM
Hi tbear,


Do you mean that even if I can not reproduce the issue consistently, I need to post the related code snippet which throws the exception? Thank you.

regards,
George
I was trying to help you out and hence the request it has nothing to do with the frequency of occurance. Think I just went overboard in trying to help. Will stick to answer your initial queries.

I only get it very randomly and very hard to reproduce. Does anyone encounter this before,

Yes.

any ideas?

No

Should it be caused by server side (output stream is not flushed)?

Dont know.

George2
November 9th, 2006, 06:10 AM
Hi dude,


I was trying to help you out and hence the request it has nothing to do with the frequency of occurance. Think I just went overboard in trying to help. Will stick to answer your initial queries.

Yes.

No

Dont know.

Seems that you have ever met with this issue before. How do you solve it in the past? By flush?


regards,
George

gmagoss
November 10th, 2006, 10:14 AM
Hi,

I have not encountered this error before. But I had similiar problems reading data sent using a secure socket.

The reason was an error in the implementation of the jsse layer. It was assumed that the packages were comming in the same order as they were sent by the server.

If the network is a little larger, say the internet, this is not necessarliy the case.

Maybe your problem has a similar cause. check which runtime and socket classes you are using and if there is an update available.

Regards
György

gmagoss
November 10th, 2006, 10:36 AM
Update. I just did some testing.

An StreamCorruptedException is encountered (for example) if you try to call read(byte[]) from an ObjectInputStream. And this ObjectInputStream does not hold a byte[] .

Can you post the code causing the exception ?

Regards
György

gmagoss
November 10th, 2006, 11:48 AM
If you have written a servlet that normaly opens an ObjectOutputStream for the response and writes an Object into it, this ObjectOutputStream has a header tha identifies it as an ObjectOutputStream.
The recipient of the response can open it as an ObjectInputStream and read from it.

If for some reason the server encounters a problem and an ServletException is thrown and not caught within the servlet, the server sends back the standard Error 500 page.
This is not a java object and thus the Stream does not have a correct Header for an ObjectInputStream.

Reading from this stream will result in an StreamCorruptedException.

The same applies when you have a proxy that catches time out and comes up with a corresponding message to the browser (some like blue coat do that). In this case a timeout on the connection will result in an error page sent to your client. This stream also has not the correct header information and thus a
StreamCorruptedException will be thrown.

George2
November 11th, 2006, 02:22 AM
Hi György,


Hi,

I have not encountered this error before. But I had similiar problems reading data sent using a secure socket.

The reason was an error in the implementation of the jsse layer. It was assumed that the packages were comming in the same order as they were sent by the server.

If the network is a little larger, say the internet, this is not necessarliy the case.

Maybe your problem has a similar cause. check which runtime and socket classes you are using and if there is an update available.

Regards
György

I am not using SSL on socket. I am using normal socket to transfer object stream.

Any ideas of what is the cause of this error?


regards,
George

George2
November 11th, 2006, 02:24 AM
Update. I just did some testing.

An StreamCorruptedException is encountered (for example) if you try to call read(byte[]) from an ObjectInputStream. And this ObjectInputStream does not hold a byte[] .

Can you post the code causing the exception ?

Regards
György

What means "ObjectInputStream does not hold a byte[]"? Means the content of the object input stream instance is null? (other than the object instance of the object input stream itself.)


regards,
George

George2
November 11th, 2006, 02:26 AM
Thank you gmagoss!


If you have written a servlet that normaly opens an ObjectOutputStream for the response and writes an Object into it, this ObjectOutputStream has a header tha identifies it as an ObjectOutputStream.
The recipient of the response can open it as an ObjectInputStream and read from it.

If for some reason the server encounters a problem and an ServletException is thrown and not caught within the servlet, the server sends back the standard Error 500 page.
This is not a java object and thus the Stream does not have a correct Header for an ObjectInputStream.

Reading from this stream will result in an StreamCorruptedException.

The same applies when you have a proxy that catches time out and comes up with a corresponding message to the browser (some like blue coat do that). In this case a timeout on the connection will result in an error page sent to your client. This stream also has not the correct header information and thus a
StreamCorruptedException will be thrown.

You are an expert! I am wondering which header do you mean is not correct? HTTP content-length header or some Java object header?


regards,
George

gmagoss
November 11th, 2006, 02:44 PM
Java object Header.
If you have a problem don't only look at the exception that is thrown but also at the method that is throwing it and look into the API documentation of that method.

From your other posting I guessed that your StreamCorruptedException is related to ObjectInputStream,so I had a look at this class. And in the API it is farely well described when and why a StreamCorruptedException is thrown.

If I am mistaken and your StreamCorruptedException is not related to the ObjectInputStream just post the part of the source code and the stack trace.

George2
November 12th, 2006, 01:27 AM
Thank you for your analysis gmagoss!


Java object Header.
If you have a problem don't only look at the exception that is thrown but also at the method that is throwing it and look into the API documentation of that method.

From your other posting I guessed that your StreamCorruptedException is related to ObjectInputStream,so I had a look at this class. And in the API it is farely well described when and why a StreamCorruptedException is thrown.

If I am mistaken and your StreamCorruptedException is not related to the ObjectInputStream just post the part of the source code and the stack trace.

I agree with you that it has something to do with object input stream. Do you have any ideas of what is the cause and how to solve it?


regards,
George

gmagoss
November 12th, 2006, 04:28 PM
There are three possible sources of problems in your set up:

the sender : Can you be positively sure that the sender always generates and sends an ObjectOutputStream ? Can it be that for some reason something else is sent (like the error 500 page mentioned above ?

the transportway : Is there a proxy system involved that could send some sort of error message if the request times out (so that instead of a time out message on the stream you get a StreamCorruptedException because ObjectInputStream does not recognize this message as a java ObjectInputStream) ?

the recepient : (But I can not think of a reason why a correctly written, flushed and closed ObjectOutputStream can not be read as an ObjectInputStream with a StreamCorruptedException).

Do you have a chance to look at the logfiles of the server ?

another idea in this matter (for debugging purposes) :
Why don't you do something like this ?
Instead of using ObjectInputStream to read the stream, open it using a normal InputStream, read it byte wise and put everything into a ByteArrayOutputStream.
Then do
try {
// your ByteArrayOutputStream is baos
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())) ;
Object o = ois.readObject() ;
} catch (Exception e) {
System.out.println("Error while reading using byte[] : " + e);
System.out.println("This is the content : " + new String(baos.toByteArray())) ;
}

This is maybe what you wanted to do anyway when asking to convert an ObjectInputStream into a byte[] for debugging purposes... ;)

George2
November 12th, 2006, 11:47 PM
Thank you gmagoss! Your idea is cool. I will follow it.