• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EOFException while using ObjectInputStream and ObjectOutputSteam

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

this code is from OCP7 programmer certification book by mala gupta. Only path i and class names are changed.
but my code is giving EOFException. Can someone explain why ? I am only writing to the file, still EOFException
Exception in thread "main" java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Sour
ce)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at A.main(A.java:13)
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
this code is from OCP7 programmer certification book by mala gupta. Only path i and class names are changed.
but my code is giving EOFException. Can someone explain why ? I am only writing to the file, still EOFException



I will speculate that the original code didn't have the same file for both output and input.

The file outputstream is causing the file to be zero length. And hence, the inputstream doesn't have anything to read... so when the object inputstream tried to read the header, there is nothing to read, hence EOF.

Henry
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:I will speculate that the original code didn't have the same file for both output and input.


the original code too having same file in both Input and Output stream. i have pasted a sanpshot here.

Henry Wong wrote:The file outputstream is causing the file to be zero length. And hence, the inputstream doesn't have anything to read... so when the object inputstream tried to read the header, there is nothing to read, hence EOF.


in my code, i have commented the read operation. So, it shouldn't give error for just writing ?
Untitled.png
[Thumbnail for Untitled.png]
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are initializing the ObjectInputStream before the ObjectOutputStream. So when the ObjectInputStream is created the file doesn't exist. Fix that to match the order of initialization in the book and you'll get a successful run...
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:You are initializing the ObjectInputStream before the ObjectOutputStream. So when the ObjectInputStream is created the file doesn't exist. Fix that to match the order of initialization in the book and you'll get a successful run...


problem solved, thank you ankit. But why is it so? why we have to declare OutputStream first ?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems like the ObjectOutputStream constructor writes some initial bytes (header) to the file which the ObjectInputStream constructor looks for (as the method name ObjectInputStream.readStreamHeader in the stack trace suggests). So if you provide an empty file to ObjectInputStream constructor then you get EOFException as it's not able to find the header.

[Edit]
Actually this is written in the documentation of the ObjectInputStream constructor:

javadoc wrote:Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.


Although the documentation doesn't say that the constructor will throw EOFException...
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:...So if you provide an empty file to ObjectInputStream constructor then you get EOFException as it's not able to find the header....


Thanks.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:It seems like the ObjectOutputStream constructor writes some initial bytes (header) to the file which the ObjectInputStream constructor looks for (as the method name ObjectInputStream.readStreamHeader in the stack trace suggests). So if you provide an empty file to ObjectInputStream constructor then you get EOFException as it's not able to find the header.


then why not in this case :

it is running fine and writind 'd' to the file . why ??
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<Assumption value="You know you have changed ObjectInputStream to DataInputStream">
The documentation of constructor of DataInputStream doesn't say it fetches any header from the input file. So no exception in case of DataInputStream...
</Assumption>
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:<Assumption value="You know you have changed ObjectInputStream to DataInputStream">
The documentation of constructor of DataInputStream doesn't say it fetches any header from the input file. So no exception in case of DataInputStream...
</Assumption>


yes, i intentionally have changed ObjectInputStream to DataInputStream to ask this question. Well frankly Ankit, I still not got why ObjectInputStream throw exception and what if it doesn't have made to do so ? Didn't understand the documentation .
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Puspender, the constructor of the ObjectInputStream class reads the input file and looks for some headers to check if it's a valid file i.e. it's a file which contains serialized objects. That's what this line from the documentation of the constructor mean

A serialization stream header is read from the stream and verified.


The documentation doesn't specify that if the file is empty, then the constructor throws an EOFException as it's not able to read the serialization headers.

I still not got why ObjectInputStream throw exception and what if it doesn't have made to do so ?


I didn't understand the last part of your question...
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Puspender, the constructor of the ObjectInputStream class reads the input file and looks for some headers to check if it's a valid file i.e. it's a file which contains serialized objects. That's what this line from the documentation of the constructor mean


and ObjectOutputSrtream guarantee that now the file can store the serialized objects or it may have such objects. That's why no exception if ObjectOutputSrtream is written first . Am i right ?
The documentation doesn't specify that if the file is empty, then the constructor throws an EOFException as it's not able to read the serialization headers.

Ankit Garg wrote:I didn't understand the last part of your question...


I meant, if the coder of this method allows the ObjectInputStream to be written first(no exception is thrown) . Then what will happen ?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't exactly know the reason why the ObjectInputStream constructor verifies the header in the file (or what the purpose of the header is). I suppose the API designers might have made it such that the header is read when you try to read the first object from the file (as opposed to reading the header during ObjectInputStream construction). But again I guess the decision was made after some consideration and since I don't know the purpose of the header I don't know why it's implemented the way it is...
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender wrote:

Ankit Garg wrote:Puspender, the constructor of the ObjectInputStream class reads the input file and looks for some headers to check if it's a valid file i.e. it's a file which contains serialized objects. That's what this line from the documentation of the constructor mean


and ObjectOutputSrtream guarantee that now the file can store the serialized objects or it may have such objects. That's why no exception if ObjectOutputSrtream is written first . Am i right ?


have i guessed it right ??
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic