• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

B&S: Invalid Stream Header

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the post. I managed to get it working using the DataInputStream rather than the ObjectInputStream.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic