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 2022/08/02 05:54:19 UTC

svn commit: r1903188 - in /pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples: lucene/LucenePDFDocument.java printing/OpaquePDFRenderer.java

Author: lehmi
Date: Tue Aug  2 05:54:19 2022
New Revision: 1903188

URL: http://svn.apache.org/viewvc?rev=1903188&view=rev
Log:
PDFBOX-5483: replace InputStream with RandomAccessRead

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/lucene/LucenePDFDocument.java
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/OpaquePDFRenderer.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/lucene/LucenePDFDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/lucene/LucenePDFDocument.java?rev=1903188&r1=1903187&r2=1903188&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/lucene/LucenePDFDocument.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/lucene/LucenePDFDocument.java Tue Aug  2 05:54:19 2022
@@ -17,9 +17,7 @@
 package org.apache.pdfbox.examples.lucene;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
@@ -36,6 +34,9 @@ import org.apache.lucene.document.String
 import org.apache.lucene.document.TextField;
 import org.apache.lucene.index.IndexOptions;
 import org.apache.pdfbox.Loader;
+import org.apache.pdfbox.io.RandomAccessRead;
+import org.apache.pdfbox.io.RandomAccessReadBuffer;
+import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDDocumentInformation;
 import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
@@ -207,21 +208,6 @@ public class LucenePDFDocument
     }
 
     /**
-     * Convert the PDF stream to a lucene document.
-     * 
-     * @param is The input stream.
-     * @return The input stream converted to a lucene document.
-     * @throws IOException If there is an error converting the PDF.
-     */
-    public Document convertDocument(InputStream is) throws IOException
-    {
-        Document document = new Document();
-        addContent(document, is, "<inputstream>");
-        return document;
-
-    }
-
-    /**
      * This will take a reference to a PDF document and create a lucene document.
      * 
      * @param file A reference to a PDF document.
@@ -250,13 +236,8 @@ public class LucenePDFDocument
         // tokenized prior to indexing.
         addUnstoredKeywordField(document, "uid", uid);
 
-        try (FileInputStream input = new FileInputStream(file))
-        {
-            addContent(document, input, file.getPath());
-        }
-
+        addContent(document, new RandomAccessReadBufferedFile(file), file.getPath());
         // return the document
-
         return document;
     }
 
@@ -288,10 +269,9 @@ public class LucenePDFDocument
         // tokenized prior to indexing.
         addUnstoredKeywordField(document, "uid", uid);
 
-        try (InputStream input = connection.getInputStream())
-        {
-            addContent(document, input, url.toExternalForm());
-        }
+        addContent(document,
+                RandomAccessReadBuffer.createBufferFromStream(connection.getInputStream()),
+                    url.toExternalForm());
 
         // return the document
         return document;
@@ -300,21 +280,6 @@ public class LucenePDFDocument
     /**
      * This will get a lucene document from a PDF file.
      * 
-     * @param is The stream to read the PDF from.
-     * 
-     * @return The lucene document.
-     * 
-     * @throws IOException If there is an error parsing or indexing the document.
-     */
-    public static Document getDocument(InputStream is) throws IOException
-    {
-        LucenePDFDocument converter = new LucenePDFDocument();
-        return converter.convertDocument(is);
-    }
-
-    /**
-     * This will get a lucene document from a PDF file.
-     * 
      * @param file The file to get the document for.
      * 
      * @return The lucene document.
@@ -346,14 +311,15 @@ public class LucenePDFDocument
      * This will add the contents to the lucene document.
      * 
      * @param document The document to add the contents to.
-     * @param is The stream to get the contents from.
+     * @param source The source to get the content from.
      * @param documentLocation The location of the document, used just for debug messages.
      * 
      * @throws IOException If there is an error parsing the document.
      */
-    private void addContent(Document document, InputStream is, String documentLocation) throws IOException
+    private void addContent(Document document, RandomAccessRead source, String documentLocation)
+            throws IOException
     {
-        try (PDDocument pdfDocument = Loader.loadPDF(is))
+        try (PDDocument pdfDocument = Loader.loadPDF(source))
         {
             // create a writer where to append the text content.
             StringWriter writer = new StringWriter();

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/OpaquePDFRenderer.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/OpaquePDFRenderer.java?rev=1903188&r1=1903187&r2=1903188&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/OpaquePDFRenderer.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/OpaquePDFRenderer.java Tue Aug  2 05:54:19 2022
@@ -20,7 +20,6 @@ import java.awt.print.Printable;
 import java.awt.print.PrinterException;
 import java.awt.print.PrinterJob;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.URL;
 import java.util.List;
 import javax.print.PrintServiceLookup;
@@ -33,6 +32,7 @@ import org.apache.pdfbox.contentstream.o
 import org.apache.pdfbox.contentstream.operator.graphics.GraphicsOperatorProcessor;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.io.RandomAccessReadBuffer;
 import org.apache.pdfbox.pdmodel.MissingResourceException;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.graphics.PDXObject;
@@ -62,8 +62,9 @@ public class OpaquePDFRenderer extends P
     public static void main(String[] args) throws IOException, PrinterException
     {
         // PDF from the QZ Tray project, who reported this problem.
-        try (InputStream is = new URL("https://github.com/qzind/tray/files/1749977/test.pdf").openStream();
-             PDDocument doc = Loader.loadPDF(is))
+        try (PDDocument doc = Loader.loadPDF(RandomAccessReadBuffer.createBufferFromStream(
+                        new URL("https://github.com/qzind/tray/files/1749977/test.pdf")
+                                .openStream())))
         {
             PDFRenderer renderer = new OpaquePDFRenderer(doc);
             Printable printable = new PDFPrintable(doc, Scaling.SCALE_TO_FIT, false, 0, true, renderer);