StreamCorruptedException  
Author Message
Krzysztof Drzy縟縴k





PostPosted: 2003-12-2 6:35:00 Top

java-programmer, StreamCorruptedException Hi
sorry my english is not very good [:())

i have something like that:

Socket------------------------------------------------class1------ObjectOutp
utStream
|
|
|
inner class in class1-thread-ObjectInputSt.
|
-----------------class2
| |
| inner class in class2 thread ObjectInputStream,
ObjectOutputStream
|
|
|
--------class3- and here when i create ObjectOutputStream in inner class in
class1 in thread
jumps out exception StreamCorruptedException
why?
please help
i don't know what i have should do?
Czeski






 
pageV





PostPosted: 2004-5-5 0:17:00 Top

java-programmer >> StreamCorruptedException I am getting a StreamCorruptedException when I try input.readObject();
When I search in google, most people get more detail than just
StreamCorruptedException. So I commented out the catch block to
try to get the error show up in the console and got an unreported exception
error; must be caught or declared thrown. So I added "throws IOException"
to
the method viewDive(), but still get an unreported exception error. How do
I get more detail on what is causing the StreamCorruptedException?

Ralph


 
Andrew Thompson





PostPosted: 2004-5-5 0:21:00 Top

java-programmer >> StreamCorruptedException On Tue, 4 May 2004 10:17:14 -0600, pageV wrote:

> I am getting a StreamCorruptedException when I try input.readObject();
> When I search in google, most people get more detail than just
> StreamCorruptedException. So I commented out the catch block to
> try to get the error show up in the console

Why? Do you have an e.printStackTrace() inside
the catch? That should print all the information
available.

Of course, it is much easier to tell what you
are doing wrong if you actually provide _code_
<http://www.physci.org/codes/sscce.jsp>

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
 
pageV





PostPosted: 2004-5-5 1:00:00 Top

java-programmer >> StreamCorruptedException
"Andrew Thompson" <email***@***.com> wrote in message
news:1amzr0c07scg6.1wxcyby4l2w3z$email***@***.com...
> On Tue, 4 May 2004 10:17:14 -0600, pageV wrote:
>
> > I am getting a StreamCorruptedException when I try input.readObject();
> > When I search in google, most people get more detail than just
> > StreamCorruptedException. So I commented out the catch block to
> > try to get the error show up in the console
>
> Why? Do you have an e.printStackTrace() inside
> the catch? That should print all the information
> available.
>
> Of course, it is much easier to tell what you
> are doing wrong if you actually provide _code_
> <http://www.physci.org/codes/sscce.jsp>
>
put in the printStackTrace(). It gives me
java.io.StreamCorruptedException
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1301)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
at divelog.DiveHandler.viewDive(DiveHandler.java:204)

etc. Doesn't give a reason anywhere.
When I search in google, people usually get a reason in the first line
for example Type code out of range, is 0, or something about the header.

The code that writes the object

try
{ //open try
Dr = new DiveRecord();
Dr.setDive(t, d, dth, s, e, u, bt, c, v, cts);
output.writeObject (Dr);
output.flush();

The read code that causes the error

Dr = (DiveRecord) input.readObject();

Part of the DiveRecord class

public class DiveRecord implements Serializable
{
//Instance variables
private String date, type, cts;
private int depth, start, end, used,
time, count, vis;
public DiveRecord ()
{
setDive();
}
public void setDive()
{
type = "";
date = "00/00/00";
depth = 00;
start = 0000;
end = 0000;
used = 0000;
time = 00;
count = 000;
vis = 00;
cts = "";
}

public void setDive (
String t,String d, int dth,
int s, int e, int u, int bt,
int c, int v, String cts)
{

this.type = t;
this.date = d;
this.depth = dth;
this.start = s;
this.end = e;
this.used = u;
this.time = bt;
this.count = c;
this.vis = v;
this.cts = cts;

}

Ralph

> Andrew Thompson
> http://www.PhySci.org/ Open-source software suite
> http://www.PhySci.org/codes/ Web & IT Help
> http://www.1point1C.org/ Science & Technology


 
 
Andrew Thompson





PostPosted: 2004-5-5 1:13:00 Top

java-programmer >> StreamCorruptedException On Tue, 4 May 2004 11:00:23 -0600, pageV wrote:
> "Andrew Thompson" <email***@***.com> wrote in message
>> On Tue, 4 May 2004 10:17:14 -0600, pageV wrote:
>>
>>> I am getting a StreamCorruptedException when I try input.readObject();
....
>> <http://www.physci.org/codes/sscce.jsp>
....
> at divelog.DiveHandler.viewDive(DiveHandler.java:204)
.....
> try
> { //open try
> Dr = new DiveRecord();
...
Now, here's a test for you. What do the second
and third letters of S_SC_CE stand for?

[ Code snippets give me a headache. ]

If you provide the code it might be possible
for the reader to understand which line is '204'
for example.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
 
pageV





PostPosted: 2004-5-5 3:27:00 Top

java-programmer >> StreamCorruptedException
"Andrew Thompson" <email***@***.com> wrote in message
news:email***@***.com...
> On Tue, 4 May 2004 11:00:23 -0600, pageV wrote:
> > "Andrew Thompson" <email***@***.com> wrote in message
> >> On Tue, 4 May 2004 10:17:14 -0600, pageV wrote:
> >>
> >>> I am getting a StreamCorruptedException when I try input.readObject();
> ....
> >> <http://www.physci.org/codes/sscce.jsp>
> ....
> > at divelog.DiveHandler.viewDive(DiveHandler.java:204)
> .....
> > try
> > { //open try
> > Dr = new DiveRecord();
> ...
> Now, here's a test for you. What do the second
> and third letters of S_SC_CE stand for?
>
> [ Code snippets give me a headache. ]
>
> If you provide the code it might be possible
> for the reader to understand which line is '204'
> for example.
>
The code is at
http://java.sun.com/developer/onlineTraining/new2java/divelog/part5/DiveHandler.java

my copy in Kawa has extra white space at the top,so line 204 is
Dr = (DiveRecord) input.readObject();

The DiveRecord class is

//Represents a record of a dive
package divelog;
import java.io.*;
import java.io.Serializable;

public class DiveRecord implements Serializable
{
//Instance variables
private String date, type, cts;
private int depth, start, end, used,
time, count, vis;
public DiveRecord ()
{
setDive();
}
public void setDive()
{
type = "";
date = "00/00/00";
depth = 00;
start = 0000;
end = 0000;
used = 0000;
time = 00;
count = 000;
vis = 00;
cts = "";
}

public void setDive (
String t,String d, int dth,
int s, int e, int u, int bt,
int c, int v, String cts)
{

this.type = t;
this.date = d;
this.depth = dth;
this.start = s;
this.end = e;
this.used = u;
this.time = bt;
this.count = c;
this.vis = v;
this.cts = cts;

}

public String toString()
{
return type + date + depth + start +
end + used + time + count + vis + cts;
}

public String getDte()
{
return date;
}
public int getDepth ()
{
return depth;
}
public String getType ()
{
return type;
}
public int getStart ()
{
return start;
}
public int getEnd ()
{
return end;
}

public int getUsed ()
{
return used;
}
public int getTime()
{
return time;
}
public int getCount ()
{
return count;
}
public int getVis()
{
return vis;
}
public String getComments()
{
return cts;
}





}

It's hard to make this self-contained and small. DiveHandler is called by
another class which calls several classes. I'll work on it.
Ralph
> --
> Andrew Thompson
> http://www.PhySci.org/ Open-source software suite
> http://www.PhySci.org/codes/ Web & IT Help
> http://www.1point1C.org/ Science & Technology


 
 
pageV





PostPosted: 2004-5-5 3:32:00 Top

java-programmer >> StreamCorruptedException
"Roedy Green" <email***@***.com> wrote in message
news:email***@***.com...
> On Tue, 4 May 2004 11:00:23 -0600, "pageV" <email***@***.com> wrote
> or quoted :
>
> >The code that writes the object
>
> have a look at http://mindprod.com/jgloss/fileio.html
> to show you how to write and read objects.
> You are probably trying to read human-readable data or something other
> than a stream created by writeObject.
>
The DiveHandler class is at
http://java.sun.com/developer/onlineTraining/new2java/divelog/part5/DiveHandler.java
as far as I can tell, the file is written to by
output.writeObject (Dr);
output.flush();
How do you get printStackTrace to give a reason for the error? The examples
I saw in google often had a reason in the first line.
Ralph
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


 
 
pageV





PostPosted: 2004-5-5 4:24:00 Top

java-programmer >> StreamCorruptedException
"Roedy Green" <email***@***.com> wrote in message
news:email***@***.com...
> On Tue, 4 May 2004 13:31:31 -0600, "pageV" <email***@***.com> wrote
> or quoted :
>
> >output.writeObject (Dr);
> > output.flush();
>
> You still are not showing the code that is important.
>
> Show every line that has the word "output" in it.
>
> I want to see how you create that file. I want to see if you close it.
>
> I also want to see every line that mentions your inputstream and how
> you got it.
>
> You might want to look in the file created with a hex viewer and see
> if it looks plausible. You should see some recognisable ints or
> Strings.

The program opens an input and output at the same time on the same file

try
{ //open try
output = new ObjectOutputStream (
new FileOutputStream ("diveLogs.dat", true));
} catch (IOException ioex)
{//open catch
JOptionPane.showMessageDialog(null,
"Error1 during reading file",
null, JOptionPane.ERROR_MESSAGE);
}//close catch
try{
input = new ObjectInputStream(
new FileInputStream("diveLogs.dat"));
}
catch (IOException ioex)
{//open catch
JOptionPane.showMessageDialog(null,
"Error2 during reading file",
null, JOptionPane.ERROR_MESSAGE);
}//close catch

after the output.writeObject (Dr);
output.flush();

is if(input != null) {
input.close();
input = null;
but the program never closes the output. I'll have to look at the program
to see the best place to close it. This program is from an example
application from java.sun.com. It had some other errors in it as well. I
commented out the
code that opens the output, but still got the same error.

in viewDive()
try {

DiveRecord Dr;

if(input == null) {
try{
input = new ObjectInputStream(new FileInputStream("diveLogs.dat"));
This line doesn't cause the error, it happens later at
Dr = (DiveRecord) input.readObject();


> --
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


 
 
pageV





PostPosted: 2004-5-5 7:44:00 Top

java-programmer >> StreamCorruptedException
"Roedy Green" <email***@***.com> wrote in message
news:email***@***.com...
> On Tue, 4 May 2004 14:23:51 -0600, "pageV" <email***@***.com> wrote
> or quoted :
>
> > input = new ObjectInputStream(new
FileInputStream("diveLogs.dat"));
>
> What is happening does not make sense. The read open should fail
> either because the file is not there yet, or because it is open for
> write already.
>
> It may be an oversight in the OS since no one does what you are doing.
> You follow very close on the heels of the write open. Perhaps that is
> how it gets confused.
>
> Any way don't do that. If you want simultaneous read write use a
> RandomAccessFile.
>
> --
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

I moved the open for output into addFile(); I also close the input at the
start of addFile() before opening for output. I close the output at the end
of addFile(). Still get the error when I try to read, in viewDive(). This
program is hard to fix, since someone else wrote it.

Ralph


 
 
pageV





PostPosted: 2004-5-5 9:11:00 Top

java-programmer >> StreamCorruptedException
"Roedy Green" <email***@***.com> wrote in
message news:email***@***.com...
> On Tue, 4 May 2004 17:44:22 -0600, "pageV" <email***@***.com> wrote
> or quoted :
>
> >I moved the open for output into addFile(); I also close the input at
the
> >start of addFile() before opening for output. I close the output at the
end
> >of addFile(). Still get the error when I try to read, in viewDive().
This
> >program is hard to fix, since someone else wrote it.
>
> I can't follow you. You must do things in this order:
>
> Put in some debug code to make sure you are doing things in that
> order.
>
> open output
> write output
> close output
>
> open input
> read input
> close input.
The first time through,I tried to view without adding any records. I got
open input and still the error.
So I deleted the file, got an error on open input because there was no file,
then created and added a record. I now no longer get the other error when
viewing the record. I looked in notepad and the file looks slightly
different than before, so it must have been corrupted the first time made.

Thanks for your help
Ralph
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


 
 
pageV





PostPosted: 2004-5-5 10:38:00 Top

java-programmer >> StreamCorruptedException
"pageV" <email***@***.com> wrote in message
news:c79esl$1abvt$email***@***.com...
>
> "Roedy Green" <email***@***.com> wrote in
> message news:email***@***.com...
> > On Tue, 4 May 2004 17:44:22 -0600, "pageV" <email***@***.com> wrote
> > or quoted :
> >
> > >I moved the open for output into addFile(); I also close the input at
> the
> > >start of addFile() before opening for output. I close the output at
the
> end
> > >of addFile(). Still get the error when I try to read, in viewDive().
> This
> > >program is hard to fix, since someone else wrote it.
> >
> > I can't follow you. You must do things in this order:
> >
> > Put in some debug code to make sure you are doing things in that
> > order.
> >
> > open output
> > write output
> > close output
> >
> > open input
> > read input
> > close input.
> The first time through,I tried to view without adding any records. I got
> open input and still the error.
> So I deleted the file, got an error on open input because there was no
file,
> then created and added a record. I now no longer get the other error when
> viewing the record. I looked in notepad and the file looks slightly
> different than before, so it must have been corrupted the first time made.
>
> Thanks for your help
> Ralph
> > Canadian Mind Products, Roedy Green.
> > Coaching, problem solving, economical contract programming.
> > See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
>

added a second record. Viewing records, get no error on the first
but get the error on the second. Printed out all the opens and closes
and output is always closed before input is opened. Something must be wrong
with the file writing. This is what I see in notepad

 sr divelog.DiveRecordvD!"\?l
I countI depthI endI startI timeI usedI visL ctst
Ljava/lang/String;L dateq ~ L typeq ~ xp 
( ? - ? t t 12/12/03t Shore sr
divelog.DiveRecordvD!"\?l
I countI depthI endI startI timeI usedI visL ctst
Ljava/lang/String;L dateq ~ L typeq ~ xp   ( ? * ? t
t 10/12/03t Boat

I will delete again and try again
Ralph


 
 
Gordon Beaton





PostPosted: 2004-5-5 14:55:00 Top

java-programmer >> StreamCorruptedException On Tue, 4 May 2004 20:38:15 -0600, pageV wrote:
> added a second record. Viewing records, get no error on the first
> but get the error on the second. Printed out all the opens and
> closes and output is always closed before input is opened.

It sounds like you open and close the output more than once when
storing objects.

Did you do this?

- open the output
- add both records
- close the output

or this?

- open the output
- add record 1
- close the output
- open the output for append
- add record 2
- close the output

These are quite different situations. Most importantly, you need to
realize that each time you create a new ObjectOutputStream, a special
header is appended to the underlying OutputStream.

When you later create an ObjectInputStream to read your stored
objects, the stream header is read from the InputStream and then you
can start to read the serialized objects. If a second stream header is
unexpectedly found "mid stream", and exception will be raised and you
will fail to read any subsequent objects.

The moral of this story is that you should open the output *once*,
store all of your objects, then close the output. To read back, open
the input *once*, read all of the objects, then close the input. It
isn't impossible to store objects independently in the file, however
it is rather complicated and you should understand the simple case
first.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
 
pageV





PostPosted: 2004-5-5 23:07:00 Top

java-programmer >> StreamCorruptedException
"Gordon Beaton" <email***@***.com> wrote in message
news:40988faf$email***@***.com...
> On Tue, 4 May 2004 20:38:15 -0600, pageV wrote:
> > added a second record. Viewing records, get no error on the first
> > but get the error on the second. Printed out all the opens and
> > closes and output is always closed before input is opened.
>
> It sounds like you open and close the output more than once when
> storing objects.
>
> Did you do this?
>
> - open the output
> - add both records
> - close the output
>
> or this?
>
> - open the output
> - add record 1
> - close the output
> - open the output for append
> - add record 2
> - close the output
>
> These are quite different situations. Most importantly, you need to
> realize that each time you create a new ObjectOutputStream, a special
> header is appended to the underlying OutputStream.
>
> When you later create an ObjectInputStream to read your stored
> objects, the stream header is read from the InputStream and then you
> can start to read the serialized objects. If a second stream header is
> unexpectedly found "mid stream", and exception will be raised and you
> will fail to read any subsequent objects.
>
> The moral of this story is that you should open the output *once*,
> store all of your objects, then close the output. To read back, open
> the input *once*, read all of the objects, then close the input. It
> isn't impossible to store objects independently in the file, however
> it is rather complicated and you should understand the simple case
> first.
>
> /gordon
>
> --
I close each time. If I want to view records before saving them all,
I'll either have to make a new file each time or put all the records
in a single Vector.

Ralph



> [ do not email me copies of your followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e


 
 
lavitha





PostPosted: 2006-2-21 14:15:00 Top

java-programmer >> StreamCorruptedException I am getting this exception when i communicate with my servlet to
applet.My code is OutputStream out = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(uid); oos.flush(); oos.close();
by anna - 11:11am