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..1a1bc17139 --- /dev/null +++ b/java/fory-core/src/test/java/org/apache/fory/io/ForyInputStreamTest.java @@ -0,0 +1,52 @@ +/* + * 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 java.io.ByteArrayInputStream; +import java.io.IOException; +import org.testng.Assert; +import org.testng.annotations.Test; + +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")); + } + } + } +}