[英]Why is InputStream.seek causing garbled content?
我必须部分地读取资源,然后稍后(在非常不同的时间点),我需要跳过我最初读取的字节数。 我需要粘上我读过的两个部分。
这是我的代码的一个非常简单的说明。 我正在使用单独的ByteArrayInputStream
-s,因为如上所述,调用将彼此完全无关(几乎可以肯定,不使用相同的InputStream
)。
我不太清楚这里出了什么问题。 而不是获得带有值的串联String
This is a big fat super long text has no meaning, but is good for the test.
,我得到了: This is a big fat super long text has no meaning, but is good for the test.aning, but is good fo
。 This is a big fat super long text has no meaning, but is good for the test.aning, but is good fo
。
public void testFoo()
{
String s = "This is a big fat super long text has no meaning, but is good for the test.";
ByteArrayInputStream bais1 = new ByteArrayInputStream(s.getBytes());
ByteArrayInputStream bais2 = new ByteArrayInputStream(s.getBytes());
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
int size = 32;
byte[] bytes = new byte[size];
int total = 0;
int len;
while ((len = bais1.read(bytes, 0, size)) != -1)
{
baos1.write(bytes);
baos1.flush();
total += len;
if (total >= size)
{
// This is here just to illustrate that
// we are reading a few bytes, and then
// just terminating before the rest of
// the stream has been read.
break;
}
}
bytes = new byte[size];
bais1.close();
System.out.println("Read " + total + " bytes.");
// Here we are supposed to skip the number
// of bytes that have already been read:
bais2.skip(total);
System.out.println("Skipped " + total + "/" + s.getBytes().length + " bytes.");
while ((len = bais2.read(bytes, 0, size)) != -1)
{
baos2.write(bytes);
baos2.flush();
total += len;
}
System.out.println("Original: " + s);
System.out.println("Partial read1: " + new String(baos1.toByteArray()));
System.out.println("Partial read2: " + new String(baos2.toByteArray()));
System.out.println("Read " + total + " bytes.");
}
有人可以指出什么是错的以及如何解决它? 我不太明白为什么在第二次读取过程中跳过字节数后,读取数据的结尾会搞砸。 请指教!
在第二个读取循环中,您忽略了读取的字节数,因此缓冲区中已有的字节将附加到输出流中。
尝试将其更改为:
System.out.println("Skipped " + total + "/" + s.getBytes().length + " bytes.");
while ((len = bais2.read(bytes, 0, size)) != -1)
{
baos2.write(bytes, 0, len); // <- added write offset and length
baos2.flush();
total += len;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.