I have a binary file which contains image.i have to jump on different locations in file to read the image file. So far i am using mark and reset methods but these are not helping me as i want. please somebody help me about that i,ll be really thankful.and i am using Input Stream to read the file.
can i use it in input Stream?
sajjoo
2010-09-02 06:49:55
If you already have FileInputStream (not generic InputStream!), you can use getChannel() method to get FileChannel. But opening new RandomAccessFile is simpler.
Peter Štibraný
2010-09-02 06:51:45
@sajjoo - [`FileInputStream`](http://download-llnw.oracle.com/javase/6/docs/api/index.html?java/lang/String.html) provides a `getChannel()` method.
Andreas_D
2010-09-02 07:03:25
yes i got it sorry i miss understood.
sajjoo
2010-09-02 07:06:49
Actually i am using 3 input stream.... inputStream -> CountingInputstream -> SwappedInputstream. and i am developing an application for android. InputStream iStream = getApplicationContext().getResources().openRawResource(R.raw.map);so i need jumping on different addresses or if i can go back to start of file some how in stream.
sajjoo
2010-09-02 07:12:41
The InputStream interface doesn't provide ways to seek to certain positions, because it's meant to abstract away a sequential input stream, and certain implementations might not support that (think about a socket, for example). So if you want to seek, you need a more specialized class: FileInputStream's getChannel(), a RandomAccessFile, or some other class that allows seeking.Looking at the android API (I'm not familiar with it, just with Java), you could potentially use Resources.openRawResourceFd() and build a FileInputStream from the returned AssetFileDescriptor.
vanza
2010-09-02 08:21:49
+2
A:
You can use the java.io.RandomAccessFile to do this. The methods seek(long) and getFilePointer() will help to jump to different offsets in the file and come back to original offsets:
RandomAccessFile f = new RandomAccessFile("/my/image/file", "rw");
// read some data.
long positionToJump = 10L;
long origPos = f.getFilePointer(); // store the original position
f.seek(positionToJump);
// now you are at position 10, start reading from here.
// go back to original position
f.seek(origPos);
naikus
2010-09-02 06:50:28