PDFBOX-4836: don't use ScratchFile within COSInputStream any more

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1881870 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pdfbox/src/main/java/org/apache/pdfbox/cos/COSInputStream.java b/pdfbox/src/main/java/org/apache/pdfbox/cos/COSInputStream.java
index f3f4a67..e4528b8 100644
--- a/pdfbox/src/main/java/org/apache/pdfbox/cos/COSInputStream.java
+++ b/pdfbox/src/main/java/org/apache/pdfbox/cos/COSInputStream.java
@@ -30,10 +30,6 @@
 import org.apache.pdfbox.filter.DecodeOptions;
 import org.apache.pdfbox.filter.DecodeResult;
 import org.apache.pdfbox.filter.Filter;
-import org.apache.pdfbox.io.RandomAccess;
-import org.apache.pdfbox.io.RandomAccessInputStream;
-import org.apache.pdfbox.io.RandomAccessOutputStream;
-import org.apache.pdfbox.io.ScratchFile;
 
 /**
  * An InputStream which reads from an encoded COS stream.
@@ -48,18 +44,27 @@
      * @param filters Filters to be applied.
      * @param parameters Filter parameters.
      * @param in Encoded input stream.
-     * @param scratchFile Scratch file to use, or null.
+     * @return Decoded stream.
+     * @throws IOException If the stream could not be read.
+     */
+    static COSInputStream create(List<Filter> filters, COSDictionary parameters, InputStream in)
+            throws IOException
+    {
+        return create(filters, parameters, in, DecodeOptions.DEFAULT);
+    }
+
+    /**
+     * Creates a new COSInputStream from an encoded input stream.
+     *
+     * @param filters Filters to be applied.
+     * @param parameters Filter parameters.
+     * @param in Encoded input stream.
+     * @param options decode options for the encoded stream
      * @return Decoded stream.
      * @throws IOException If the stream could not be read.
      */
     static COSInputStream create(List<Filter> filters, COSDictionary parameters, InputStream in,
-                                 ScratchFile scratchFile) throws IOException
-    {
-        return create(filters, parameters, in, scratchFile, DecodeOptions.DEFAULT);
-    }
-
-    static COSInputStream create(List<Filter> filters, COSDictionary parameters, InputStream in,
-                                 ScratchFile scratchFile, DecodeOptions options) throws IOException
+            DecodeOptions options) throws IOException
     {
         List<DecodeResult> results = new ArrayList<>();
         InputStream input = in;
@@ -70,32 +75,13 @@
             {
                 throw new IOException("Duplicate");
             }
+            ByteArrayOutputStream output = new ByteArrayOutputStream();
             // apply filters
             for (int i = 0; i < filters.size(); i++)
             {
-                if (scratchFile != null)
-                {
-                    // scratch file
-                    final RandomAccess buffer = scratchFile.createBuffer();
-                    DecodeResult result = filters.get(i).decode(input, new RandomAccessOutputStream(buffer), parameters, i, options);
-                    results.add(result);
-                    input = new RandomAccessInputStream(buffer)
-                    {
-                        @Override
-                        public void close() throws IOException
-                        {
-                            buffer.close();
-                        }
-                    };
-                }
-                else
-                {
-                    // in-memory
-                    ByteArrayOutputStream output = new ByteArrayOutputStream();
-                    DecodeResult result = filters.get(i).decode(input, output, parameters, i, options);
-                    results.add(result);
-                    input = new ByteArrayInputStream(output.toByteArray());
-                }
+                output.reset();
+                results.add(filters.get(i).decode(input, output, parameters, i, options));
+                input = new ByteArrayInputStream(output.toByteArray());
             }
         }
         return new COSInputStream(input, results);
diff --git a/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java b/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java
index 32dfb53..277cc68 100644
--- a/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java
+++ b/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java
@@ -221,7 +221,7 @@
     public COSInputStream createInputStream(DecodeOptions options) throws IOException
     {
         InputStream input = createRawInputStream();
-        return COSInputStream.create(getFilterList(), this, input, scratchFile, options);
+        return COSInputStream.create(getFilterList(), this, input, options);
     }
 
     /**
@@ -253,23 +253,18 @@
                 throw new IOException("Duplicate");
             }
             InputStream input = createRawInputStream();
-            ByteArrayOutputStream output = null;
+            ByteArrayOutputStream output = new ByteArrayOutputStream();
             // apply filters
             for (int i = 0; i < filterList.size(); i++)
             {
-                if (output != null)
+                if (i > 0)
                 {
                     input = new ByteArrayInputStream(output.toByteArray());
+                    output.reset();
                 }
-                output = new ByteArrayOutputStream();
                 filterList.get(i).decode(input, output, this, i, DecodeOptions.DEFAULT);
             }
-            if (output != null)
-            {
-                return new RandomAccessReadBuffer(output.toByteArray());
-            }
-            // shouldn't be reached at all
-            return null;
+            return new RandomAccessReadBuffer(output.toByteArray());
         }
     }