This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S: Invalid Stream Header

 
Tom Holt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the post. I managed to get it working using the DataInputStream rather than the ObjectInputStream.
 
GD Deepz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic