aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S: Invalid Stream Header Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "B&S: Invalid Stream Header" Watch "B&S: Invalid Stream Header" New topic
Author

B&S: Invalid Stream Header

Tom Holt
Greenhorn

Joined: Oct 06, 2004
Posts: 3
I am just starting on the Bodgitt and Scarper assignment and have created a class to read the data file - I hope this will be a starting point from which I can write the rest of the db server code. The only problem is that it crashes when I try to instantiate the ObjectInputStream. I get the following error:

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:737)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:253)
at suncertify.db.DataReader.read(DataReader.java:28)
at suncertify.db.DataReader.main(DataReader.java:57)

Here is a code snippet:

FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
ByteBuffer bb = ByteBuffer.allocate((int)fc.size());
fc.read(bb);

ByteArrayInputStream bais = new ByteArrayInputStream(bb.array());
ObjectInputStream ois = new ObjectInputStream(bais);

long magicCookieValue = ois.readLong();

Can anyone give me a clue what I have done wrong.

Many Thanks,
Tom.
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Tom Holt:
I am just starting on the Bodgitt and Scarper assignment and have created a class to read the data file - I hope this will be a starting point from which I can write the rest of the db server code. The only problem is that it crashes when I try to instantiate the ObjectInputStream. I get the following error:

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:737)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:253)
at suncertify.db.DataReader.read(DataReader.java:28)
at suncertify.db.DataReader.main(DataReader.java:57)

Here is a code snippet:

FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
ByteBuffer bb = ByteBuffer.allocate((int)fc.size());
fc.read(bb);

ByteArrayInputStream bais = new ByteArrayInputStream(bb.array());
ObjectInputStream ois = new ObjectInputStream(bais);

long magicCookieValue = ois.readLong();

Can anyone give me a clue what I have done wrong.

Many Thanks,
Tom.


The file is not an Object file, so ObjectInputStream can't be used. The spec says that the file is a legacy file containing text stored in 8 bit ascii. You need either an InputStream or a RandomAccessFile opened on that file. You will need to read byte arrays and then convert them into Strings.
Tom Holt
Greenhorn

Joined: Oct 06, 2004
Posts: 3
Thanks for the post. I managed to get it working using the DataInputStream rather than the ObjectInputStream.
GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
readInt() --- > will get you the magic cookie from start of file
readInt() --- > again will get you the record length in bytes
readShort() ---> will return the number of fields

BUT how do you get the length of bytes for each field "name" and the field length in bytes.

According to my spec from B&S:

Repeated for each field in a record
2 byte numeric, length in bytes of field name
n bytes(defined by previous entry), field name
2 byte numeric, field length in bytes
end of repeating block

I am confused by the above spec, how do I determine the above, any suggestion on the code would help
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: B&S: Invalid Stream Header