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 2020/12/27 14:12:53 UTC
svn commit: r1884845 - in
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter:
COSWriter.java compress/COSWriterObjectStream.java
Author: lehmi
Date: Sun Dec 27 14:12:53 2020
New Revision: 1884845
URL: http://svn.apache.org/viewvc?rev=1884845&view=rev
Log:
PDFBOX-4952: refactor COSWriterObjectStream to avoid creating a new ScratchFile for every object stream
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/compress/COSWriterObjectStream.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java?rev=1884845&r1=1884844&r2=1884845&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java Sun Dec 27 14:12:53 2020
@@ -539,9 +539,11 @@ public class COSWriter implements ICOSVi
{
highestXRefObjectNumber++;
// Create new COSObject for object stream.
- COSStream stream = finalizedObjectStream.update();
+ COSStream stream = finalizedObjectStream
+ .writeObjectsToStream(document.createCOSStream());
// Determine key for object stream.
COSObjectKey objectStreamKey = new COSObjectKey(highestXRefObjectNumber, 0);
+ // Create new COSObject for object stream.
COSObject objectStream = new COSObject(stream, objectStreamKey);
// Add object stream entries to xref - stream.
int i = 0;
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/compress/COSWriterObjectStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/compress/COSWriterObjectStream.java?rev=1884845&r1=1884844&r2=1884845&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/compress/COSWriterObjectStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/compress/COSWriterObjectStream.java Sun Dec 27 14:12:53 2020
@@ -27,6 +27,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -37,9 +38,8 @@ import java.util.Map;
*
* @author Christian Appl
*/
-public class COSWriterObjectStream extends COSStream
+public class COSWriterObjectStream
{
-
private final COSWriterCompressionPool compressionPool;
private final List<COSObjectKey> preparedKeys = new ArrayList<>();
private final List<COSBase> preparedObjects = new ArrayList<>();
@@ -48,54 +48,13 @@ public class COSWriterObjectStream exten
* Creates an object stream for compressible objects from the given {@link COSWriterCompressionPool}. The objects
* must first be prepared for this object stream, by adding them via calling
* {@link COSWriterObjectStream#prepareStreamObject(COSObjectKey, COSBase)} and will be written to this
- * {@link COSStream}, when {@link COSWriterObjectStream#update()} is called.
+ * {@link COSStream}, when {@link COSWriterObjectStream#writeObjectsToStream()} is called.
*
* @param compressionPool The compression pool an object stream shall be created for.
*/
public COSWriterObjectStream(COSWriterCompressionPool compressionPool)
{
this.compressionPool = compressionPool;
- setItem(COSName.TYPE, COSName.OBJ_STM);
- }
-
- /**
- * Returns the number of objects, that have been written to this object stream. ({@link COSName#N})
- *
- * @return The number of objects, that have been written to this object stream.
- */
- public int getObjectCount()
- {
- return getInt(COSName.N, 0);
- }
-
- /**
- * Sets the number of objects, that have been written to this object stream. ({@link COSName#N})
- *
- * @param size The number of objects, that have been written to this object stream.
- */
- public void setObjectCount(int size)
- {
- setInt(COSName.N, size);
- }
-
- /**
- * Returns the byte offset of the first object contained in this object stream. ({@link COSName#FIRST})
- *
- * @return The byte offset of the first object contained in this object stream.
- */
- public int getFirstEntryOffset()
- {
- return getInt(COSName.FIRST, 0);
- }
-
- /**
- * Sets the byte offset of the first object contained in this object stream. ({@link COSName#FIRST})
- *
- * @param firstEntryOffset The byte offset of the first object contained in this object stream.
- */
- public void setFirstEntryOffset(int firstEntryOffset)
- {
- setInt(COSName.FIRST, firstEntryOffset);
}
/**
@@ -117,39 +76,31 @@ public class COSWriterObjectStream exten
/**
* Returns all {@link COSObjectKey}s, that shall be added to the object stream, when
- * {@link COSWriterObjectStream#update()} is called.
+ * {@link COSWriterObjectStream#writeObjectsToStream()} is called.
*
* @return All {@link COSObjectKey}s, that shall be added to the object stream.
*/
public List<COSObjectKey> getPreparedKeys()
{
- return preparedKeys;
- }
-
- /**
- * Returns all {@link COSObject}s, that shall be added to the object stream, when
- * {@link COSWriterObjectStream#update()} is called.
- *
- * @return All {@link COSObject}s, that shall be added to the object stream.
- */
- public List<COSBase> getPreparedObjects()
- {
- return preparedObjects;
+ return Collections.unmodifiableList(preparedKeys);
}
/**
- * Updates the underlying {@link COSStream} by writing all prepared {@link COSObject}s to this object stream.
+ * Writes all prepared {@link COSObject}s to the given {@link COSStream}.
*
- * @return The underlying {@link COSStream} dictionary of this object stream.
+ * @param stream The stream for the compressed objects.
+ * @return The given {@link COSStream} of this object stream.
* @throws IOException Shall be thrown, if writing the object stream failed.
*/
- public COSStream update() throws IOException
+ public COSStream writeObjectsToStream(COSStream stream) throws IOException
{
- setObjectCount(preparedKeys.size());
+ int objectCount = preparedKeys.size();
+ stream.setItem(COSName.TYPE, COSName.OBJ_STM);
+ stream.setInt(COSName.N, objectCount);
// Prepare the compressible objects for writing.
List<Long> objectNumbers = new ArrayList<>();
List<byte[]> objectsBuffer = new ArrayList<>();
- for (int i = 0; i < getObjectCount(); i++)
+ for (int i = 0; i < objectCount; i++)
{
try (ByteArrayOutputStream partialOutput = new ByteArrayOutputStream())
{
@@ -179,16 +130,16 @@ public class COSWriterObjectStream exten
}
// Write Flate compressed object stream data.
- try (OutputStream output = createOutputStream(COSName.FLATE_DECODE))
+ try (OutputStream output = stream.createOutputStream(COSName.FLATE_DECODE))
{
output.write(offsetsMapBuffer);
- setFirstEntryOffset(offsetsMapBuffer.length);
+ stream.setInt(COSName.FIRST, offsetsMapBuffer.length);
for (byte[] rawObject : objectsBuffer)
{
output.write(rawObject);
}
}
- return this;
+ return stream;
}
/**