@@ -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