Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

StreamCorruptedException

Thread view: 
pageV - 04 May 2004 17:17 GMT
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 - 04 May 2004 17:21 GMT
> 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>

Signature

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 - 04 May 2004 18:00 GMT
> > I am getting a StreamCorruptedException when I try input.readObject();
> > When I search in google, most people get more detail than just
[quoted text clipped - 8 lines]
> 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 - 04 May 2004 18:13 GMT
> "Andrew Thompson" <SeeMySites@www.invalid> wrote in message
>>
>>> 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.

Signature

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 - 04 May 2004 20:27 GMT
> > "Andrew Thompson" <SeeMySites@www.invalid> wrote in message
> >>
[quoted text clipped - 16 lines]
> 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
Roedy Green - 04 May 2004 18:40 GMT
>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
pageV - 04 May 2004 20:31 GMT
> >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.
Roedy Green - 04 May 2004 20:44 GMT
>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
pageV - 04 May 2004 21:23 GMT
> >output.writeObject (Dr);
> >              output.flush();
[quoted text clipped - 11 lines]
> 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.
Roedy Green - 04 May 2004 23:23 GMT
>The program opens an input and output at the same time on the same file

I would not do that.  Write the file, close it, then read it.
See http://mindprod.com/fileio.html for sample code.

Write files have exclusive access. I'm not sure if Java on read gives
exclusive access or shared access.  In any case there are no open
modifiers to tell the OS.

I'm surprised you even got the file open.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green - 04 May 2004 23:28 GMT
>         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.
pageV - 05 May 2004 00:44 GMT
> >         input = new ObjectInputStream(new FileInputStream("diveLogs.dat"));
>
[quoted text clipped - 13 lines]
> 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
Roedy Green - 05 May 2004 01:14 GMT
>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
pageV - 05 May 2004 02:10 GMT
> >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
[quoted text clipped - 13 lines]
> 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 - 05 May 2004 03:38 GMT
> > >I moved the open for output into addFile();  I also close the input at
> the
[quoted text clipped - 28 lines]
> > 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 - 05 May 2004 07:54 GMT
> 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

Signature

[  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 - 05 May 2004 16:06 GMT
> > 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
[quoted text clipped - 38 lines]
>
> --
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
Roedy Green - 04 May 2004 18:45 GMT
>  output.writeObject (Dr);
>              output.flush();
your error likely is in the code to open and close both files.

You did not post that.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green - 04 May 2004 19:59 GMT
>>  output.writeObject (Dr);

It is both confusing and irritating when you violate coding
conventions like variables begin with lower case and classes upper
case. It is particularly important to follow them when posting
snippets.

I wish Javac would mark violations at least with warnings.

see http://mindprod.com/jgloss/codingconventions.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green - 04 May 2004 18:39 GMT
>How do
>I get more detail on what is causing the StreamCorruptedException?

presumably you got this on a readObject?

The file may have been damaged.
Missing classes, out of date classes in the stream might cause that or
similar error.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green - 04 May 2004 20:00 GMT
>StreamCorruptedException.

As a first line of defense on any obtuse error message or exception,
look it up in http://mindprod.com/jgloss/errormessages.html

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.