ByteReaderInputByteStream.java
4.06 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
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.internal.io.stream;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.io.stream.InputByteStreamImpl;
import com.adobe.internal.io.stream.StreamManager;
import java.io.IOException;
final class ByteReaderInputByteStream
extends InputByteStreamImpl {
private StreamManager streamManager;
private boolean isSlice = false;
public ByteReader byteReader = null;
private long position;
private long start;
private long length;
public boolean lengthInitialized;
private boolean registered = false;
ByteReaderInputByteStream(StreamManager streamManager, ByteReader byteReader, boolean register) throws IOException {
this(streamManager, byteReader, 0, -1, register);
}
ByteReaderInputByteStream(StreamManager streamManager, ByteReader byteReader, long startOffset, long length, boolean register) throws IOException {
if (byteReader == null) {
throw new IOException("Null ByteReader parameter.");
}
this.streamManager = streamManager;
this.byteReader = byteReader;
this.start = startOffset;
this.length = length;
this.lengthInitialized = false;
this.registered = register;
if (this.registered) {
this.streamManager.registerInputByteStream(this, this.byteReader, this.isSlice);
}
}
ByteReaderInputByteStream(ByteReaderInputByteStream original, long startOffset, long length) throws IOException {
if (startOffset < 0 || length < 0 || startOffset + length > original.length()) {
throw new IOException("Invalid slice of Bytestream");
}
this.streamManager = original.streamManager;
this.byteReader = original.byteReader;
this.start = original.start + startOffset;
this.length = length;
this.position = this.start;
this.lengthInitialized = original.lengthInitialized;
this.isSlice = true;
this.registered = original.registered;
if (this.registered) {
this.streamManager.registerInputByteStream(this, this.byteReader, this.isSlice);
}
}
@Override
public InputByteStream slice(long begin, long length) throws IOException {
return new ByteReaderInputByteStream(this, begin, length);
}
@Override
public void close() throws IOException {
if (this.registered) {
this.streamManager.deregisterInputByteStream(this, this.byteReader, this.isSlice);
} else if (!this.isSlice) {
this.byteReader.close();
}
this.byteReader = null;
}
@Override
public InputByteStream seek(long position) throws IOException {
if (position < 0) {
position = 0;
}
if (position > this.length()) {
position = this.length();
}
this.position = position + this.start;
return this;
}
@Override
public long getPosition() throws IOException {
return this.position - this.start;
}
@Override
public long length() throws IOException {
if (!this.lengthInitialized) {
this.length = this.length == -1 ? this.byteReader.length() : Math.min(this.byteReader.length() - this.start, this.length);
this.lengthInitialized = true;
}
return this.length;
}
@Override
public int read() throws IOException {
int b = -1;
if (this.position - this.start < this.length || !this.lengthInitialized && this.getPosition() < this.length()) {
b = this.byteReader.read(this.position);
++this.position;
}
return b;
}
@Override
public int read(byte[] b, int offset, int length) throws IOException {
if ((length = (int)Math.min((long)length, this.length() - this.getPosition())) == 0) {
return -1;
}
int bytesRead = this.byteReader.read(this.position, b, offset, length);
this.seek(this.getPosition() + (long)bytesRead);
return bytesRead;
}
}