You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2021/02/25 07:16:09 UTC

svn commit: r1886911 - in /pdfbox/branches/2.0/pdfbox/src: main/java/org/apache/pdfbox/io/ main/java/org/apache/pdfbox/pdfparser/ test/java/org/apache/pdfbox/pdfparser/

Author: lehmi
Date: Thu Feb 25 07:16:09 2021
New Revision: 1886911

URL: http://svn.apache.org/viewvc?rev=1886911&view=rev
Log:
PDFBOX-4892: sync read/readFully code to get the same behaviour for all classes implementing the same interface

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBufferedFileInputStream.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFileBuffer.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/InputStreamSource.java
    pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/InputStreamSourceTest.java
    pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/RandomAccessSourceTest.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java?rev=1886911&r1=1886910&r2=1886911&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java Thu Feb 25 07:16:09 2021
@@ -16,6 +16,7 @@
  */
 package org.apache.pdfbox.io;
 
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -243,7 +244,7 @@ public class RandomAccessBuffer implemen
         checkClosed();
         if (pointer >= size)
         {
-            return 0;
+            return -1;
         }
         int bytesRead = readRemainingBytes(b, offset, length);
         while (bytesRead < length && available() > 0)
@@ -259,10 +260,6 @@ public class RandomAccessBuffer implemen
 
     private int readRemainingBytes(byte[] b, int offset, int length)
     {
-        if (pointer >= size)
-        {
-            return 0;
-        }
         int maxLength = (int) Math.min(length, size-pointer);
         int remainingBytes = chunkSize - currentBufferPointer;
         // no more bytes left
@@ -500,13 +497,18 @@ public class RandomAccessBuffer implemen
     @Override
     public byte[] readFully(int length) throws IOException
     {
-        byte[] b = new byte[length];
-        int bytesRead = read(b);
-        while (bytesRead < length)
+        byte[] bytes = new byte[length];
+        int bytesRead = 0;
+        do
         {
-            bytesRead += read(b, bytesRead, length - bytesRead);
-        }
-        return b;
+            int count = read(bytes, bytesRead, length - bytesRead);
+            if (count < 0)
+            {
+                throw new EOFException();
+            }
+            bytesRead += count;
+        } while (bytesRead < length);
+        return bytes;
     }
 
     /**

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBufferedFileInputStream.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBufferedFileInputStream.java?rev=1886911&r1=1886910&r2=1886911&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBufferedFileInputStream.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBufferedFileInputStream.java Thu Feb 25 07:16:09 2021
@@ -16,6 +16,7 @@
  */
 package org.apache.pdfbox.io;
 
+import java.io.EOFException;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -336,13 +337,18 @@ extends InputStream implements RandomAcc
     @Override
     public byte[] readFully(int length) throws IOException
     {
-        byte[] b = new byte[length];
-        int bytesRead = read(b);
-        while(bytesRead < length)
+        byte[] bytes = new byte[length];
+        int bytesRead = 0;
+        do
         {
-            bytesRead += read(b, bytesRead, length-bytesRead);
-        }
-        return b;
+            int count = read(bytes, bytesRead, length - bytesRead);
+            if (count < 0)
+            {
+                throw new EOFException();
+            }
+            bytesRead += count;
+        } while (bytesRead < length);
+        return bytes;
     }
 
     @Override

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFileBuffer.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFileBuffer.java?rev=1886911&r1=1886910&r2=1886911&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFileBuffer.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFileBuffer.java Thu Feb 25 07:16:09 2021
@@ -372,22 +372,20 @@ class ScratchFileBuffer implements Rando
      * {@inheritDoc}
      */
     @Override
-    public byte[] readFully(int len) throws IOException
+    public byte[] readFully(int length) throws IOException
     {
-        byte[] b = new byte[len];
-
-        int n = 0;
+        byte[] bytes = new byte[length];
+        int bytesRead = 0;
         do
         {
-            int count = read(b, n, len - n);
+            int count = read(bytes, bytesRead, length - bytesRead);
             if (count < 0)
             {
                 throw new EOFException();
             }
-            n += count;
-        } while (n < len);
-
-        return b;
+            bytesRead += count;
+        } while (bytesRead < length);
+        return bytes;
     }
 
     /**

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/InputStreamSource.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/InputStreamSource.java?rev=1886911&r1=1886910&r2=1886911&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/InputStreamSource.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/InputStreamSource.java Thu Feb 25 07:16:09 2021
@@ -17,6 +17,7 @@
 
 package org.apache.pdfbox.pdfparser;
 
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PushbackInputStream;
@@ -120,21 +121,16 @@ final class InputStreamSource implements
     public byte[] readFully(int length) throws IOException
     {
         byte[] bytes = new byte[length];
-        int off = 0;
-        int len = length;
-        while (len > 0)
+        int bytesRead = 0;
+        do
         {
-            int n = this.read(bytes, off, len);
-            if (n > 0)
+            int count = read(bytes, bytesRead, length - bytesRead);
+            if (count < 0)
             {
-                off += n;
-                len -= n;
+                throw new EOFException();
             }
-            else
-            {
-                break;
-            }
-        }
+            bytesRead += count;
+        } while (bytesRead < length);
         return bytes;
     }
 

Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/InputStreamSourceTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/InputStreamSourceTest.java?rev=1886911&r1=1886910&r2=1886911&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/InputStreamSourceTest.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/InputStreamSourceTest.java Thu Feb 25 07:16:09 2021
@@ -17,6 +17,7 @@
 package org.apache.pdfbox.pdfparser;
 
 import java.io.ByteArrayInputStream;
+import java.io.EOFException;
 import java.io.IOException;
 import org.junit.Assert;
 import org.junit.Test;
@@ -38,6 +39,16 @@ public class InputStreamSourceTest
         inputStreamSource.readFully(5);
         Assert.assertEquals(5, inputStreamSource.getPosition());
 
+        try
+        {
+            inputStreamSource.readFully(10);
+            Assert.fail("readFully beyond EOF should have triggered an EOFException");
+        }
+        catch(EOFException exception)
+        {
+
+        }
+
         inputStreamSource.close();
     }
 

Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/RandomAccessSourceTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/RandomAccessSourceTest.java?rev=1886911&r1=1886910&r2=1886911&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/RandomAccessSourceTest.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/RandomAccessSourceTest.java Thu Feb 25 07:16:09 2021
@@ -17,6 +17,7 @@
 package org.apache.pdfbox.pdfparser;
 
 import java.io.ByteArrayInputStream;
+import java.io.EOFException;
 import java.io.IOException;
 
 import org.apache.pdfbox.io.RandomAccessBuffer;
@@ -41,6 +42,16 @@ public class RandomAccessSourceTest
         randomAccessSource.readFully(5);
         Assert.assertEquals(5, randomAccessSource.getPosition());
 
+        try
+        {
+            randomAccessSource.readFully(10);
+            Assert.fail("readFully beyond EOF should have triggered an EOFException");
+        }
+        catch (EOFException exception)
+        {
+
+        }
+
         randomAccessSource.close();
     }