InputStream不会重置为开头 [英] InputStream will not reset to beginning

查看:189
本文介绍了InputStream不会重置为开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

InputStream data = realResponse.getEntity().getContent();
byte[] preview = new byte[100];
data.read(preview, 0, 100);

//现在我想稍后引用InputStream,但我想从头开始流,而不是100字节。我尝试 mark()它在100,然后 reset()在我之后读取前100个字节,但这也不起作用。

// Now I want to refer to the InputStream later on, but I want it from the beginning of the stream, not 100 bytes in. I tried mark() it at 100, and then reset() after I read the first 100 bytes, but that doesn't work either.

任何想法?可能是一个愚蠢的错误..只是没有看到它。

Any ideas? Probably a stupid mistake..just not seeing it.

推荐答案

当你使用 mark() java.io.InputStream 对象的code>你如果您的InputStream实际上支持使用mark,则应使用 markSupported()方法进行检查。根据API, InputStream 类不会,但java.io.BufferedInputStream 类。也许你应该将你的流嵌入一个 BufferedInputStream 对象,如:

When you use mark() of the java.io.InputStream object you should check with the markSupported() method if your InputStream actually support using mark. According to the API the InputStream class doesn't, but the java.io.BufferedInputStream class does. Maybe you should embed your stream inside a BufferedInputStream object like:

InputStream data = new BufferedInputStream(realResponse.getEntity().getContent());
// data.markSupported() should return "true" now
data.mark(some_size);
// work with "data" now
...
data.reset();

这篇关于InputStream不会重置为开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆