Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  EofException when instantiating ObjectInputStream?  (Read 2576 times)
0 Members and 1 Guest are viewing this topic.
Offline DrewLols

Senior Duke


Medals: 3
Projects: 1


Noob going through metamorphosis...


« Posted 2012-12-14 00:26:41 »

Can anyone explain to me why this would throw an EofException?  It really doesn't make sense to me.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
   public void setSaveFile(File saveFile)
   {
      // Assigns the save file
      this.saveFile = saveFile;
      try
      {
         // May create the save file if it does not yet exist
         saveFile.createNewFile();
         
         // Potentially closes streams
         if(!isClosed && in != null)
         {
            in.close();
            out.close();
         }
         
         // Creates new in and out streams
         in = new ObjectInputStream(new FileInputStream(saveFile));    // Decides to get thrown here...
         out = new ObjectOutputStream(new FileOutputStream(saveFile));
      }
      catch(IOException e)
      {
         e.printStackTrace();
      }
   }


Basically, I'm trying to load a save file from the directory "data".  When the file does not exist in this folder, I create a new one.  When I do this, however, this exception gets thrown.

1  
2  
3  
4  
5  
java.io.EOFException
   at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
   at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
   at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
   at java.io.ObjectInputStream.<init>(Unknown Source)


This exception does not get thrown when the file already exists.  Any suggestions?

Did you know that 90% of statistics are wrong?
Offline HeroesGraveDev

JGO Kernel


Medals: 269
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2012-12-14 01:17:55 »

An EOFException is thrown when the end of the stream is reached unexpectedly.

I have never used ObjectInputStreams, but I think that they load the header first, and if the file is empty, it reaches the EOS unexpectedly and throws an EOFException.

If the file doesn't exist, you should not be reading from it.

Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #2 - Posted 2012-12-14 23:51:06 »

I don't know why it is not documented but readObject will throw an EOFException once you try to read past the last object. It is an indicator that you are done reading.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Best Username Ever

Junior Duke





« Reply #3 - Posted 2012-12-15 01:46:40 »

What are you expecting from having a FileInputStream and a FileOutputStream of the same file open at the same time to do?
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #4 - Posted 2012-12-16 02:17:14 »

In FileInputStream it returns -1 at the end and doesn't throw an exception. Normal behaviour should not throw an exception. I would expect readObject to return null if it has reached end of stream.

Offline DrewLols

Senior Duke


Medals: 3
Projects: 1


Noob going through metamorphosis...


« Reply #5 - Posted 2012-12-16 16:45:25 »

I should have commented that I had solved my problem earlier, but better late than never, I suppose.  I didn't realize that OIS read the file header before actually its actual use.  I was instantiating an OIS with a FileInputStream pointing to an empty file.

So, in conclusion, I should have created an OIS if, and only if, the file existed and had been written to.   Yawn

Did you know that 90% of statistics are wrong?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (39 views)
2014-10-17 03:59:02

Norakomi (30 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (58 views)
2014-10-14 00:35:47

TehJavaDev (48 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!