CosObjectStream.java
7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.io.ByteWriterFactory
* com.adobe.internal.io.ByteWriterFactory$Fixed
* com.adobe.internal.io.stream.IO
* com.adobe.internal.io.stream.InputByteStream
* com.adobe.internal.io.stream.OutputByteStream
* com.adobe.internal.io.stream.StreamManager
*/
package com.adobe.internal.pdftoolkit.core.cos;
import com.adobe.internal.io.ByteWriterFactory;
import com.adobe.internal.io.stream.IO;
import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.io.stream.OutputByteStream;
import com.adobe.internal.io.stream.StreamManager;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosList;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectInfo;
import com.adobe.internal.pdftoolkit.core.cos.CosParseBuf;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.cos.CosToken;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.util.StringOps;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.Iterator;
import java.util.Map;
public class CosObjectStream
extends CosStream {
private CosList mNewObjList;
private int[] mNewObjNumbers;
private int[] mNewObjOffsets;
private int[] mOldObjNumbers;
private int[] mOldObjOffsets;
private Object mOldDataStream;
CosObjectStream(CosDocument doc) throws PDFCosParseException, PDFIOException, PDFSecurityException, IOException {
super(doc, doc.newObjectInfo());
this.put(ASName.k_Type, ASName.k_ObjStm);
this.getInfo().setIsObjStm(true);
}
CosObjectStream(CosDocument doc, Map map, CosObjectInfo info, long pos) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
super(doc, map, info, pos);
int numObjects = this.get(ASName.k_N).intValue();
int objBase = this.get(ASName.k_First).intValue();
InputByteStream stmData = this.getDataStream();
stmData.seek(0);
CosParseBuf pBuf = new CosParseBuf(stmData, objBase);
this.mOldObjNumbers = new int[numObjects];
this.mOldObjOffsets = new int[numObjects];
for (int objNdx = 0; objNdx < numObjects; ++objNdx) {
this.mOldObjNumbers[objNdx] = CosToken.readNumber(pBuf, CosToken.skipWhitespace(pBuf)).intValue();
int offset = CosToken.readNumber(pBuf, CosToken.skipWhitespace(pBuf)).intValue();
this.mOldObjOffsets[objNdx] = objBase + offset;
}
info.setIsObjStm(true);
}
InputByteStream getBytesForObject(int objNum) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
for (int i = 0; i < this.mOldObjNumbers.length; ++i) {
if (this.mOldObjNumbers[i] != objNum) continue;
InputByteStream stm = this.getDataStream();
stm.seek((long)this.mOldObjOffsets[i]);
int objLen = 0;
objLen = i == this.mOldObjNumbers.length - 1 ? (int)stm.length() : this.mOldObjOffsets[i + 1];
return new CosParseBuf(stm, (objLen -= this.mOldObjOffsets[i]) + 1, true);
}
return null;
}
InputByteStream getDataStream() throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
if (this.mOldDataStream instanceof InputByteStream) {
return (InputByteStream)this.mOldDataStream;
}
InputByteStream dataStream = null;
if (this.mOldDataStream instanceof SoftReference) {
dataStream = (InputByteStream)((SoftReference)this.mOldDataStream).get();
}
if (dataStream == null) {
dataStream = this.getStream(false, false, false);
this.mOldDataStream = new SoftReference<InputByteStream>(dataStream);
}
return dataStream;
}
void addObjectToStream(CosObject obj) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
this.getInfo().markDirty();
if (this.mNewObjList == null) {
this.mNewObjList = new CosList();
}
this.mNewObjList.add(obj.getInfo());
}
void setObjectStreamList(CosList objectList) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
this.getInfo().markDirty();
this.mNewObjList = objectList;
}
void update() {
if (this.mNewObjList != null) {
this.mOldObjNumbers = this.mNewObjNumbers;
this.mOldObjOffsets = this.mNewObjOffsets;
this.reset();
}
}
void reset() {
this.mNewObjList = null;
this.mNewObjNumbers = null;
this.mNewObjOffsets = null;
this.mOldDataStream = null;
}
void writeObjectsToStream() throws PDFCosParseException, PDFIOException, PDFSecurityException, IOException {
if (this.mNewObjList == null || this.mNewObjList.isEmpty()) {
return;
}
int numObjects = 0;
int[] objNumbers = new int[this.mNewObjList.count()];
int[] objOffsets = new int[this.mNewObjList.count()];
OutputByteStream objos = this.getStreamManager().getOutputByteStreamClearDocument(ByteWriterFactory.Fixed.GROWABLE, -1);
Iterator iterator = this.mNewObjList.iterator();
while (iterator.hasNext()) {
CosObjectInfo info = (CosObjectInfo)iterator.next();
CosObject obj = info.getObject();
info.setPos(objos.getPosition());
info.setObjGen(0);
objNumbers[numObjects] = info.getObjNum();
objOffsets[numObjects] = (int)info.getPos();
info.setWriteCompressed(true);
obj.writeOut(objos);
objos.write(32);
info.setStreamInfo(this.getInfo());
info.setStreamNdx(numObjects);
++numObjects;
}
OutputByteStream os = this.getStreamManager().getOutputByteStreamClearDocument(ByteWriterFactory.Fixed.GROWABLE, -1);
Iterator iter = this.mNewObjList.iterator();
while (iter.hasNext()) {
CosObjectInfo info = (CosObjectInfo)iter.next();
os.write(StringOps.toByteArray(Integer.toString(info.getObjNum())));
os.write(32);
os.write(StringOps.toByteArray(Long.toString(info.getPos())));
os.write(32);
}
long objBase = os.getPosition();
this.put(ASName.k_First, (int)objBase);
int i = 0;
while (i < numObjects) {
int[] arrn = objOffsets;
int n = i++;
arrn[n] = (int)((long)arrn[n] + objBase);
}
this.put(ASName.k_N, numObjects);
InputByteStream objis = objos.closeAndConvert();
objos = null;
IO.copy((InputByteStream)objis, (OutputByteStream)os);
objis.close();
objis = null;
this.mOldDataStream = this.getDataStream();
this.newDataDecoded(os.closeAndConvert());
os = null;
this.mNewObjNumbers = objNumbers;
this.mNewObjOffsets = objOffsets;
this.put(ASName.k_Filter, ASName.k_FlateDecode);
}
}