Weird with FileinputStream

I saw some weird behaviour, don't know why. I did the following:

File f = new File("abc");

1. FileInputStream fin = new FileInputStream(f);

2. InputStreamReader in = new InputStreamReader (fin);

3. BufferedReader br = new BufferedReader(in);

4. Read everything from br.read

5. BufferedInputStream bin = new BufferedInputStream (fin);

6. WHEN I READ FROM STEP 5 I GET GARBAGE

7. Now I changed no. 5 to BufferedInputStream bin = new BufferedInputStream (new FileInputStream(f));

8. NOW AFTER REPLACING LINE 5 TO LINE 7 IT WORKED JUST FINE.

Am I missing something. Can't I re-use fin ?

[651 byte] By [mohitanchliaa] at [2007-11-27 11:50:16]
# 1

> Can't I re-use fin ?

Short answer: no.

BigDaddyLoveHandlesa at 2007-7-29 18:30:20 > top of Java-index,Java Essentials,Java Programming...
# 2

Long answer: you probably can, using the reset() method, but it's probably not worth the effort, just create a new inputstream :)

http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html#reset()

hunter9000a at 2007-7-29 18:30:20 > top of Java-index,Java Essentials,Java Programming...
# 3

Don't get the OP's hopes up. FileInputStream inherits reset's implementation

from InputStream, and the javadoc for reset states:

<quote>

The method reset for class InputStream does nothing except throw an IOException.

</quote>

And indeed, I called reset on a FileInputStream and it dutifully threw an

IOException with the message, "mark/reset not supported".

BigDaddyLoveHandlesa at 2007-7-29 18:30:20 > top of Java-index,Java Essentials,Java Programming...
# 4

> Don't get the OP's hopes up. FileInputStream inherits

> reset's implementation

> from InputStream, and the javadoc for reset states:

>

> <quote>

> The method reset for class InputStream does nothing

> except throw an IOException.

> </quote>

>

> And indeed, I called reset on a FileInputStream and

> it dutifully threw an

> IOException with the message, "mark/reset not

> supported".

I guess the answer's not so long after all. We'll just call this a lesson to the OP to read the entire documentation before making assumptions :-\

hunter9000a at 2007-7-29 18:30:20 > top of Java-index,Java Essentials,Java Programming...
# 5

I read the brief documentation about BufferedInputStream, it says:

A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.

--

But it doesn't say anything about changing the FileInputStream's mark operation. Is there a place where I can read about why FileInputStream can't be re-used if already is used by other BufferedReader/BuferredInputStream ?

mohitanchliaa at 2007-7-29 18:30:20 > top of Java-index,Java Essentials,Java Programming...
# 6

> But it doesn't say anything about changing the

> FileInputStream's mark operation.

It doesn't. It provides one of its own.

> Is there a place

> where I can read about why FileInputStream can't be

> re-used if already is used by other

> BufferedReader/BuferredInputStream ?

It can be reused, but not usefully. Obviously its pointer has already been moved by the first read, and also by the read-ahead of the BufferedReader to fill its buffer.

Don't do this.

ejpa at 2007-7-29 18:30:20 > top of Java-index,Java Essentials,Java Programming...
# 7

Thanks, that's what I was wanting to know. Looking at the documentation it sounds as if BufferedReader maintains it's own pointer and it increments it to point to the location from where it wants to read. I didn't know it actually increments the pointer of FileInputStream and not it's own. So that's why I was thinking of re-suing FileInputStream.

mohitanchliaa at 2007-7-29 18:30:20 > top of Java-index,Java Essentials,Java Programming...