Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing the index positioning in InputStream

Tags:

java

android

I have a binary file which contains keys and after every key there is an image associated with it. I want to jump off different keys but could not find any method which changes the index positioning in input stream. I have seen the mark() method but it does not jump on different places.

Does anybody have any idea how to do that?

like image 796
sajjoo Avatar asked Aug 13 '10 07:08

sajjoo


People also ask

How does InputStream read () method work?

read() method reads the next byte of the data from the the input stream and returns int in the range of 0 to 255. If no byte is available because the end of the stream has been reached, the returned value is -1.

What does InputStream Reset do?

The java. io. InputStream. reset() method repositions this stream to the position at the time the mark method was last called on this input stream.


1 Answers

There's a long skip(long n) method that you may be able to use:

Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.

As documented, you're not guaranteed that n bytes will be skipped, so doublecheck the returned value always. Note that this does not allow you to "skip backward", but if it's markSupported(), you can reset() first and then skip forward to an earlier position if you must.


Other options

You may also use java.io.RandomAccessFile, which as the name implies, permits random access with its seek(long pos) method.

You mentioned images, so if you are using Java Advanced Imaging, another possible option is com.sun.media.jai.codec.FileSeekableStream, which is a SeekableStream that takes its input from a File or RandomAccessFile. Note that this class is not a committed part of the JAI API. It may be removed or changed in future releases of JAI.

like image 56
polygenelubricants Avatar answered Oct 23 '22 00:10

polygenelubricants