Reputation: 21
I've been messing around with Input/Output in Java. I'm still at the very basic level of the mechanic. I've learned how to use FileInputStream and FileOutputStream to read an write data to/from a text file.
There are two methods in the FileInputStream class that I'm trying to learn to use. The mark() and reset(). From the little that I've read, I need to wrap the stream in a buffer.
If this is true, then wouldn't the buffer object(not sure if that's what you call it) have it's own mark() and reset()?
Why would these methods exist if they cannot be used without buffer?
Upvotes: 1
Views: 933
Reputation: 999
The mark/reset availability is not directly tied to the presence of a buffer in an InputStream. Any InputStream implementation that can efficiently rollback its "reading pointer" position on the stream should implement the functionality. A ByteArrayInputStream is an example of InputStream that doesn't extend BufferedInputStream and yet is capable of going back to any position as opposed to the BufferedInputStream that can only go back a few positions (up to the size of the buffer).
Upvotes: 2
Reputation: 3097
From the InputStream
javadoc you can read that the mark()
method does nothing. And it is not being overloaded in FileInputStream
, neither is markSupported()
that returns false
, also for FileInputStream
.
So it's not possible to use it with a raw FileInputStream
. However, you can wrap it in a BufferedInputStream
which does implement mark()
. Hence the buffer thing...
InputStream is = new BufferedInputStream(new FileInputStream(myFile));
is.mark(1024);
...
is.reset();
Upvotes: 0