java 更改 InputStream 中的索引定位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3474911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:59:38  来源:igfitidea点击:

changing the index positioning in InputStream

javaandroid

提问by sajjoo

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.

我有一个包含密钥的二进制文件,在每个密钥之后都有一个与之关联的图像。我想跳出不同的键,但找不到任何更改输入流中索引定位的方法。我见过这个mark()方法,但它不会跳到不同的地方。

Does anybody have any idea how to do that?

有人知道该怎么做吗?

回答by polygenelubricants

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

long skip(long n)您可以使用一种方法:

Skips over and discards nbytes of data from this input stream. The skipmethod 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 nbytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If nis negative, no bytes are skipped.

跳过并丢弃n此输入流中的数据字节。由于skip各种原因,该方法最终可能会跳过一些较小数量的字节,可能是0。这可能由多种情况中的任何一种引起;n在跳过字节之前到达文件末尾只是一种可能性。返回实际跳过的字节数。如果n为负,则不跳过任何字节。

As documented, you're not guaranteed that nbytes 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 thenskipforward to an earlier position if you must.

正如所记录的,您不能保证n会跳过字节,因此请始终仔细检查返回的值。请注意,这并不是让你“快退”,但如果它markSupported(),你可以reset()先和skip前进到一个较早的位置,如果你必须这样做。



Other options

其他选项

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

您也可以使用java.io.RandomAccessFile,顾名思义,它允许使用其seek(long pos)方法进行随机访问。

You mentioned images, so if you are using Java Advanced Imaging, another possible option is com.sun.media.jai.codec.FileSeekableStream, which is a SeekableStreamthat takes its input from a Fileor 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.

您提到了图像,因此如果您使用Java Advanced Imaging,另一个可能的选项是com.sun.media.jai.codec.FileSeekableStream,它是SeekableStream从 aFile或获取输入的 a RandomAccessFile。请注意,此类不是 JAI API 的已提交部分。它可能会在 JAI 的未来版本中被删除或更改。