-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRIFFPrimitivesInputStream.java
More file actions
264 lines (243 loc) · 7.37 KB
/
RIFFPrimitivesInputStream.java
File metadata and controls
264 lines (243 loc) · 7.37 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
136
137
138
139
140
141
142
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* @(#)RIFFPrimitivesInputStream.java 1.0 2005-01-15
*
* Copyright (c) 2005 Werner Randelshofer, Immensee, Switzerland.
* All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with Werner Randelshofer.
* For details see accompanying license terms.
*/
import java.io.*;
/**
* A RIFF primitives input stream lets an application read primitive data
* types in the Microsoft Resource Interfache File Format (RIFF) format from an
* underlying input stream.
*
* Reference:
* AVI RIFF File Reference
* http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c/directx/htm/avirifffilereference.asp
*
* @author Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland
* @version 1.0 2005-01-15 Created.
*/
public class RIFFPrimitivesInputStream extends FilterInputStream {
private long scan, mark;
/**
* Creates a new instance.
*
* @param in the input stream.
*/
public RIFFPrimitivesInputStream(InputStream in) {
super(in);
}
/**
* Read 1 byte from the input stream and interpret
* them as an 8 Bit unsigned UBYTE value.
*/
public int readUBYTE()
throws IOException {
int b0 = in.read();
if (b0 == -1) {
throw new EOFException();
}
scan += 1;
return b0 & 0xff;
}
/**
* Read 2 bytes from the input stream and interpret
* them as a 16 Bit signed WORD value.
*/
public short readWORD()
throws IOException {
int b0 = in.read();
int b1 = in.read();
if (b1 == -1) {
throw new EOFException();
}
scan += 2;
return (short) (((b0 & 0xff) << 0) | ((b1 & 0xff) << 8));
}
/**
* Read 2 bytes from the input stream and interpret
* them as a 16 Bit unsigned UWORD value.
*/
public int readUWORD()
throws IOException {
return readWORD() & 0xffff;
}
/**
* Read 4 bytes from the input stream and interpret
* them as a 32 Bit signed LONG value.
*/
public int readLONG()
throws IOException {
int b0 = in.read();
int b1 = in.read();
int b2 = in.read();
int b3 = in.read();
if (b3 == -1) {
throw new EOFException();
}
scan += 4;
return ((b0&0xff) << 0) +
((b1&0xff) << 8) +
((b2&0xff) << 16) +
((b3&0xff) << 24);
}
/**
* Read 4 bytes from the input stream and interpret
* them as a four byte character code.
*
* Cited from Referenced "AVI RIFF File Reference":
* "A FOURCC (four-character code) is a 32-bit unsigned integer created by
* concatenating four ASCII characters. For example, the FOURCC 'abcd' is
* represented on a Little-Endian system as 0x64636261. FOURCCs can contain
* space characters, so ' abc' is a valid FOURCC. The AVI file format uses
* FOURCC codes to identify stream types, data chunks, index entries, and
* other information."
*/
public int readFourCC()
throws IOException {
int b3 = in.read();
int b2 = in.read();
int b1 = in.read();
int b0 = in.read();
if (b0 == -1) {
throw new EOFException();
}
scan += 4;
return ((b0&0xff) << 0) +
((b1&0xff) << 8) +
((b2&0xff) << 16) +
((b3&0xff) << 24);
}
/**
* Read 4 bytes from the input stream and interpret
* them as a four byte character code.
*
* Cited from Referenced "AVI RIFF File Reference":
* "A FOURCC (four-character code) is a 32-bit unsigned integer created by
* concatenating four ASCII characters. For example, the FOURCC 'abcd' is
* represented on a Little-Endian system as 0x64636261. FOURCCs can contain
* space characters, so ' abc' is a valid FOURCC. The AVI file format uses
* FOURCC codes to identify stream types, data chunks, index entries, and
* other information."
*/
public String readFourCCString()
throws IOException {
byte[] buf = new byte[4];
readFully(buf, 0, 4);
//scan += 4; <- scan is updated by method readFully
return new String(buf, "ASCII");
}
/**
* Read 4 Bytes from the input Stream and interpret
* them as an unsigned Integer value of type ULONG.
*/
public long readULONG()
throws IOException {
return (long)(readLONG()) & 0x00ffffffff;
}
/**
* Align to an even byte position in the input stream.
* This will skip one byte in the stream if the current
* read position is not even.
*/
public void align()
throws IOException {
if (scan % 2 == 1) {
in.skip(1);
scan++;
}
}
/**
* Get the current read position within the file (as seen
* by this input stream filter).
*/
public long getScan()
{ return scan; }
/**
* Reads one byte.
*/
public int read()
throws IOException {
int data = in.read();
if (data != -1) scan++;
return data;
}
/**
* Reads a sequence of bytes.
*/
public int readFully(byte[] b,int offset, int length)
throws IOException {
int count = read(b, offset, length);
if (count != length) {
throw new EOFException("readFully for "+length+" bytes, unexpected EOF after "+count+" bytes.");
}
//scan += count; <- scan is already counted by read method
return count;
}
/**
* Reads a sequence of bytes.
*/
public int read(byte[] b,int offset, int length)
throws IOException {
int count = 0;
while (count < length) {
int result = in.read(b,offset+count,length-count);
if (result == -1) break;
count += result;
}
scan += count;
return count;
}
/**
* Marks the input stream.
* @param readlimit The maximum limit of bytes that can be read before
* the mark position becomes invalid.
*/
public void mark(int readlimit) {
in.mark(readlimit);
mark = scan;
}
/**
* Repositions the stream at the previously marked position.
*
* @exception IOException If the stream has not been marked or if the
* mark has been invalidated.
*/
public void reset()
throws IOException {
in.reset();
scan = mark;
}
/**
* Skips over and discards n bytes of data from this input stream. This skip
* method tries to skip the provided number of bytes.
*/
public long skip(long n)
throws IOException {
long skipped = in.skip(n);
scan += skipped;
return skipped;
}
/**
* Skips over and discards n bytes of data from this input stream. Throws
*
* @param n the number of bytes to be skipped.
* @exception EOFException if this input stream reaches the end before
* skipping all the bytes.
*/
public void skipFully(long n)
throws IOException {
if (n==0) return;
int total = 0;
int cur = 0;
while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
total += cur;
}
if (cur == 0) throw new EOFException();
scan += total;
}
}