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 2018/07/11 19:25:38 UTC
svn commit: r1835665 - in /pdfbox/trunk/pdfbox/src:
main/java/org/apache/pdfbox/cos/COSOutputStream.java
test/java/org/apache/pdfbox/cos/TestCOSStream.java
Author: tilman
Date: Wed Jul 11 19:25:38 2018
New Revision: 1835665
URL: http://svn.apache.org/viewvc?rev=1835665&view=rev
Log:
PDFBOX-4260: support scratch file buffer instead of byte array output stream, by Jesse Long
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSOutputStream.java
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSStream.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSOutputStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSOutputStream.java?rev=1835665&r1=1835664&r2=1835665&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSOutputStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSOutputStream.java Wed Jul 11 19:25:38 2018
@@ -17,13 +17,15 @@
package org.apache.pdfbox.cos;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
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;
/**
@@ -35,66 +37,135 @@ public final class COSOutputStream exten
{
private final List<Filter> filters;
private final COSDictionary parameters;
- // todo: this is an in-memory buffer, should use scratch file (if any) instead
- private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-
+ private final ScratchFile scratchFile;
+ private RandomAccess buffer;
+
/**
* Creates a new COSOutputStream writes to an encoded COS stream.
*
* @param filters Filters to apply.
* @param parameters Filter parameters.
* @param output Encoded stream.
- * @param scratchFile Scratch file to use, or null.
+ * @param scratchFile Scratch file to use.
+ *
+ * @throws IOException If there was an error creating a temporary buffer
*/
COSOutputStream(List<Filter> filters, COSDictionary parameters, OutputStream output,
- ScratchFile scratchFile)
+ ScratchFile scratchFile) throws IOException
{
super(output);
this.filters = filters;
this.parameters = parameters;
+ this.scratchFile = scratchFile;
+
+ if (filters.isEmpty())
+ {
+ this.buffer = null;
+ }
+ else
+ {
+ this.buffer = scratchFile.createBuffer();
+ }
}
@Override
public void write(byte[] b) throws IOException
{
- buffer.write(b);
+ if (buffer != null)
+ {
+ buffer.write(b);
+ }
+ else
+ {
+ super.write(b);
+ }
}
@Override
public void write(byte[] b, int off, int len) throws IOException
{
- buffer.write(b, off, len);
+ if (buffer != null)
+ {
+ buffer.write(b, off, len);
+ }
+ else
+ {
+ super.write(b, off, len);
+ }
}
@Override
public void write(int b) throws IOException
{
- buffer.write(b);
+ if (buffer != null)
+ {
+ buffer.write(b);
+ }
+ else
+ {
+ super.write(b);
+ }
}
@Override
public void flush() throws IOException
{
}
-
+
@Override
public void close() throws IOException
{
- if (buffer == null)
+ try
{
- return;
+ if (buffer != null)
+ {
+ try
+ {
+ // apply filters in reverse order
+ for (int i = filters.size() - 1; i >= 0; i--)
+ {
+ try (InputStream unfilteredIn = new RandomAccessInputStream(buffer))
+ {
+ if (i == 0)
+ {
+ /*
+ * The last filter to run can encode directly to the enclosed output
+ * stream.
+ */
+ filters.get(i).encode(unfilteredIn, out, parameters, i);
+ }
+ else
+ {
+ RandomAccess filteredBuffer = scratchFile.createBuffer();
+ try
+ {
+ try (OutputStream filteredOut = new RandomAccessOutputStream(filteredBuffer))
+ {
+ filters.get(i).encode(unfilteredIn, filteredOut, parameters, i);
+ }
+
+ RandomAccess tmpSwap = filteredBuffer;
+ filteredBuffer = buffer;
+ buffer = tmpSwap;
+ }
+ finally
+ {
+ filteredBuffer.close();
+ }
+ }
+ }
+ }
+ }
+ finally
+ {
+ buffer.close();
+ buffer = null;
+ }
+ }
}
- // apply filters in reverse order
- for (int i = filters.size() - 1; i >= 0; i--)
+ finally
{
- // todo: this is an in-memory buffer, should use scratch file (if any) instead
- ByteArrayInputStream input = new ByteArrayInputStream(buffer.toByteArray());
- buffer = new ByteArrayOutputStream();
- filters.get(i).encode(input, buffer, parameters, i);
- }
- // flush the entire stream
- buffer.writeTo(out);
- super.close();
- buffer = null;
+ super.close();
+ }
}
}
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSStream.java?rev=1835665&r1=1835664&r2=1835665&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSStream.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/TestCOSStream.java Wed Jul 11 19:25:38 2018
@@ -78,9 +78,10 @@ public class TestCOSStream extends TestC
byte[] testStringEncoded = encodeData(testString, COSName.FLATE_DECODE);
COSStream stream = new COSStream();
- OutputStream output = stream.createRawOutputStream();
- output.write(testStringEncoded);
- output.close();
+ try (OutputStream output = stream.createRawOutputStream())
+ {
+ output.write(testStringEncoded);
+ }
stream.setItem(COSName.FILTER, COSName.FLATE_DECODE);
validateDecoded(stream, testString);
@@ -122,13 +123,33 @@ public class TestCOSStream extends TestC
filters.add(COSName.FLATE_DECODE);
stream.setItem(COSName.FILTER, filters);
- OutputStream output = stream.createRawOutputStream();
- output.write(testStringEncoded);
- output.close();
+ try (OutputStream output = stream.createRawOutputStream())
+ {
+ output.write(testStringEncoded);
+ }
validateDecoded(stream, testString);
}
+ /**
+ * Tests tests that encoding is done correctly even if the the stream is closed twice.
+ * Closeable.close() allows streams to be closed multiple times. The second and subsequent
+ * close() calls should have no effect.
+ *
+ * @throws IOException
+ */
+ public void testCompressedStreamDoubleClose() throws IOException
+ {
+ byte[] testString = "This is a test string to be used as input for TestCOSStream".getBytes("ASCII");
+ byte[] testStringEncoded = encodeData(testString, COSName.FLATE_DECODE);
+ COSStream stream = new COSStream();
+ OutputStream output = stream.createOutputStream(COSName.FLATE_DECODE);
+ output.write(testString);
+ output.close();
+ output.close();
+ validateEncoded(stream, testStringEncoded);
+ }
+
private byte[] encodeData(byte[] original, COSName filter) throws IOException
{
Filter encodingFilter = FilterFactory.INSTANCE.getFilter(filter);
@@ -140,9 +161,10 @@ public class TestCOSStream extends TestC
private COSStream createStream(byte[] testString, COSBase filters) throws IOException
{
COSStream stream = new COSStream();
- OutputStream output = stream.createOutputStream(filters);
- output.write(testString);
- output.close();
+ try (OutputStream output = stream.createOutputStream(filters))
+ {
+ output.write(testString);
+ }
return stream;
}