BitInputStream.java
3.53 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
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.internal.io.stream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidParameterException;
public class BitInputStream {
private static final int BUFFER_SIZE = 512;
private InputStream stream = null;
private int currentBitPos = 0;
private byte[] buffer = null;
private int currentBytePos = 0;
private int maxBytesLimit = 512;
private void fillBuffer() throws IOException {
this.maxBytesLimit = this.stream.available() > 0 ? this.stream.read(this.buffer) : -1;
this.currentBytePos = 0;
}
public BitInputStream(InputStream stream) throws IOException {
this.stream = stream;
}
private int readData(int nob) throws IOException {
if (this.maxBytesLimit == -1) {
throw new IOException("End of stream has been reached so no data can be read now.");
}
if (this.currentBytePos == this.maxBytesLimit) {
this.fillBuffer();
}
int value = (255 & this.buffer[this.currentBytePos] << this.currentBitPos) >> 8 - nob;
this.currentBitPos += nob;
if (this.currentBitPos >= 8) {
++this.currentBytePos;
}
this.currentBitPos %= 8;
return value;
}
public int read(int nob) throws IOException {
if (this.buffer == null) {
this.buffer = new byte[512];
this.fillBuffer();
}
int value = 0;
int bitsToRead = 0;
while (nob > 0) {
bitsToRead = nob <= 8 - this.currentBitPos ? nob : 8 - this.currentBitPos;
value |= this.readData(bitsToRead) << (nob -= bitsToRead);
}
return value;
}
public int read() throws IOException {
return this.read(1);
}
public void close() throws IOException {
if (this.stream != null) {
this.stream.close();
}
this.buffer = null;
}
public long skip(long n) throws IOException {
this.read((int)n);
return n;
}
public int read(byte[] b, int bitsPerValue, int length) {
if (b == null || length > b.length) {
throw new InvalidParameterException("Byte array passed is either null or insufficient to read the number of bytes actually asked to read.");
}
if (bitsPerValue > 8) {
throw new InvalidParameterException("BitsPerValue is " + bitsPerValue + " which is greater than 8 and cann't be fit into byte.");
}
int i = 0;
try {
for (i = 0; i < length && this.maxBytesLimit != -1; ++i) {
b[i] = (byte)this.read(bitsPerValue);
}
}
catch (IOException e) {
// empty catch block
}
return i;
}
public int read(int[] b, int bitsPerValue, int length) {
if (b == null || length > b.length) {
throw new InvalidParameterException("Byte array passed is either null or insufficient to read the number of bytes actually asked to read.");
}
int i = 0;
try {
for (i = 0; i < length && this.maxBytesLimit != -1; ++i) {
b[i] = this.read(bitsPerValue);
}
}
catch (IOException e) {
// empty catch block
}
return i;
}
public int available() throws IOException {
if (this.maxBytesLimit == -1) {
return 0;
}
return 8 - this.currentBitPos + (this.maxBytesLimit - this.currentBytePos + this.stream.available()) * 8;
}
}