Newer
Older
import java.io.IOException;
import java.io.InputStream;
public class ByteReader extends InputStream {
private InputStream stream;
private Endianness end = Endianness.LITTLE_ENDIAN;
public ByteReader(InputStream in) {
stream = in;
}
public ByteReader setBigEndian() {
end = Endianness.BIG_ENDIAN;
return this;
}
public ByteReader setLittleEndian() {
end = Endianness.LITTLE_ENDIAN;
return this;
}
public ByteReader changeEndiannes() {
if (isLitteEndian())
setBigEndian();
else
setLittleEndian();
return this;
}
public boolean isBigEndian() {
return end == Endianness.BIG_ENDIAN;
}
public boolean isLitteEndian() {
return end == Endianness.LITTLE_ENDIAN;
}
public boolean seek(int n) throws IOException {
if (n == 0) return false;
for (int i=0; i<n; i++)
read();
return true;
}
@Override
public int read() throws IOException {
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
return stream.read();
}
public long read2bytes() throws IOException {
long b0 = read();
long b1 = read();
if (isLitteEndian())
return b0 | (b1 << 8);
else
return b1 | (b0 << 8);
}
public long read3bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16);
else
return b2 | (b1 << 8) | (b0 << 16);
}
public long read4bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
long b3 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
else
return b3 | (b2 << 8) | (b1 << 16) | (b0 << 24);
}
public long read5bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
long b3 = read();
long b4 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32);
else
return b4 | (b3 << 8) | (b2 << 16) | (b1 << 24) | (b0 << 32);
}
public long read6bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
long b3 = read();
long b4 = read();
long b5 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32) | (b5 << 40);
else
return b5 | (b4 << 8) | (b3 << 16) | (b2 << 24) | (b1 << 32) | (b0 << 40);
}
public long read7bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
long b3 = read();
long b4 = read();
long b5 = read();
long b6 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32) | (b5 << 40) | (b6 << 48);
else
return b6 | (b5 << 8) | (b4 << 16) | (b3 << 24) | (b2 << 32) | (b1 << 40) | (b0 << 48);
}
public long read8bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
long b3 = read();
long b4 = read();
long b5 = read();
long b6 = read();
long b7 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32) | (b5 << 40) | (b6 << 48) | (b7 << 56);
else
return b7 | (b6 << 8) | (b5 << 16) | (b4 << 24) | (b3 << 32) | (b2 << 40) | (b1 << 48) | (b0 << 56);
}
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
public String readString(int start, int size) throws IOException {
int sz = size + start - getPosition();
if (sz == 0) return null;
byte[] buf = new byte[sz];
int i = 0;
for (;; i++) {
int b = read();
if (b == 0) break;
buf[i] = (byte)b;
}
if (i == 0) return null;
return new String(buf, 0, i);
}
public String readUnicodeString(int start, int size) throws IOException {
int sz = (size + start - getPosition())>>1;
if (sz == 0) return null;
char[] buf = new char[sz];
int i = 0;
for (;; i++) {
char c = (char)read2bytes();
if (c == 0) break;
buf[i] = c;
}
if (i == 0) return null;
return new String(buf, 0, i);
}
}
enum Endianness {
BIG_ENDIAN, LITTLE_ENDIAN
}