Skip to content

Commit c4d1433

Browse files
committed
PDFBOX-6142: take the size of the stream into account when accessing the data of the underlying byte array
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1931216 13f79535-47bb-0310-9956-ffa450edef68
1 parent cd25b4e commit c4d1433

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/compress/COSWriterObjectStream.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ public COSStream writeObjectsToStream(COSStream stream) throws IOException
113113
stream.setInt(COSName.N, objectCount);
114114
// Prepare the compressible objects for writing.
115115
List<Long> objectNumbers = new ArrayList<>(objectCount);
116-
List<ByteArrayOutputStream> objectsBuffer = new ArrayList<>(objectCount);
116+
List<DirectAccessByteArrayOutputStream> objectsBuffer = new ArrayList<>(objectCount);
117117
for (int i = 0; i < objectCount; i++)
118118
{
119-
try (ByteArrayOutputStream partialOutput = new DirectAccessByteArrayOutputStream())
119+
try (DirectAccessByteArrayOutputStream partialOutput = new DirectAccessByteArrayOutputStream())
120120
{
121121
objectNumbers.add(preparedKeys.get(i).getNumber());
122122
COSBase base = preparedObjects.get(i);
@@ -128,7 +128,7 @@ public COSStream writeObjectsToStream(COSStream stream) throws IOException
128128
// Deduce the object stream byte offset map.
129129
byte[] offsetsMapBuffer;
130130
long nextObjectOffset = 0;
131-
try (ByteArrayOutputStream partialOutput = new DirectAccessByteArrayOutputStream())
131+
try (ByteArrayOutputStream partialOutput = new ByteArrayOutputStream())
132132
{
133133
for (int i = 0; i < objectNumbers.size(); i++)
134134
{
@@ -148,9 +148,9 @@ public COSStream writeObjectsToStream(COSStream stream) throws IOException
148148
{
149149
output.write(offsetsMapBuffer);
150150
stream.setInt(COSName.FIRST, offsetsMapBuffer.length);
151-
for (ByteArrayOutputStream rawObject : objectsBuffer)
151+
for (DirectAccessByteArrayOutputStream rawObject : objectsBuffer)
152152
{
153-
output.write(rawObject.toByteArray());
153+
output.write(rawObject.getRawData(), 0, rawObject.size());
154154
}
155155
}
156156
return stream;
@@ -396,9 +396,13 @@ private void writeCOSNull(OutputStream output) throws IOException
396396
*/
397397
private class DirectAccessByteArrayOutputStream extends ByteArrayOutputStream
398398
{
399-
400-
@Override
401-
public synchronized byte[] toByteArray()
399+
/**
400+
* Gets the underlying byte array. It is most likely bigger than the real size of the stream, so that you have
401+
* to take the size of the stream into account when accessing the data.
402+
*
403+
* @return the underlying byte array.
404+
*/
405+
public byte[] getRawData()
402406
{
403407
return buf;
404408
}

0 commit comments

Comments
 (0)