ChunkedInflaterInputStream.java
3.88 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.
*/
package com.day.io;
import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
import java.util.zip.ZipException;
public class ChunkedInflaterInputStream
extends FilterInputStream {
protected Inflater inf;
protected byte[] buf;
protected int len;
private boolean closed = false;
private boolean reachEOF = false;
boolean usesDefaultInflater = false;
private byte[] singleByteBuf = new byte[1];
private byte[] b = new byte[512];
private void ensureOpen() throws IOException {
if (this.closed) {
throw new IOException("Stream closed");
}
}
public ChunkedInflaterInputStream(InputStream in, Inflater inf, int size) {
super(in);
if (in == null || inf == null) {
throw new NullPointerException();
}
if (size <= 0) {
throw new IllegalArgumentException("buffer size <= 0");
}
this.inf = inf;
this.buf = new byte[size];
}
public ChunkedInflaterInputStream(InputStream in, Inflater inf) {
this(in, inf, 8192);
}
public ChunkedInflaterInputStream(InputStream in) {
this(in, new Inflater());
this.usesDefaultInflater = true;
}
public int read() throws IOException {
this.ensureOpen();
return this.read(this.singleByteBuf, 0, 1) == -1 ? -1 : this.singleByteBuf[0] & 255;
}
public int read(byte[] b, int off, int len) throws IOException {
this.ensureOpen();
if ((off | len | off + len | b.length - (off + len)) < 0) {
throw new IndexOutOfBoundsException();
}
if (len == 0) {
return 0;
}
try {
int n;
while ((n = this.inf.inflate(b, off, len)) == 0) {
if (this.inf.finished() || this.inf.needsDictionary()) {
int r = this.inf.getRemaining();
if (r == 0 && this.in.available() <= 0) {
this.reachEOF = true;
return -1;
}
this.inf.reset();
this.inf.setInput(this.buf, this.len - r, r);
}
if (!this.inf.needsInput()) continue;
this.fill();
}
return n;
}
catch (DataFormatException e) {
String s = e.getMessage();
throw new ZipException(s != null ? s : "Invalid ZLIB data format");
}
}
public int available() throws IOException {
this.ensureOpen();
if (this.reachEOF) {
return 0;
}
return 1;
}
public long skip(long n) throws IOException {
int len;
int total;
if (n < 0) {
throw new IllegalArgumentException("negative skip length");
}
this.ensureOpen();
int max = (int)Math.min(n, Integer.MAX_VALUE);
for (total = 0; total < max; total += len) {
len = max - total;
if (len > this.b.length) {
len = this.b.length;
}
if ((len = this.read(this.b, 0, len)) != -1) continue;
this.reachEOF = true;
break;
}
return total;
}
public void close() throws IOException {
if (!this.closed) {
if (this.usesDefaultInflater) {
this.inf.end();
}
this.in.close();
this.closed = true;
}
}
protected void fill() throws IOException {
this.ensureOpen();
this.len = this.in.read(this.buf, 0, this.buf.length);
if (this.len == -1) {
throw new EOFException("Unexpected end of ZLIB input stream");
}
this.inf.setInput(this.buf, 0, this.len);
}
}