• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

I'm getting a really weird IOException...

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so I have this program, which reads two characters, appends them to a StringBuffer, adds the resulting string to a Vector, and does the same until the end of file is reached.

Now, since I didn't managed to remember or find anything on the documentation about an End Of File flag (I remember you can see if you reached it on C with the feof(An input stream) function), I found that I can use the available() function from the FileInputStream class, which returns how much bytes are between the file pointer and the EOF marker. So, I have a While loop like this:


The problem? This code, compiled with JCreator 3.50 LE, worked yesterday in my computer, but it doesn't works anymore! When I run my program, I get the following stack dump (BTW, I think "Controlador no v�lido" translates to "Invalid controller" or something; I've never seen the English version since my computer is in Spanish):


Any idea on what could be going on?
[ September 29, 2007: Message edited by: Acoyani Garrido Sandoval ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what's the cause of this exception. However, the available() method doesn't do what you think it does, and there's really no reason to use it at all here.

[AGS]: found that I can use the available() function from the FileInputStream class, which returns how much bytes are between the file pointer and the EOF marker.

No - read what the API for this method says:

Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

In some cases, a non-blocking read (or skip) may appear to be blocked when it is merely slow, for example when reading large files over slow networks.


[ emphasis added by me ]

It's possible for available() to return less than the total bytes remaining in the file - it may even return 0 - simply because those bytes are not available right now. For example, the disc drive may be out of position it the moment, and it will take a few milliseconds to position it.

If you want to detect the end of a file, don't use available(). In fact it's hard to imagine anything that available() is really good for - I advise ignoring it in general. Instead, you can look at what the API for read() says:

Returns:
the next byte of data, or -1 if the end of the file is reached./quote]
If you want to know whether the end of a file has been reached, just check if read() returned a -1. If it didn't, then the file still has data.

 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just read the input character into a byte and check for -1.
available() is rarely useful.

available() can be useful when you are reading across a network and want to know if you should sleep a bit more to wait for more input. But its not an EOF indicator.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic