|
27 | 27 | import java.security.MessageDigest; |
28 | 28 | import java.security.NoSuchAlgorithmException; |
29 | 29 | import java.util.ArrayList; |
30 | | -import java.util.Collection; |
31 | 30 | import java.util.Collections; |
32 | 31 | import java.util.Date; |
33 | 32 | import java.util.HashMap; |
@@ -1043,26 +1042,30 @@ public PDFReference resolveExtensionReference(String id) { |
1043 | 1042 | * @throws IOException if there is an exception writing to the output stream |
1044 | 1043 | */ |
1045 | 1044 | public void output(OutputStream stream) throws IOException { |
| 1045 | + output(stream, objects); |
| 1046 | + } |
| 1047 | + |
| 1048 | + private void output(OutputStream stream, List<? extends PDFObject> objs) throws IOException { |
1046 | 1049 | outputStarted = true; |
1047 | 1050 | //Write out objects until the list is empty. This approach (used with a |
1048 | 1051 | //LinkedList) allows for output() methods to create and register objects |
1049 | 1052 | //on the fly even during serialization. |
1050 | 1053 |
|
1051 | 1054 | if (objectStreamsEnabled) { |
1052 | | - List<PDFObject> indirectObjects = new ArrayList<>(); |
1053 | | - while (objects.size() > 0) { |
1054 | | - PDFObject object = objects.remove(0); |
| 1055 | + List indirectObjects = new ArrayList<>(); |
| 1056 | + while (!objs.isEmpty()) { |
| 1057 | + PDFObject object = objs.remove(0); |
1055 | 1058 | if (object.supportsObjectStream()) { |
1056 | 1059 | addToObjectStream(object); |
1057 | 1060 | } else { |
1058 | 1061 | indirectObjects.add(object); |
1059 | 1062 | } |
1060 | 1063 | } |
1061 | | - objects.addAll(indirectObjects); |
| 1064 | + objs.addAll(indirectObjects); |
1062 | 1065 | } |
1063 | 1066 |
|
1064 | | - while (objects.size() > 0) { |
1065 | | - PDFObject object = objects.remove(0); |
| 1067 | + while (!objs.isEmpty()) { |
| 1068 | + PDFObject object = objs.remove(0); |
1066 | 1069 | streamIndirectObject(object, stream); |
1067 | 1070 | } |
1068 | 1071 | } |
@@ -1099,13 +1102,6 @@ protected int streamIndirectObject(PDFObject o, OutputStream stream) throws IOEx |
1099 | 1102 | return len; |
1100 | 1103 | } |
1101 | 1104 |
|
1102 | | - private void streamIndirectObjects(Collection<? extends PDFObject> objects, OutputStream stream) |
1103 | | - throws IOException { |
1104 | | - for (PDFObject o : objects) { |
1105 | | - streamIndirectObject(o, stream); |
1106 | | - } |
1107 | | - } |
1108 | | - |
1109 | 1105 | private void recordObjectOffset(PDFObject object) { |
1110 | 1106 | int index = object.getObjectNumber().getNumber() - 1; |
1111 | 1107 | while (indirectObjectOffsets.size() <= index) { |
@@ -1174,6 +1170,7 @@ public void outputHeader(OutputStream stream) throws IOException { |
1174 | 1170 | public void outputTrailer(OutputStream stream) throws IOException { |
1175 | 1171 | createDestinations(); |
1176 | 1172 | output(stream); |
| 1173 | + objects = trailerObjects; |
1177 | 1174 | outputTrailerObjectsAndXref(stream); |
1178 | 1175 | } |
1179 | 1176 |
|
@@ -1203,7 +1200,7 @@ private void outputTrailerObjectsAndXref(OutputStream stream) throws IOException |
1203 | 1200 | } |
1204 | 1201 | trailerOutputHelper.outputStructureTreeElements(stream); |
1205 | 1202 | } |
1206 | | - streamIndirectObjects(trailerObjects, stream); |
| 1203 | + output(stream, trailerObjects); |
1207 | 1204 | TrailerDictionary trailerDictionary = createTrailerDictionary(true); |
1208 | 1205 | long startxref = trailerOutputHelper.outputCrossReferenceObject(stream, trailerDictionary, 0, |
1209 | 1206 | indirectObjectOffsets.size(), indirectObjectOffsets.size()); |
@@ -1267,7 +1264,7 @@ private class UncompressedTrailerOutputHelper implements TrailerOutputHelper { |
1267 | 1264 |
|
1268 | 1265 | public void outputStructureTreeElements(OutputStream stream) |
1269 | 1266 | throws IOException { |
1270 | | - streamIndirectObjects(structureTreeElements, stream); |
| 1267 | + output(stream, structureTreeElements); |
1271 | 1268 | } |
1272 | 1269 |
|
1273 | 1270 | public long outputCrossReferenceObject(OutputStream stream, |
|
0 commit comments