From 60e585abf8793aa8f0b790b3e1a060fa45d409a9 Mon Sep 17 00:00:00 2001 From: muzhiyuan10607 <739061411@qq.com> Date: Thu, 23 Oct 2025 16:36:23 +0800 Subject: [PATCH 1/3] =?UTF-8?q?bugfix-2772=EF=BC=9A=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=AF=BB=E5=8F=96EOF=E6=B5=81=E5=BC=82=E5=B8=B8=E4=B8=8D?= =?UTF-8?q?=E4=B8=80=E8=87=B4=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/apache/fory/io/ForyInputStream.java | 14 ++--- .../apache/fory/io/ForyInputStreamTest.java | 53 +++++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java diff --git a/java/fory-core/src/main/java/org/apache/fory/io/ForyInputStream.java b/java/fory-core/src/main/java/org/apache/fory/io/ForyInputStream.java index 93dbd77b7e..301b81644e 100644 --- a/java/fory-core/src/main/java/org/apache/fory/io/ForyInputStream.java +++ b/java/fory-core/src/main/java/org/apache/fory/io/ForyInputStream.java @@ -57,16 +57,16 @@ public int fillBuffer(int minFillSize) { heapMemory = growBuffer(minFillSize, buffer); } try { - int read; + int read = 0; int len = heapMemory.length - offset; - read = stream.read(heapMemory, offset, len); - while (read < minFillSize) { + do { int newRead = stream.read(heapMemory, offset + read, len - read); if (newRead < 0) { throw new IndexOutOfBoundsException("No enough data in the stream " + stream); } read += newRead; - } + } while (read < minFillSize); + buffer.increaseSize(read); return read; } catch (IOException e) { @@ -101,14 +101,14 @@ public void readTo(byte[] dst, int dstIndex, int len) { len -= remaining; dstIndex += remaining; try { - int read = stream.read(dst, dstIndex, len); - while (read < len) { + int read = 0; + do { int newRead = stream.read(dst, dstIndex + read, len - read); if (newRead < 0) { throw new IndexOutOfBoundsException("No enough data in the stream " + stream); } read += newRead; - } + } while (read < len); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java b/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java new file mode 100644 index 0000000000..26a19989a6 --- /dev/null +++ b/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fory.io; + +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +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")); + } + } + } +} From 986ad6657162d3748e4332dfd2c8c30a90de9e7c Mon Sep 17 00:00:00 2001 From: muzhiyuan10607 <739061411@qq.com> Date: Thu, 23 Oct 2025 17:28:56 +0800 Subject: [PATCH 2/3] =?UTF-8?q?bugfix-2772=EF=BC=9A=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test/java/org/apache/fory/io/ForyInputStreamTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java b/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java index 26a19989a6..a63fe12ca4 100644 --- a/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java +++ b/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java @@ -29,7 +29,7 @@ public class ForyInputStreamTest { @Test public void testFillBufferIndexOutOfBoundsException() throws IOException { - try (ForyInputStream in = new ForyInputStream(new ByteArrayInputStream(new byte[0])) ) { + try (ForyInputStream in = new ForyInputStream(new ByteArrayInputStream(new byte[0]))) { try { in.fillBuffer(1); Assert.fail("Expected IndexOutOfBoundsException to be thrown"); @@ -41,7 +41,7 @@ public void testFillBufferIndexOutOfBoundsException() throws IOException { @Test public void testReadToIndexOutOfBoundsException() throws IOException { - try (ForyInputStream in = new ForyInputStream(new ByteArrayInputStream(new byte[0])) ) { + 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"); From 995d6233289eeb2a72b1ad56601dc5acd9cc25a9 Mon Sep 17 00:00:00 2001 From: muzhiyuan10607 <739061411@qq.com> Date: Thu, 23 Oct 2025 17:36:36 +0800 Subject: [PATCH 3/3] =?UTF-8?q?bugfix-2772=EF=BC=9AAdjust=20import=20order?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/org/apache/fory/io/ForyInputStreamTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java b/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java index a63fe12ca4..1a1bc17139 100644 --- a/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java +++ b/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java @@ -19,11 +19,10 @@ package org.apache.fory.io; -import org.testng.Assert; -import org.testng.annotations.Test; - import java.io.ByteArrayInputStream; import java.io.IOException; +import org.testng.Assert; +import org.testng.annotations.Test; public class ForyInputStreamTest {