829N/A/*
6321N/A * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
829N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
829N/A *
829N/A * This code is free software; you can redistribute it and/or modify it
829N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
829N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
829N/A *
829N/A * This code is distributed in the hope that it will be useful, but WITHOUT
829N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
829N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
829N/A * version 2 for more details (a copy is included in the LICENSE file that
829N/A * accompanied this code).
829N/A *
829N/A * You should have received a copy of the GNU General Public License version
829N/A * 2 along with this work; if not, write to the Free Software Foundation,
829N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
829N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
829N/A */
829N/Apackage com.sun.media.sound;
829N/A
829N/Aimport java.io.File;
829N/Aimport java.io.FileNotFoundException;
829N/Aimport java.io.IOException;
829N/Aimport java.io.OutputStream;
829N/Aimport java.io.RandomAccessFile;
829N/A
829N/A/**
829N/A * Resource Interchange File Format (RIFF) stream encoder.
829N/A *
829N/A * @author Karl Helgason
829N/A */
6321N/Apublic final class RIFFWriter extends OutputStream {
829N/A
829N/A private interface RandomAccessWriter {
829N/A
829N/A public void seek(long chunksizepointer) throws IOException;
829N/A
829N/A public long getPointer() throws IOException;
829N/A
829N/A public void close() throws IOException;
829N/A
829N/A public void write(int b) throws IOException;
829N/A
829N/A public void write(byte[] b, int off, int len) throws IOException;
829N/A
829N/A public void write(byte[] bytes) throws IOException;
829N/A
829N/A public long length() throws IOException;
829N/A
829N/A public void setLength(long i) throws IOException;
829N/A }
829N/A
829N/A private static class RandomAccessFileWriter implements RandomAccessWriter {
829N/A
829N/A RandomAccessFile raf;
829N/A
6321N/A RandomAccessFileWriter(File file) throws FileNotFoundException {
829N/A this.raf = new RandomAccessFile(file, "rw");
829N/A }
829N/A
6321N/A RandomAccessFileWriter(String name) throws FileNotFoundException {
829N/A this.raf = new RandomAccessFile(name, "rw");
829N/A }
829N/A
829N/A public void seek(long chunksizepointer) throws IOException {
829N/A raf.seek(chunksizepointer);
829N/A }
829N/A
829N/A public long getPointer() throws IOException {
829N/A return raf.getFilePointer();
829N/A }
829N/A
829N/A public void close() throws IOException {
829N/A raf.close();
829N/A }
829N/A
829N/A public void write(int b) throws IOException {
829N/A raf.write(b);
829N/A }
829N/A
829N/A public void write(byte[] b, int off, int len) throws IOException {
829N/A raf.write(b, off, len);
829N/A }
829N/A
829N/A public void write(byte[] bytes) throws IOException {
829N/A raf.write(bytes);
829N/A }
829N/A
829N/A public long length() throws IOException {
829N/A return raf.length();
829N/A }
829N/A
829N/A public void setLength(long i) throws IOException {
829N/A raf.setLength(i);
829N/A }
829N/A }
829N/A
829N/A private static class RandomAccessByteWriter implements RandomAccessWriter {
829N/A
829N/A byte[] buff = new byte[32];
829N/A int length = 0;
829N/A int pos = 0;
829N/A byte[] s;
6321N/A final OutputStream stream;
829N/A
6321N/A RandomAccessByteWriter(OutputStream stream) {
829N/A this.stream = stream;
829N/A }
829N/A
829N/A public void seek(long chunksizepointer) throws IOException {
829N/A pos = (int) chunksizepointer;
829N/A }
829N/A
829N/A public long getPointer() throws IOException {
829N/A return pos;
829N/A }
829N/A
829N/A public void close() throws IOException {
829N/A stream.write(buff, 0, length);
829N/A stream.close();
829N/A }
829N/A
829N/A public void write(int b) throws IOException {
829N/A if (s == null)
829N/A s = new byte[1];
829N/A s[0] = (byte)b;
829N/A write(s, 0, 1);
829N/A }
829N/A
829N/A public void write(byte[] b, int off, int len) throws IOException {
829N/A int newsize = pos + len;
829N/A if (newsize > length)
829N/A setLength(newsize);
829N/A int end = off + len;
829N/A for (int i = off; i < end; i++) {
829N/A buff[pos++] = b[i];
829N/A }
829N/A }
829N/A
829N/A public void write(byte[] bytes) throws IOException {
829N/A write(bytes, 0, bytes.length);
829N/A }
829N/A
829N/A public long length() throws IOException {
829N/A return length;
829N/A }
829N/A
829N/A public void setLength(long i) throws IOException {
829N/A length = (int) i;
829N/A if (length > buff.length) {
829N/A int newlen = Math.max(buff.length << 1, length);
829N/A byte[] newbuff = new byte[newlen];
829N/A System.arraycopy(buff, 0, newbuff, 0, buff.length);
829N/A buff = newbuff;
829N/A }
829N/A }
829N/A }
829N/A private int chunktype = 0; // 0=RIFF, 1=LIST; 2=CHUNK
829N/A private RandomAccessWriter raf;
6321N/A private final long chunksizepointer;
6321N/A private final long startpointer;
829N/A private RIFFWriter childchunk = null;
829N/A private boolean open = true;
829N/A private boolean writeoverride = false;
829N/A
829N/A public RIFFWriter(String name, String format) throws IOException {
829N/A this(new RandomAccessFileWriter(name), format, 0);
829N/A }
829N/A
829N/A public RIFFWriter(File file, String format) throws IOException {
829N/A this(new RandomAccessFileWriter(file), format, 0);
829N/A }
829N/A
829N/A public RIFFWriter(OutputStream stream, String format) throws IOException {
829N/A this(new RandomAccessByteWriter(stream), format, 0);
829N/A }
829N/A
829N/A private RIFFWriter(RandomAccessWriter raf, String format, int chunktype)
829N/A throws IOException {
829N/A if (chunktype == 0)
829N/A if (raf.length() != 0)
829N/A raf.setLength(0);
829N/A this.raf = raf;
829N/A if (raf.getPointer() % 2 != 0)
829N/A raf.write(0);
829N/A
829N/A if (chunktype == 0)
829N/A raf.write("RIFF".getBytes("ascii"));
829N/A else if (chunktype == 1)
829N/A raf.write("LIST".getBytes("ascii"));
829N/A else
829N/A raf.write((format + " ").substring(0, 4).getBytes("ascii"));
829N/A
829N/A chunksizepointer = raf.getPointer();
829N/A this.chunktype = 2;
829N/A writeUnsignedInt(0);
829N/A this.chunktype = chunktype;
829N/A startpointer = raf.getPointer();
829N/A if (chunktype != 2)
829N/A raf.write((format + " ").substring(0, 4).getBytes("ascii"));
829N/A
829N/A }
829N/A
829N/A public void seek(long pos) throws IOException {
829N/A raf.seek(pos);
829N/A }
829N/A
829N/A public long getFilePointer() throws IOException {
829N/A return raf.getPointer();
829N/A }
829N/A
829N/A public void setWriteOverride(boolean writeoverride) {
829N/A this.writeoverride = writeoverride;
829N/A }
829N/A
829N/A public boolean getWriteOverride() {
829N/A return writeoverride;
829N/A }
829N/A
829N/A public void close() throws IOException {
829N/A if (!open)
829N/A return;
829N/A if (childchunk != null) {
829N/A childchunk.close();
829N/A childchunk = null;
829N/A }
829N/A
829N/A int bakchunktype = chunktype;
829N/A long fpointer = raf.getPointer();
829N/A raf.seek(chunksizepointer);
829N/A chunktype = 2;
829N/A writeUnsignedInt(fpointer - startpointer);
829N/A
829N/A if (bakchunktype == 0)
829N/A raf.close();
829N/A else
829N/A raf.seek(fpointer);
829N/A open = false;
829N/A raf = null;
829N/A }
829N/A
829N/A public void write(int b) throws IOException {
829N/A if (!writeoverride) {
829N/A if (chunktype != 2) {
829N/A throw new IllegalArgumentException(
829N/A "Only chunks can write bytes!");
829N/A }
829N/A if (childchunk != null) {
829N/A childchunk.close();
829N/A childchunk = null;
829N/A }
829N/A }
829N/A raf.write(b);
829N/A }
829N/A
829N/A public void write(byte b[], int off, int len) throws IOException {
829N/A if (!writeoverride) {
829N/A if (chunktype != 2) {
829N/A throw new IllegalArgumentException(
829N/A "Only chunks can write bytes!");
829N/A }
829N/A if (childchunk != null) {
829N/A childchunk.close();
829N/A childchunk = null;
829N/A }
829N/A }
829N/A raf.write(b, off, len);
829N/A }
829N/A
829N/A public RIFFWriter writeList(String format) throws IOException {
829N/A if (chunktype == 2) {
829N/A throw new IllegalArgumentException(
829N/A "Only LIST and RIFF can write lists!");
829N/A }
829N/A if (childchunk != null) {
829N/A childchunk.close();
829N/A childchunk = null;
829N/A }
829N/A childchunk = new RIFFWriter(this.raf, format, 1);
829N/A return childchunk;
829N/A }
829N/A
829N/A public RIFFWriter writeChunk(String format) throws IOException {
829N/A if (chunktype == 2) {
829N/A throw new IllegalArgumentException(
829N/A "Only LIST and RIFF can write chunks!");
829N/A }
829N/A if (childchunk != null) {
829N/A childchunk.close();
829N/A childchunk = null;
829N/A }
829N/A childchunk = new RIFFWriter(this.raf, format, 2);
829N/A return childchunk;
829N/A }
829N/A
829N/A // Write ASCII chars to stream
829N/A public void writeString(String string) throws IOException {
829N/A byte[] buff = string.getBytes();
829N/A write(buff);
829N/A }
829N/A
829N/A // Write ASCII chars to stream
829N/A public void writeString(String string, int len) throws IOException {
829N/A byte[] buff = string.getBytes();
829N/A if (buff.length > len)
829N/A write(buff, 0, len);
829N/A else {
829N/A write(buff);
829N/A for (int i = buff.length; i < len; i++)
829N/A write(0);
829N/A }
829N/A }
829N/A
829N/A // Write 8 bit signed integer to stream
829N/A public void writeByte(int b) throws IOException {
829N/A write(b);
829N/A }
829N/A
829N/A // Write 16 bit signed integer to stream
829N/A public void writeShort(short b) throws IOException {
829N/A write((b >>> 0) & 0xFF);
829N/A write((b >>> 8) & 0xFF);
829N/A }
829N/A
829N/A // Write 32 bit signed integer to stream
829N/A public void writeInt(int b) throws IOException {
829N/A write((b >>> 0) & 0xFF);
829N/A write((b >>> 8) & 0xFF);
829N/A write((b >>> 16) & 0xFF);
829N/A write((b >>> 24) & 0xFF);
829N/A }
829N/A
829N/A // Write 64 bit signed integer to stream
829N/A public void writeLong(long b) throws IOException {
829N/A write((int) (b >>> 0) & 0xFF);
829N/A write((int) (b >>> 8) & 0xFF);
829N/A write((int) (b >>> 16) & 0xFF);
829N/A write((int) (b >>> 24) & 0xFF);
829N/A write((int) (b >>> 32) & 0xFF);
829N/A write((int) (b >>> 40) & 0xFF);
829N/A write((int) (b >>> 48) & 0xFF);
829N/A write((int) (b >>> 56) & 0xFF);
829N/A }
829N/A
829N/A // Write 8 bit unsigned integer to stream
829N/A public void writeUnsignedByte(int b) throws IOException {
829N/A writeByte((byte) b);
829N/A }
829N/A
829N/A // Write 16 bit unsigned integer to stream
829N/A public void writeUnsignedShort(int b) throws IOException {
829N/A writeShort((short) b);
829N/A }
829N/A
829N/A // Write 32 bit unsigned integer to stream
829N/A public void writeUnsignedInt(long b) throws IOException {
829N/A writeInt((int) b);
829N/A }
829N/A}