views:

720

answers:

4

I need to write a function that takes in some kind of input stream thing (e.g. an InputStream or a FileChannel) in order to read a large file in two passes: once to precompute some capacities, and second to do the "real" work. I do not want the whole file loaded into memory at once (unless it is small).

Is there an appropriate Java class that provides this capability? FileInputStream itself does not support mark()/reset(). BufferedInputStream does, I think, but I'm not clear whether it has to store the whole file to do this.

C is so simple, you just use fseek(), ftell(), and rewind(). :-(

+2  A: 

Check out java.io.RandomAccessFile

Rich Seller
ok, thanks. looks like I can use it to open the file and then use FileChannel as the class to manipulate/read/write it.
Jason S
Too bad RandomAccessFile doesn't implement InputStream with its mark()/reset() methods. >:(
Jason S
You can role your own fairly easily (if not that elegantly), see http://www.coderanch.com/t/277378/Streams/java/InputStream-RandomAccessFile-best-way for an example
Rich Seller
thanks but it's the other direction (accessing a RandomAccessFile as an InputStream). FileChannel is an OK class to pass around in my interface.
Jason S
+2  A: 

java.nio.channels.FileChannel has a method position(long) to reset the position back to zero like fseek() in C.

Arne Burmeister
+1  A: 

RandomAccessFile is what you want:

dfa
+3  A: 

BufferedInputStream supports mark by buffering the content in memory. It is best reserved for relatively small look-aheads of a predictable size.

Instead, RandomAccessFile can be used directly, or it could serve as the basis for a concrete InputStream, extended it a "rewind" method.

Alternatively, a new FileInputStream can be opened for each pass.

erickson
I'm switching to this answer, because I need to use an interface that I can share between regular files and in-memory buffers. Grrrrr. I'm writing my own interface RewindableStream + implementation classes, one of which wraps RandomAccessFile.
Jason S