Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (602)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
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 4483 times)
0 Members and 1 Guest are viewing this topic.
Offline DrewLols

Senior Devvie

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.

   public void setSaveFile(File saveFile)
      // Assigns the save file
      this.saveFile = saveFile;
         // May create the save file if it does not yet exist
         // Potentially closes streams
         if(!isClosed && in != null)
         // 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)

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.

   at$PeekInputStream.readFully(Unknown Source)
   at$BlockDataInputStream.readShort(Unknown Source)
   at Source)
   at<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: 360
Projects: 11
Exp: 3 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 Devvie

« 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 Devvie

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.

Jesse (14 views)
2015-07-29 04:35:27

Riven (36 views)
2015-07-27 16:38:00

Riven (18 views)
2015-07-27 15:35:20

Riven (21 views)
2015-07-27 12:26:13

Riven (12 views)
2015-07-27 12:23:39

BurntPizza (30 views)
2015-07-25 00:14:37

BurntPizza (41 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (29 views)
2015-07-22 22:16:53

NoxInc (18 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!