-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRIFFWriter.java
365 lines (304 loc) · 10.8 KB
/
RIFFWriter.java
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.gervill;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
/**
* Resource Interchange File Format (RIFF) stream encoder.
*
* @author Karl Helgason
*/
public final class RIFFWriter extends OutputStream {
private interface RandomAccessWriter {
public void seek(long chunksizepointer) throws IOException;
public long getPointer() throws IOException;
public void close() throws IOException;
public void write(int b) throws IOException;
public void write(byte[] b, int off, int len) throws IOException;
public void write(byte[] bytes) throws IOException;
public long length() throws IOException;
public void setLength(long i) throws IOException;
}
private static class RandomAccessFileWriter implements RandomAccessWriter {
RandomAccessFile raf;
RandomAccessFileWriter(File file) throws FileNotFoundException {
this.raf = new RandomAccessFile(file, "rw");
}
RandomAccessFileWriter(String name) throws FileNotFoundException {
this.raf = new RandomAccessFile(name, "rw");
}
public void seek(long chunksizepointer) throws IOException {
raf.seek(chunksizepointer);
}
public long getPointer() throws IOException {
return raf.getFilePointer();
}
public void close() throws IOException {
raf.close();
}
public void write(int b) throws IOException {
raf.write(b);
}
public void write(byte[] b, int off, int len) throws IOException {
raf.write(b, off, len);
}
public void write(byte[] bytes) throws IOException {
raf.write(bytes);
}
public long length() throws IOException {
return raf.length();
}
public void setLength(long i) throws IOException {
raf.setLength(i);
}
}
private static class RandomAccessByteWriter implements RandomAccessWriter {
byte[] buff = new byte[32];
int length = 0;
int pos = 0;
byte[] s;
final OutputStream stream;
RandomAccessByteWriter(OutputStream stream) {
this.stream = stream;
}
public void seek(long chunksizepointer) throws IOException {
pos = (int) chunksizepointer;
}
public long getPointer() throws IOException {
return pos;
}
public void close() throws IOException {
stream.write(buff, 0, length);
stream.close();
}
public void write(int b) throws IOException {
if (s == null)
s = new byte[1];
s[0] = (byte)b;
write(s, 0, 1);
}
public void write(byte[] b, int off, int len) throws IOException {
int newsize = pos + len;
if (newsize > length)
setLength(newsize);
int end = off + len;
for (int i = off; i < end; i++) {
buff[pos++] = b[i];
}
}
public void write(byte[] bytes) throws IOException {
write(bytes, 0, bytes.length);
}
public long length() throws IOException {
return length;
}
public void setLength(long i) throws IOException {
length = (int) i;
if (length > buff.length) {
int newlen = Math.max(buff.length << 1, length);
byte[] newbuff = new byte[newlen];
System.arraycopy(buff, 0, newbuff, 0, buff.length);
buff = newbuff;
}
}
}
private int chunktype = 0; // 0=RIFF, 1=LIST; 2=CHUNK
private RandomAccessWriter raf;
private final long chunksizepointer;
private final long startpointer;
private RIFFWriter childchunk = null;
private boolean open = true;
private boolean writeoverride = false;
public RIFFWriter(String name, String format) throws IOException {
this(new RandomAccessFileWriter(name), format, 0);
}
public RIFFWriter(File file, String format) throws IOException {
this(new RandomAccessFileWriter(file), format, 0);
}
public RIFFWriter(OutputStream stream, String format) throws IOException {
this(new RandomAccessByteWriter(stream), format, 0);
}
private RIFFWriter(RandomAccessWriter raf, String format, int chunktype)
throws IOException {
if (chunktype == 0)
if (raf.length() != 0)
raf.setLength(0);
this.raf = raf;
if (raf.getPointer() % 2 != 0)
raf.write(0);
if (chunktype == 0)
raf.write("RIFF".getBytes("ascii"));
else if (chunktype == 1)
raf.write("LIST".getBytes("ascii"));
else
raf.write((format + " ").substring(0, 4).getBytes("ascii"));
chunksizepointer = raf.getPointer();
this.chunktype = 2;
writeUnsignedInt(0);
this.chunktype = chunktype;
startpointer = raf.getPointer();
if (chunktype != 2)
raf.write((format + " ").substring(0, 4).getBytes("ascii"));
}
public void seek(long pos) throws IOException {
raf.seek(pos);
}
public long getFilePointer() throws IOException {
return raf.getPointer();
}
public void setWriteOverride(boolean writeoverride) {
this.writeoverride = writeoverride;
}
public boolean getWriteOverride() {
return writeoverride;
}
public void close() throws IOException {
if (!open)
return;
if (childchunk != null) {
childchunk.close();
childchunk = null;
}
int bakchunktype = chunktype;
long fpointer = raf.getPointer();
raf.seek(chunksizepointer);
chunktype = 2;
writeUnsignedInt(fpointer - startpointer);
if (bakchunktype == 0)
raf.close();
else
raf.seek(fpointer);
open = false;
raf = null;
}
public void write(int b) throws IOException {
if (!writeoverride) {
if (chunktype != 2) {
throw new IllegalArgumentException(
"Only chunks can write bytes!");
}
if (childchunk != null) {
childchunk.close();
childchunk = null;
}
}
raf.write(b);
}
public void write(byte b[], int off, int len) throws IOException {
if (!writeoverride) {
if (chunktype != 2) {
throw new IllegalArgumentException(
"Only chunks can write bytes!");
}
if (childchunk != null) {
childchunk.close();
childchunk = null;
}
}
raf.write(b, off, len);
}
public RIFFWriter writeList(String format) throws IOException {
if (chunktype == 2) {
throw new IllegalArgumentException(
"Only LIST and RIFF can write lists!");
}
if (childchunk != null) {
childchunk.close();
childchunk = null;
}
childchunk = new RIFFWriter(this.raf, format, 1);
return childchunk;
}
public RIFFWriter writeChunk(String format) throws IOException {
if (chunktype == 2) {
throw new IllegalArgumentException(
"Only LIST and RIFF can write chunks!");
}
if (childchunk != null) {
childchunk.close();
childchunk = null;
}
childchunk = new RIFFWriter(this.raf, format, 2);
return childchunk;
}
// Write ASCII chars to stream
public void writeString(String string) throws IOException {
byte[] buff = string.getBytes();
write(buff);
}
// Write ASCII chars to stream
public void writeString(String string, int len) throws IOException {
byte[] buff = string.getBytes();
if (buff.length > len)
write(buff, 0, len);
else {
write(buff);
for (int i = buff.length; i < len; i++)
write(0);
}
}
// Write 8 bit signed integer to stream
public void writeByte(int b) throws IOException {
write(b);
}
// Write 16 bit signed integer to stream
public void writeShort(short b) throws IOException {
write((b >>> 0) & 0xFF);
write((b >>> 8) & 0xFF);
}
// Write 32 bit signed integer to stream
public void writeInt(int b) throws IOException {
write((b >>> 0) & 0xFF);
write((b >>> 8) & 0xFF);
write((b >>> 16) & 0xFF);
write((b >>> 24) & 0xFF);
}
// Write 64 bit signed integer to stream
public void writeLong(long b) throws IOException {
write((int) (b >>> 0) & 0xFF);
write((int) (b >>> 8) & 0xFF);
write((int) (b >>> 16) & 0xFF);
write((int) (b >>> 24) & 0xFF);
write((int) (b >>> 32) & 0xFF);
write((int) (b >>> 40) & 0xFF);
write((int) (b >>> 48) & 0xFF);
write((int) (b >>> 56) & 0xFF);
}
// Write 8 bit unsigned integer to stream
public void writeUnsignedByte(int b) throws IOException {
writeByte((byte) b);
}
// Write 16 bit unsigned integer to stream
public void writeUnsignedShort(int b) throws IOException {
writeShort((short) b);
}
// Write 32 bit unsigned integer to stream
public void writeUnsignedInt(long b) throws IOException {
writeInt((int) b);
}
}