如何查看InputStream中的前两个字节? [英] How do I peek at the first two bytes in an InputStream?
本文介绍了如何查看InputStream中的前两个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
应该很简单:我有一个InputStream,我想偷看(不读取)前两个字节,即我希望在偷看之后,InputStream的当前位置为0。最好和最安全的方法是什么?
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?
答案 - 正如我所怀疑的,解决方案是将其包装在BufferedInputStream中提供可标记性。谢谢Rasmus。
Answer - As I had suspected, the solution was to wrap it in a BufferedInputStream which offers markability. Thanks Rasmus.
推荐答案
对于一般的InputStream,我会将它包装在BufferedInputStream中,并执行以下操作:
For a general InputStream, I would wrap it in a BufferedInputStream and do something like this:
BufferedInputStream bis = new BufferedInputStream(inputStream);
bis.mark(2);
int byte1 = bis.read();
int byte2 = bis.read();
bis.reset();
// note: you must continue using the BufferedInputStream instead of the inputStream
这篇关于如何查看InputStream中的前两个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文