fury icon indicating copy to clipboard operation
fury copied to clipboard

[java] ForyInputStream当读取已经是EOF的流有问题

Open wuwangben opened this issue 2 months ago • 2 comments

Search before asking

  • [x] I had searched in the issues and found no similar issues.

Version

0.12.2

Component(s)

Java

Minimal reproduce step

当读取一个已经是EOF的流时返回非期望异常

What did you expect to see?

期望看到一致的异常new IndexOutOfBoundsException("No enough data in the stream " + stream); 更合理似乎应该是EOFException("No enough data in the stream " + stream);

What did you see instead?

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1

Anything Else?

Image Image 可以这样修改吗?

Are you willing to submit a PR?

  • [ ] I'm willing to submit a PR!

wuwangben avatar Oct 15 '25 06:10 wuwangben

@wuwangben 你好,没太理解你这里的改动,感觉跟之前的行为好像没有区别,方便提交一个带test的PR吗

chaokunyang avatar Oct 20 '25 11:10 chaokunyang

package org.apache.fory.io;

import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

import static org.testng.Assert.assertEquals;

public class ForyInputStreamTest {

  @Test
  public void testFillBufferIndexOutOfBoundsException() throws IOException {
    try (ForyInputStream in = new ForyInputStream(new ByteArrayInputStream(new byte[0])) ) {
      try {
        in.fillBuffer(1);
        Assert.fail("Expected IndexOutOfBoundsException to be thrown");
      } catch (IndexOutOfBoundsException e) {
        Assert.assertTrue(e.getMessage().contains("No enough data in the stream"));
      }
    }
  }

  @Test
  public void testReadToIndexOutOfBoundsException() throws IOException {
    try (ForyInputStream in = new ForyInputStream(new ByteArrayInputStream(new byte[0])) ) {
      try {
        in.readTo(new byte[10], 0, 10);
        Assert.fail("Expected IndexOutOfBoundsException to be thrown");
      } catch (IndexOutOfBoundsException e) {
        Assert.assertTrue(e.getMessage().contains("No enough data in the stream"));
      }
    }
  }
}

wuwangben avatar Oct 23 '25 08:10 wuwangben