CosCloneMgr.java
10.9 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* 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.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosBoolean;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosName;
import com.adobe.internal.pdftoolkit.core.cos.CosNull;
import com.adobe.internal.pdftoolkit.core.cos.CosNumeric;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectRef;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectRefAdapter;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.cos.CosString;
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 java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class CosCloneMgr {
private CosDocument mTarget;
private HashMap<CosObjectRefAdapter, CosObjectRefAdapter> mClonedCosObjects;
private HashSet<ASName> streamsToClone;
public CosCloneMgr(CosDocument target) {
this.mTarget = target;
this.mClonedCosObjects = new HashMap();
this.streamsToClone = new HashSet();
}
public CosCloneMgr(CosDocument target, HashSet<ASName> streamsToClone) {
this(target);
if (streamsToClone != null) {
this.streamsToClone.addAll(streamsToClone);
}
}
private CosObject cloneCosArray(CosArray cosObj) throws PDFCosParseException, PDFIOException, PDFSecurityException {
CosArray newCosArray = this.mTarget.createCosArray();
if (cosObj.isIndirect()) {
this.mClonedCosObjects.put(CosObjectRefAdapter.newInstance(cosObj), CosObjectRefAdapter.newInstance(newCosArray));
}
for (int i = 0; i < cosObj.size(); ++i) {
newCosArray.add(this.clone(cosObj.get(i)));
}
return newCosArray;
}
private CosObject cloneCosDictionary(CosDictionary original, CosDictionary clone) throws PDFCosParseException, PDFIOException, PDFSecurityException {
if (original.isIndirect()) {
this.mClonedCosObjects.put(CosObjectRefAdapter.newInstance(original), CosObjectRefAdapter.newInstance(clone));
}
Iterator<Map.Entry<ASName, CosObject>> iter = original.entrySet().iterator();
CosDocument doc = original.getDocument();
try {
while (iter.hasNext()) {
Map.Entry<ASName, CosObject> entry = iter.next();
CosObject obj = entry.getValue();
if (obj instanceof CosObjectRef) {
obj = doc.resolveReference((CosObjectRef)obj);
}
clone.put(entry.getKey(), this.clone(obj));
}
}
catch (IOException e) {
throw new PDFIOException(e);
}
return clone;
}
private CosObject cloneCosDictionary(CosDictionary cosObj) throws PDFCosParseException, PDFIOException, PDFSecurityException {
LinkedHashMap newMap = new LinkedHashMap(cosObj.size());
CosDictionary newCosDictionary = this.mTarget.createCosDictionary(newMap);
return this.cloneCosDictionary(cosObj, newCosDictionary);
}
private CosObject cloneCosStream(CosStream cosObj) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
CosStream cloneStream = this.mTarget.createCosStream();
cloneStream = (CosStream)this.cloneCosDictionary(cosObj, cloneStream);
CosArray outputFilterList = cosObj.getOutputFiltersList();
if (outputFilterList != null) {
CosArray clonedOutputFilterList = (CosArray)this.cloneCosArray(outputFilterList);
cloneStream.setOutputFiltersList(clonedOutputFilterList);
}
this.cloneStreamdata(cosObj, cloneStream);
return cloneStream;
}
public CosObject clone(CosObject cosObj) throws PDFCosParseException, PDFIOException, PDFSecurityException {
if (cosObj instanceof CosBoolean) {
return this.mTarget.createCosBoolean(cosObj.booleanValue());
}
if (cosObj instanceof CosName) {
return this.mTarget.createCosName(cosObj.nameValue());
}
if (cosObj instanceof CosNull) {
return this.mTarget.createCosNull();
}
if (cosObj instanceof CosNumeric) {
return this.mTarget.createCosNumeric((CosNumeric)cosObj);
}
if (cosObj instanceof CosString) {
CosString str = this.mTarget.createCosString(((CosString)cosObj).byteArrayValue());
str.setWriteHex(((CosString)cosObj).getWriteHex());
return str;
}
if (this.mClonedCosObjects.containsKey(CosObjectRefAdapter.newInstance(cosObj))) {
return (CosObject)this.mClonedCosObjects.get(CosObjectRefAdapter.newInstance(cosObj)).getObject();
}
if (cosObj instanceof CosArray) {
return this.cloneCosArray((CosArray)cosObj);
}
if (cosObj instanceof CosStream) {
try {
return this.cloneCosStream((CosStream)cosObj);
}
catch (IOException e) {
throw new PDFIOException(e);
}
}
if (cosObj instanceof CosDictionary) {
return this.cloneCosDictionary((CosDictionary)cosObj);
}
return null;
}
public CosObject shallowClone(CosObject cosObj) throws PDFCosParseException, PDFIOException, PDFSecurityException {
try {
return this.shallowClone(cosObj, false);
}
catch (IOException e) {
throw new PDFIOException(e);
}
}
private CosObject shallowClone(CosObject cosObj, boolean copyStream) throws PDFCosParseException, PDFIOException, PDFSecurityException, IOException {
if (cosObj instanceof CosBoolean) {
return this.mTarget.createCosBoolean(cosObj.booleanValue());
}
if (cosObj instanceof CosName) {
return this.mTarget.createCosName(cosObj.nameValue());
}
if (cosObj instanceof CosNull) {
return this.mTarget.createCosNull();
}
if (cosObj instanceof CosNumeric) {
return this.mTarget.createCosNumeric((CosNumeric)cosObj);
}
if (cosObj instanceof CosString) {
CosString str = this.mTarget.createCosString(((CosString)cosObj).byteArrayValue());
str.setWriteHex(((CosString)cosObj).getWriteHex());
return str;
}
if (cosObj instanceof CosObjectRef) {
return this.shallowClone(this.mTarget.resolveReference((CosObjectRef)cosObj));
}
if (cosObj instanceof CosArray) {
return this.shallowCloneCosArray((CosArray)cosObj);
}
if (cosObj instanceof CosStream) {
if (copyStream) {
try {
return this.shallowCloneCosStream((CosStream)cosObj);
}
catch (IOException e) {
throw new PDFIOException(e);
}
}
return cosObj;
}
if (cosObj instanceof CosDictionary) {
return this.shallowCloneCosDictionary((CosDictionary)cosObj);
}
return null;
}
private CosObject shallowCloneCosArray(CosArray cosObj) throws PDFCosParseException, PDFIOException, PDFSecurityException, IOException {
CosArray newCosArray = this.mTarget.createCosArray();
for (int i = 0; i < cosObj.size(); ++i) {
newCosArray.add(this.shallowClone(cosObj.get(i)));
}
return newCosArray;
}
private CosObject shallowCloneCosDictionary(CosDictionary cosObj) throws PDFCosParseException, PDFIOException, PDFSecurityException, IOException {
LinkedHashMap newMap = new LinkedHashMap(cosObj.size());
CosDictionary newCosDictionary = this.mTarget.createCosDictionary(newMap);
return this.shallowCloneCosDictionary(cosObj, newCosDictionary);
}
private CosObject shallowCloneCosDictionary(CosDictionary original, CosDictionary clone) throws PDFCosParseException, PDFIOException, PDFSecurityException, IOException {
for (Map.Entry<ASName, CosObject> entry : original.entrySet()) {
clone.put(entry.getKey(), this.shallowClone(entry.getValue(), this.streamsToClone != null && this.streamsToClone.contains(entry.getKey())));
}
return clone;
}
private CosObject shallowCloneCosStream(CosStream cosObj) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
CosStream cloneStream = this.mTarget.createCosStream();
cloneStream = (CosStream)this.shallowCloneCosDictionary(cosObj, cloneStream);
this.cloneStreamdata(cosObj, cloneStream);
return cloneStream;
}
private void cloneStreamdata(CosStream cosObj, CosStream cloneStream) throws PDFCosParseException, PDFIOException, PDFSecurityException, IOException {
boolean encoded = cosObj.isEncoded();
InputByteStream dataStream = null;
dataStream = encoded ? cosObj.getStreamEncoded() : cosObj.getStreamDecoded();
if (dataStream != null) {
StreamManager streamManager = cloneStream.getStreamManager();
OutputByteStream clonedData = streamManager.getOutputByteStreamEncryptedDocument(ByteWriterFactory.Fixed.FIXED, dataStream.length());
IO.copy((InputByteStream)dataStream, (OutputByteStream)clonedData);
if (encoded) {
cloneStream.newDataEncoded(clonedData.closeAndConvert());
} else {
cloneStream.newDataDecoded(clonedData.closeAndConvert());
}
dataStream.close();
}
}
public CosObject removeMapping(CosObject cosObj) {
CosObjectRefAdapter object = this.mClonedCosObjects.remove(CosObjectRefAdapter.newInstance(cosObj));
if (object == null) {
return null;
}
return (CosObject)object.getObject();
}
}