Author: lehmi
Date: Tue Nov 21 16:28:52 2023
New Revision: 1914016

URL: http://svn.apache.org/viewvc?rev=1914016&view=rev
Log:
PDFBOX-5711: add support for java.nio.file.Path

Modified:
    
pdfbox/branches/3.0/io/src/main/java/org/apache/pdfbox/io/RandomAccessReadBufferedFile.java
    
pdfbox/branches/3.0/io/src/main/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFile.java
    
pdfbox/branches/3.0/io/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferedFileTest.java
    
pdfbox/branches/3.0/io/src/test/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFileTest.java

Modified: 
pdfbox/branches/3.0/io/src/main/java/org/apache/pdfbox/io/RandomAccessReadBufferedFile.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/3.0/io/src/main/java/org/apache/pdfbox/io/RandomAccessReadBufferedFile.java?rev=1914016&r1=1914015&r2=1914016&view=diff
==============================================================================
--- 
pdfbox/branches/3.0/io/src/main/java/org/apache/pdfbox/io/RandomAccessReadBufferedFile.java
 (original)
+++ 
pdfbox/branches/3.0/io/src/main/java/org/apache/pdfbox/io/RandomAccessReadBufferedFile.java
 Tue Nov 21 16:28:52 2023
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
+import java.nio.file.Path;
 import java.nio.file.StandardOpenOption;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -70,7 +71,7 @@ public class RandomAccessReadBufferedFil
     private int offsetWithinPage = 0;
 
     private final FileChannel fileChannel;
-    private final File file;
+    private final Path path;
     private final long fileLength;
     private long fileOffset = 0;
     private boolean isClosed;
@@ -94,9 +95,20 @@ public class RandomAccessReadBufferedFil
      */
     public RandomAccessReadBufferedFile( File file ) throws IOException 
     {
-        this.file = file;
-        fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
-        fileLength = file.length();
+        this(file.toPath());
+    }
+
+    /**
+     * Create a random access buffered file instance using the given path.
+     *
+     * @param path path of the file to be read.
+     * @throws IOException if something went wrong while accessing the given 
file.
+     */
+    public RandomAccessReadBufferedFile(Path path) throws IOException
+    {
+        this.path = path;
+        fileChannel = FileChannel.open(path, StandardOpenOption.READ);
+        fileLength = fileChannel.size();
         seek(0);
     }
 
@@ -269,7 +281,7 @@ public class RandomAccessReadBufferedFil
         RandomAccessReadBufferedFile randomAccessReadBufferedFile = 
rafCopies.get(currentThreadID);
         if (randomAccessReadBufferedFile == null || 
randomAccessReadBufferedFile.isClosed())
         {
-            randomAccessReadBufferedFile = new 
RandomAccessReadBufferedFile(file);
+            randomAccessReadBufferedFile = new 
RandomAccessReadBufferedFile(path);
             rafCopies.put(currentThreadID, randomAccessReadBufferedFile);
         }
         return new RandomAccessReadView(randomAccessReadBufferedFile, 
startPosition, streamLength);

Modified: 
pdfbox/branches/3.0/io/src/main/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFile.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/3.0/io/src/main/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFile.java?rev=1914016&r1=1914015&r2=1914016&view=diff
==============================================================================
--- 
pdfbox/branches/3.0/io/src/main/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFile.java
 (original)
+++ 
pdfbox/branches/3.0/io/src/main/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFile.java
 Tue Nov 21 16:28:52 2023
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
+import java.nio.file.Path;
 import java.nio.file.StandardOpenOption;
 import java.util.EnumSet;
 import java.util.Optional;
@@ -45,7 +46,7 @@ public class RandomAccessReadMemoryMappe
     private final Consumer<? super ByteBuffer> unmapper;
 
     /**
-     * Default constructor.
+     * Create a random access memory mapped file instance for the file with 
the given name.
      * 
      * @param filename the filename of the file to be read
      * 
@@ -57,7 +58,7 @@ public class RandomAccessReadMemoryMappe
     }
 
     /**
-     * Default constructor.
+     * Create a random access memory mapped file instance for the given file.
      * 
      * @param file the file to be read
      * 
@@ -65,13 +66,24 @@ public class RandomAccessReadMemoryMappe
      */
     public RandomAccessReadMemoryMappedFile(File file) throws IOException
     {
-        fileChannel = FileChannel.open(file.toPath(), 
EnumSet.of(StandardOpenOption.READ));
+        this(file.toPath());
+    }
+
+    /**
+     * Create a random access memory mapped file instance using the given path.
+     * 
+     * @param path path of the file to be read.
+     * 
+     * @throws IOException If there is an IO error opening the file.
+     */
+    public RandomAccessReadMemoryMappedFile(Path path) throws IOException
+    {
+        fileChannel = FileChannel.open(path, 
EnumSet.of(StandardOpenOption.READ));
         size = fileChannel.size();
         // TODO only ints are allowed -> implement paging
         if (size > Integer.MAX_VALUE)
         {
-            throw new IOException(getClass().getName()
-                    + " doesn't yet support files bigger than "
+            throw new IOException(getClass().getName() + " doesn't yet support 
files bigger than "
                     + Integer.MAX_VALUE);
         }
         // map the whole file to memory

Modified: 
pdfbox/branches/3.0/io/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferedFileTest.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/3.0/io/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferedFileTest.java?rev=1914016&r1=1914015&r2=1914016&view=diff
==============================================================================
--- 
pdfbox/branches/3.0/io/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferedFileTest.java
 (original)
+++ 
pdfbox/branches/3.0/io/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferedFileTest.java
 Tue Nov 21 16:28:52 2023
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Asse
 import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
+import java.nio.file.Paths;
 
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
@@ -122,6 +123,16 @@ class RandomAccessReadBufferedFileTest
         }
     }
 
+    @Test
+    void testPathConstructor() throws IOException, URISyntaxException
+    {
+        try (RandomAccessRead randomAccessSource = new 
RandomAccessReadBufferedFile(
+                
Paths.get(getClass().getResource("RandomAccessReadFile1.txt").toURI())))
+        {
+            assertEquals(130, randomAccessSource.length());
+        }
+    }
+
     @Test
     void testPositionUnreadBytes() throws IOException, URISyntaxException
     {

Modified: 
pdfbox/branches/3.0/io/src/test/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFileTest.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/3.0/io/src/test/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFileTest.java?rev=1914016&r1=1914015&r2=1914016&view=diff
==============================================================================
--- 
pdfbox/branches/3.0/io/src/test/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFileTest.java
 (original)
+++ 
pdfbox/branches/3.0/io/src/test/java/org/apache/pdfbox/io/RandomAccessReadMemoryMappedFileTest.java
 Tue Nov 21 16:28:52 2023
@@ -26,6 +26,7 @@ import java.io.IOException;
 import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
 
 import org.junit.jupiter.api.Assertions;
@@ -49,6 +50,16 @@ class RandomAccessReadMemoryMappedFileTe
         }
     }
 
+    @Test
+    void testPathConstructor() throws IOException, URISyntaxException
+    {
+        try (RandomAccessRead randomAccessSource = new 
RandomAccessReadMemoryMappedFile(
+                
Paths.get(getClass().getResource("RandomAccessReadFile1.txt").toURI())))
+        {
+            assertEquals(130, randomAccessSource.length());
+        }
+    }
+
     @Test
     void testPositionRead() throws IOException, URISyntaxException
     {


Reply via email to