我想使用 FilterInputStream 类的 Skip 方法,该方法应该跳过流中所需的字节数。 但是skip方法的文档说 http://docs.oracle.com/javase/1.4.2/docs/api/java/io/FilterInputStream.html#skip(long)
Skips over and discards n bytes of data from the input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. The actual number of bytes skipped is returned.
使用skip方法来寻找特定位置真的安全吗?
最佳答案
我还发现这个 API 有点问题。它给具体实现留下了太多的自由:
1) 目前尚不清楚流结束时会发生什么
2)尚不清楚当流非空时,重复调用skip是否最终返回正数。
假设您知道 n 没有超出流末尾,并且假设 (2) 成立,那么以下代码可能会有所帮助:
public static void skipSecure(InputStream is, long n) throws IOException {
while (n >= 0) {
long skipped = is.skip(n);
n -= skipped;
}
}
关于java - 使用FilterInputStream的skip方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14934281/