From 466c2b2dece9db740e78f15342daedda398e695f Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sat, 8 Mar 2025 21:53:39 -0500 Subject: [PATCH] fixed PSTFileContent.readCompletely() method for several read() calls case --- src/main/java/com/pff/PSTFileContent.java | 5 +- src/test/java/com/pff/PSTFileContentTest.java | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/pff/PSTFileContentTest.java diff --git a/src/main/java/com/pff/PSTFileContent.java b/src/main/java/com/pff/PSTFileContent.java index e599d06..96a2cfd 100644 --- a/src/main/java/com/pff/PSTFileContent.java +++ b/src/main/java/com/pff/PSTFileContent.java @@ -25,8 +25,9 @@ public final void readCompletely(final byte[] target) throws IOException { if (read <= 0) { break; } - System.arraycopy(buffer, 0, target, offset, read); - offset += read; + int length = Math.min(read, target.length - offset); + System.arraycopy(buffer, 0, target, offset, length); + offset += length; } } diff --git a/src/test/java/com/pff/PSTFileContentTest.java b/src/test/java/com/pff/PSTFileContentTest.java new file mode 100644 index 0000000..824c120 --- /dev/null +++ b/src/test/java/com/pff/PSTFileContentTest.java @@ -0,0 +1,54 @@ +package com.pff; + + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.IOException; + +@RunWith(JUnit4.class) +public class PSTFileContentTest { + static class TestPSTFileContent extends PSTFileContent { + int position = 0; + + @Override + public void seek(long index) throws IOException { + position = (int) index; + } + + @Override + public long getFilePointer() throws IOException { + return position; + } + + @Override + public int read() throws IOException { + return 0; + } + + @Override + public int read(byte[] target) throws IOException { + int read = 3; + position += read; + return read; + } + + @Override + public byte readByte() throws IOException { + return 0; + } + + @Override + public void close() throws IOException { + + } + } + + @Test + public void testReadCompletely() throws IOException { + PSTFileContent pstFileContent = new TestPSTFileContent(); + + pstFileContent.readCompletely(new byte[10]); + } +} \ No newline at end of file