PDFMetadata.java
8.31 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
/*
* 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.PaddedInputByteStream
* com.adobe.internal.io.stream.StreamManager
* com.adobe.internal.pdftoolkit.core.cos.CosObject
* com.adobe.internal.pdftoolkit.core.cos.CosStream
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidXMLException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.types.ASName
* com.adobe.internal.pdftoolkit.core.util.Utility
*/
package com.adobe.internal.pdftoolkit.pdf.interchange.metadata;
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.PaddedInputByteStream;
import com.adobe.internal.io.stream.StreamManager;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidXMLException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.util.Utility;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosStream;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class PDFMetadata
extends PDFCosStream {
private static final byte[] xmptag = new byte[]{60, 120, 58, 120, 109, 112, 109, 101, 116, 97};
private static final byte[] xaptag = new byte[]{60, 120, 58, 120, 97, 112, 109, 101, 116, 97};
private static final byte[] xmpendtag = new byte[]{60, 47, 120, 58, 120, 109, 112, 109, 101, 116, 97};
private static final byte[] xapendtag = new byte[]{60, 47, 120, 58, 120, 97, 112, 109, 101, 116, 97};
private static final byte[] closetag = new byte[]{62};
private PDFMetadata(CosObject cosStream) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
super(cosStream);
}
public static PDFMetadata getInstance(CosObject cosObject) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (PDFCosObject.checkNullCosObject(cosObject) == null) {
return null;
}
PDFMetadata pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFMetadata.class);
if (pdfObject == null) {
pdfObject = new PDFMetadata(cosObject);
}
return pdfObject;
}
private static PDFMetadata newInstance(PDFDocument pdfDocument) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosStream cosObject = PDFCosObject.newCosStream(pdfDocument);
PDFMetadata pdfObject = new PDFMetadata((CosObject)cosObject);
pdfObject.setDictionaryNameValue(ASName.k_Type, ASName.k_Metadata);
pdfObject.setDictionaryNameValue(ASName.k_Subtype, ASName.k_XML);
return pdfObject;
}
public static PDFMetadata newInstance(PDFDocument pdfDocument, InputByteStream metadata) throws PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
if (metadata == null) {
return null;
}
PDFMetadata pdfMetadata = PDFMetadata.newInstance(pdfDocument);
try {
pdfMetadata.setStreamData(metadata.toInputStream());
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
return pdfMetadata;
}
public static PDFMetadata newInstance(PDFDocument pdfDocument, InputStream metadata, int padding) throws PDFInvalidXMLException, PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
PDFMetadata pdfMetadata = PDFMetadata.newInstance(pdfDocument);
pdfMetadata.setData(metadata, padding);
return pdfMetadata;
}
private InputByteStream getData() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
try {
CosStream cosMetadata = this.getCosStream();
InputByteStream metaData = cosMetadata.getStreamDecoded();
long[] xmpStartEndIndex = this.getXMPMetadata(metaData);
if (xmpStartEndIndex != null && xmpStartEndIndex.length == 2 && xmpStartEndIndex[0] != -1 && xmpStartEndIndex[1] != -1) {
InputByteStream metaDataSlice = metaData.slice(xmpStartEndIndex[0], xmpStartEndIndex[1] - xmpStartEndIndex[0] + 1);
metaData.close();
metaData = null;
return metaDataSlice;
}
return null;
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
}
public boolean getData(OutputStream dataStream) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
try {
InputByteStream returnData = this.getData();
if (returnData == null) {
return false;
}
IO.copy((InputByteStream)returnData, (OutputStream)dataStream);
returnData.close();
returnData = null;
return true;
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
}
private long[] getXMPMetadata(InputByteStream searchStm) throws IOException {
long[] startEndIndex = new long[]{-1, -1};
startEndIndex = this.findXMPTag(searchStm, xmptag, xmpendtag);
if (startEndIndex == null || startEndIndex.length != 2 || startEndIndex[0] == -1 || startEndIndex[1] == -1) {
startEndIndex = this.findXMPTag(searchStm, xaptag, xapendtag);
}
return startEndIndex;
}
private long[] findXMPTag(InputByteStream searchStm, byte[] startTag, byte[] endTag) throws IOException {
long[] startEndIndex = new long[]{-1, -1};
searchStm.seek(0);
int[] startTagKMPArray = Utility.ComputeKMPNextArray((byte[])startTag);
long metastart = Utility.KMPFindFirst((byte[])startTag, (int[])startTagKMPArray, (InputByteStream)searchStm);
if (metastart != -1) {
int[] endTagKMPArray = Utility.ComputeKMPNextArray((byte[])endTag);
long metaend = Utility.KMPFindFirst((byte[])endTag, (int[])endTagKMPArray, (InputByteStream)searchStm);
long dataend = -1;
if (metaend != -1) {
searchStm.seek(metaend);
int[] closeTagKMPArray = Utility.ComputeKMPNextArray((byte[])closetag);
dataend = Utility.KMPFindFirst((byte[])closetag, (int[])closeTagKMPArray, (InputByteStream)searchStm);
}
if (dataend == -1) {
dataend = searchStm.length() - 1;
}
startEndIndex[0] = metastart;
startEndIndex[1] = dataend;
}
return startEndIndex;
}
public void setData(InputStream metadata, int padding) throws PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
try {
OutputByteStream obs = this.getStreamManager().getOutputByteStreamClearDocument(ByteWriterFactory.Fixed.GROWABLE, (long)metadata.available());
IO.copy((InputStream)metadata, (OutputByteStream)obs);
PaddedInputByteStream newData = new PaddedInputByteStream(obs.closeAndConvert(), 32, (long)padding);
obs = null;
this.getCosStream().newDataDecoded((InputByteStream)newData);
newData = null;
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
}
public boolean getXMLData(OutputStream dataStream) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getCosStream().copyStream((Object)dataStream, false);
}
}