ImageInputSourceImpl.java
4.36 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.pdftoolkit.core.cos.CosStream
* com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
*/
package com.adobe.internal.pdftoolkit.graphicsDOM.utils;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
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.image.ImageInputSource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidParameterException;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class ImageInputSourceImpl<T>
implements ImageInputSource {
private static final int readLimit = 67108864;
private T inputSource;
private ResettableInputStream ris = null;
private boolean mark = false;
public ImageInputSourceImpl(T inputSource, boolean mark) {
if (!(inputSource instanceof CosStream) && !(inputSource instanceof InputStream)) {
throw new InvalidParameterException("CosStream or InputStream was expected.");
}
this.inputSource = inputSource;
this.mark = mark;
}
@Override
public InputStream getImageInputStream() throws IOException {
try {
if (this.ris != null) {
if (this.ris.totalBytesRead == 0) {
return this.ris;
}
if (this.mark && this.ris.markSupported() && this.ris.totalBytesRead < 0x4000000) {
this.ris.reset();
return this.ris;
}
}
InputStream decodedStream = null;
if (this.inputSource instanceof InputStream) {
decodedStream = (InputStream)this.inputSource;
} else if (this.inputSource instanceof CosStream) {
CosStream cosObj = (CosStream)this.inputSource;
decodedStream = cosObj.getStreamDecodedNoCopying();
}
this.ris = new ResettableInputStream(decodedStream);
if (this.mark && this.ris.markSupported()) {
this.ris.mark(67108864);
}
return this.ris;
}
catch (PDFSecurityException e) {
throw new IOException((Throwable)e);
}
catch (PDFCosParseException e) {
throw new IOException((Throwable)e);
}
catch (PDFIOException e) {
throw new IOException((Throwable)e);
}
}
@Override
public void close() throws IOException {
if (this.ris != null) {
this.ris.close();
}
this.inputSource = null;
}
private static class ResettableInputStream
extends InputStream {
private InputStream sourceIS = null;
private long totalBytesRead = 0;
private ResettableInputStream(InputStream is) {
this.sourceIS = is;
}
public int read() throws IOException {
++this.totalBytesRead;
return this.sourceIS.read();
}
public int read(byte[] b) throws IOException {
return this.read(b, 0, b.length);
}
public int read(byte[] b, int off, int len) throws IOException {
int bytesRead = this.sourceIS.read(b, off, len);
this.totalBytesRead += (long)bytesRead;
return bytesRead;
}
public long skip(long n) throws IOException {
long bytesRead = this.sourceIS.skip(n);
this.totalBytesRead += bytesRead;
return bytesRead;
}
public int available() throws IOException {
return this.sourceIS.available();
}
public void close() throws IOException {
this.sourceIS.close();
}
public synchronized void mark(int readlimit) {
this.sourceIS.mark(readlimit);
}
public synchronized void reset() throws IOException {
this.sourceIS.reset();
}
public boolean markSupported() {
return this.sourceIS.markSupported();
}
}
}