如何查看输入流中的前两个字节?
应该很简单:我有一个输入流,我想在其中查看(而不是读取)前两个字节,即我希望输入流的“当前位置”在我查看后仍然为 0。 最好和最安全的方法是什么?
答案 - 正如我所怀疑的,解决方案是将其包装在提供可标记性的 BufferedInputStream 中。 谢谢拉斯穆斯。
Should be pretty simple: I have an InputStream where I want to peek at (not read) the first two bytes, i.e. I want the "current position" of the InputStream to stil be at 0 after my peeking. What is the best and safest way to do this?
Answer - As I had suspected, the solution was to wrap it in a BufferedInputStream which offers markability. Thanks Rasmus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于一般的 InputStream,我会将其包装在 BufferedInputStream 中并执行如下操作:
For a general InputStream, I would wrap it in a BufferedInputStream and do something like this:
您可能会发现 PushbackInputStream 很有用:
http://docs .oracle.com/javase/6/docs/api/java/io/PushbackInputStream.html
You might find PushbackInputStream to be useful:
http://docs.oracle.com/javase/6/docs/api/java/io/PushbackInputStream.html
使用 BufferedInputStream 时,请确保 inputStream 尚未缓冲,双缓冲将导致一些严重难以发现的错误。
此外,您还需要以不同的方式处理读取器,如果读取器是缓冲的,则转换为 StreamReader 和缓冲将导致字节丢失。
另外,如果您使用读取器,请记住您读取的不是字节,而是默认编码中的字符(除非设置了显式编码)。
您可能不知道的缓冲输入流的一个示例是 URL url; url.openStream();
我没有任何关于此信息的参考,它来自调试代码。
对我来说,出现问题的主要情况是从文件读取到压缩流的代码。
如果我没记错的话,一旦你开始调试代码,Java 源代码中就会有一些注释,指出某些事情并不总是正常工作。
我不记得使用 BufferedReader 和 BufferedInputStream 的信息在哪里
来自但我认为即使是最简单的测试也会立即失败。
请记住,要测试这一点,您需要标记超过缓冲区大小(这对于 BufferedReader 与 BufferedInputStream 来说是不同的),当读取的字节到达缓冲区末尾时就会出现问题。
请注意,源代码缓冲区大小可能与您在构造函数中设置的缓冲区大小不同。
我已经有一段时间没有这样做了,所以我对细节的记忆可能有点模糊。
使用 FilterReader/FilterInputStream 进行测试,将一个添加到直接流,将一个添加到缓冲流以查看差异。
When using a BufferedInputStream make sure that the inputStream is not already buffered, double buffering will cause some seriously hard to find bugs.
Also you need to handle Readers differently, converting to a StreamReader and Buffering will cause bytes to be lost if the Reader is Buffered.
Also if you are using a Reader remember that you are not reading bytes but characters in the default encoding (unless an explicit encoding was set).
An example of a buffered input stream, that you may not know is URL url; url.openStream();
I do not have any references for this information, it comes from debugging code.
The main case where the issue occurred for me was in code that read from a file into a compressed stream.
If I remember correctly once you start debugging through the code there are comments in the Java source that certain things do not work correctly always.
I do not remember where the information from using BufferedReader and BufferedInputStream
comes from but I think that fails straight away on even the simplest test.
Remember to test this you need to be marking more than the buffer size (which is different for BufferedReader versus BufferedInputStream), the problems occur when the bytes being read reach the end of the buffer.
Note there is a source code buffer size which can be different to the buffer size you set in the constructor.
It is a while since I did this so my recollections of details may be a little off.
Testing was done using a FilterReader/FilterInputStream, add one to the direct stream and one to the buffered stream to see the difference.
我在这里找到了 PeekableInputStream 的实现:
http://www.heatonresearch.com/articles /147/page2.html
本文中所示的实现的想法是,它在内部保留一个“查看”值的数组。 当您调用 read 时,值首先从查看的数组返回,然后从输入流返回。 当您调用 peek 时,将读取值并将其存储在“peeked”数组中。
由于示例代码的许可证是LGPL,因此可以将其附加到此帖子中:
I found an implementation of a PeekableInputStream here:
http://www.heatonresearch.com/articles/147/page2.html
The idea of the implementation shown in the article is that it keeps an array of "peeked" values internally. When you call read, the values are returned first from the peeked array, then from the input stream. When you call peek, the values are read and stored in the "peeked" array.
As the license of the sample code is LGPL, It can be attached to this post: