简体   繁体   English

为什么InputStream.seek导致乱码内容?

[英]Why is InputStream.seek causing garbled content?

I have to read a resource partially, then later on (at a very different point in time), I need to skip the number of bytes I'd initially read. 我必须部分地读取资源,然后稍后(在非常不同的时间点),我需要跳过我最初读取的字节数。 I need to glue the two parts I've read. 我需要粘上我读过的两个部分。

This is a very simplistic illustration of my code. 这是我的代码的一个非常简单的说明。 I am using separate ByteArrayInputStream -s because, as mentioned above, the invocations will be totally unrelated to each other (almost certainly, not using the same InputStream ). 我正在使用单独的ByteArrayInputStream -s,因为如上所述,调用将彼此完全无关(几乎可以肯定,不使用相同的InputStream )。

I am not quite sure what's going wrong here. 我不太清楚这里出了什么问题。 Instead of getting a concatenated String with a value of This is a big fat super long text has no meaning, but is good for the test. 而不是获得带有值的串联String This is a big fat super long text has no meaning, but is good for the test. , I am getting: 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 foThis 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.");
}

Could somebody please point out what's wrong and how to fix it? 有人可以指出什么是错的以及如何解决它? I don't quite understand why after skipping the number of bytes during the second read, the end of the read data gets messed up. 我不太明白为什么在第二次读取过程中跳过字节数后,读取数据的结尾会搞砸。 Please, advise! 请指教!

In your second read loop, you are ignoring the number of bytes read, so the bytes already in the buffer are being appended to the output stream. 在第二个读取循环中,您忽略了读取的字节数,因此缓冲区中已有的字节将附加到输出流中。

Try changing it to this: 尝试将其更改为:

  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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM