You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2021/10/02 11:33:47 UTC

svn commit: r1893808 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFile.java

Author: tilman
Date: Sat Oct  2 11:33:46 2021
New Revision: 1893808

URL: http://svn.apache.org/viewvc?rev=1893808&view=rev
Log:
PDFBOX-4892: optimize, as suggested by valerybokov

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFile.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFile.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFile.java?rev=1893808&r1=1893807&r2=1893808&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFile.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFile.java Sat Oct  2 11:33:46 2021
@@ -39,10 +39,10 @@ public class RandomAccessReadMemoryMappe
     private final long size;
 
     // file channel of the file to be read
-    private FileChannel fileChannel;
+    private final FileChannel fileChannel;
 
     // function to unmap the byte buffer
-    private Consumer<? super ByteBuffer> unmapper = IOUtils::unmap;
+    private final Consumer<? super ByteBuffer> unmapper;
 
     /**
      * Default constructor.
@@ -68,6 +68,7 @@ public class RandomAccessReadMemoryMappe
         }
         // map the whole file to memory
         mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
+        unmapper = IOUtils::unmap;
     }
 
     private RandomAccessReadMemoryMappedFile(RandomAccessReadMemoryMappedFile parent)
@@ -77,6 +78,7 @@ public class RandomAccessReadMemoryMappe
         mappedByteBuffer.rewind();
         // unmap doesn't work on duplicate, see Unsafe#invokeCleaner
         unmapper = null;
+        fileChannel = null;
     }
 
     /**
@@ -143,8 +145,9 @@ public class RandomAccessReadMemoryMappe
             return -1;
         }
         int remainingBytes = (int)size - mappedByteBuffer.position();
-        mappedByteBuffer.get(b, offset, Math.min(remainingBytes, length));
-        return Math.min(remainingBytes, length);
+        remainingBytes = Math.min(remainingBytes, length);
+        mappedByteBuffer.get(b, offset, remainingBytes);
+        return remainingBytes;
     }
 
     /**