Author: msahyoun
Date: Sun Apr 12 18:06:32 2026
New Revision: 1932993

Log:
PDFBOX-6166: fixes after code review; adjust unit test; parts by Claude Sonnet

Modified:
   
pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/NonSeekableRandomAccessReadInputStream.java
   
pdfbox/trunk/io/src/test/java/org/apache/pdfbox/io/NonSeekableRandomAccessReadInputStreamTest.java

Modified: 
pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/NonSeekableRandomAccessReadInputStream.java
==============================================================================
--- 
pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/NonSeekableRandomAccessReadInputStream.java
      Sun Apr 12 11:23:26 2026        (r1932992)
+++ 
pdfbox/trunk/io/src/main/java/org/apache/pdfbox/io/NonSeekableRandomAccessReadInputStream.java
      Sun Apr 12 18:06:32 2026        (r1932993)
@@ -95,9 +95,16 @@ public class NonSeekableRandomAccessRead
     @Override
     public void skip(int length) throws IOException
     {
-        for (int i=0; i< length;i++)
+        byte[] skipBuffer = new byte[Math.min(length, BUFFER_SIZE)];
+        int remaining = length;
+        while (remaining > 0)
         {
-            read();
+            int bytesRead = read(skipBuffer, 0, Math.min(remaining, 
skipBuffer.length));
+            if (bytesRead == -1)
+            {
+                break;
+            }
+            remaining -= bytesRead;
         }
     }
 
@@ -138,6 +145,20 @@ public class NonSeekableRandomAccessRead
     public int read(byte[] b, int offset, int length) throws IOException
     {
         checkClosed();
+        // Parameter validation as defined in InputStream.read(byte[], int, 
int)
+        if (b == null)
+        {
+            throw new NullPointerException("buffer is null");
+        }
+        if (offset < 0 || length < 0 || offset + length > b.length)
+        {
+            throw new IndexOutOfBoundsException("buffer length=" + b.length + 
" offset=" + offset
+                    + " length=" + length);
+        }
+        if (length == 0)
+        {
+            return 0;
+        }
         if (isEOF())
         {
             return -1;
@@ -162,7 +183,7 @@ public class NonSeekableRandomAccessRead
                 break;
             }
         }
-        return numberOfBytesRead;
+        return numberOfBytesRead > 0 ? numberOfBytesRead : -1;
     }
 
     @Override
@@ -233,7 +254,7 @@ public class NonSeekableRandomAccessRead
         catch (IOException exception)
         {
             // some data could be read -> don't throw an exception
-            LOG.warn("FlateFilter: premature end of stream due to a 
DataFormatException");
+            LOG.warn("premature end of stream, some data could be read ", 
exception);
             isEOF = true;
             throw exception;
         }
@@ -248,7 +269,8 @@ public class NonSeekableRandomAccessRead
     public int available() throws IOException
     {
         checkClosed();
-        return is.available();
+        int buffered = Math.max(0, bufferBytes[CURRENT] - 
currentBufferPointer);
+        return buffered + is.available();
     }
 
     /**
@@ -258,7 +280,7 @@ public class NonSeekableRandomAccessRead
     public long length() throws IOException
     {
         checkClosed();
-        return size;
+        return size + is.available();
     }
 
     @Override
@@ -269,8 +291,9 @@ public class NonSeekableRandomAccessRead
         {
             currentBufferPointer -= bytes;
             position -= bytes;
+            isEOF = false;
         }
-        else if (bufferBytes[LAST] > 0)
+        else if (bufferBytes[LAST] > 0 && (bytes - currentBufferPointer) <= 
bufferBytes[LAST])
         {
             // there is a former buffer
             int remainingBytesToRewind = bytes - currentBufferPointer;

Modified: 
pdfbox/trunk/io/src/test/java/org/apache/pdfbox/io/NonSeekableRandomAccessReadInputStreamTest.java
==============================================================================
--- 
pdfbox/trunk/io/src/test/java/org/apache/pdfbox/io/NonSeekableRandomAccessReadInputStreamTest.java
  Sun Apr 12 11:23:26 2026        (r1932992)
+++ 
pdfbox/trunk/io/src/test/java/org/apache/pdfbox/io/NonSeekableRandomAccessReadInputStreamTest.java
  Sun Apr 12 18:06:32 2026        (r1932993)
@@ -255,7 +255,7 @@ class NonSeekableRandomAccessReadInputSt
         ByteArrayInputStream bais = new ByteArrayInputStream(ba);
         try (RandomAccessRead rar = new 
NonSeekableRandomAccessReadInputStream(bais))
         {
-            assertEquals(0, rar.length()); // not really what I'd expect...
+            assertEquals(4096 * 2, rar.length());
             int len = rar.read(new byte[4096 + 1]);
             assertEquals(4096 * 2, rar.length());
             assertEquals(4096 + 1, len);

Reply via email to