Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java
41161 views
/*1* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.media.sound;2627import java.io.BufferedOutputStream;28import java.io.ByteArrayInputStream;29import java.io.ByteArrayOutputStream;30import java.io.DataOutputStream;31import java.io.File;32import java.io.FileOutputStream;33import java.io.IOException;34import java.io.InputStream;35import java.io.OutputStream;36import java.io.RandomAccessFile;37import java.io.SequenceInputStream;38import java.util.Objects;3940import javax.sound.sampled.AudioFileFormat;41import javax.sound.sampled.AudioFileFormat.Type;42import javax.sound.sampled.AudioFormat;43import javax.sound.sampled.AudioInputStream;44import javax.sound.sampled.AudioSystem;4546/**47* AU file writer.48*49* @author Jan Borgersen50*/51public final class AuFileWriter extends SunFileWriter {5253/**54* Value for length field if length is not known.55*/56private static final int UNKNOWN_SIZE = -1;5758/**59* Constructs a new AuFileWriter object.60*/61public AuFileWriter() {62super(new Type[]{Type.AU});63}6465@Override66public Type[] getAudioFileTypes(AudioInputStream stream) {6768Type[] filetypes = new Type[types.length];69System.arraycopy(types, 0, filetypes, 0, types.length);7071// make sure we can write this stream72AudioFormat format = stream.getFormat();73AudioFormat.Encoding encoding = format.getEncoding();7475if (AudioFormat.Encoding.ALAW.equals(encoding)76|| AudioFormat.Encoding.ULAW.equals(encoding)77|| AudioFormat.Encoding.PCM_SIGNED.equals(encoding)78|| AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)79|| AudioFormat.Encoding.PCM_FLOAT.equals(encoding)) {80return filetypes;81}8283return new Type[0];84}8586@Override87public int write(AudioInputStream stream, Type fileType, OutputStream out) throws IOException {88Objects.requireNonNull(stream);89Objects.requireNonNull(fileType);90Objects.requireNonNull(out);9192// we must know the total data length to calculate the file length93//$$fb 2001-07-13: fix for bug 4351296: do not throw an exception94//if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {95// throw new IOException("stream length not specified");96//}9798// throws IllegalArgumentException if not supported99AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);100return writeAuFile(stream, auFileFormat, out);101}102103@Override104public int write(AudioInputStream stream, Type fileType, File out) throws IOException {105Objects.requireNonNull(stream);106Objects.requireNonNull(fileType);107Objects.requireNonNull(out);108109// throws IllegalArgumentException if not supported110AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);111112// first write the file without worrying about length fields113final int bytesWritten;114try (final FileOutputStream fos = new FileOutputStream(out);115final BufferedOutputStream bos = new BufferedOutputStream(fos)) {116bytesWritten = writeAuFile(stream, auFileFormat, bos);117}118119// now, if length fields were not specified, calculate them,120// open as a random access file, write the appropriate fields,121// close again....122if( auFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {123124// $$kk: 10.22.99: jan: please either implement this or throw an exception!125// $$fb: 2001-07-13: done. Fixes Bug 4479981126try (final RandomAccessFile raf = new RandomAccessFile(out, "rw")) {127if (raf.length() <= 0x7FFFFFFFl) {128// skip AU magic and data offset field129raf.skipBytes(8);130raf.writeInt(bytesWritten - AuFileFormat.AU_HEADERSIZE);131// that's all132}133}134}135136return bytesWritten;137}138139// -------------------------------------------------------------140141/**142* Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.143* Throws IllegalArgumentException if not supported.144*/145private AudioFileFormat getAudioFileFormat(Type type, AudioInputStream stream) {146if (!isFileTypeSupported(type, stream)) {147throw new IllegalArgumentException("File type " + type + " not supported.");148}149150AudioFormat streamFormat = stream.getFormat();151AudioFormat.Encoding encoding = streamFormat.getEncoding();152153if (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) {154encoding = AudioFormat.Encoding.PCM_SIGNED;155}156157// We always write big endian au files, this is by far the standard158AudioFormat format = new AudioFormat(encoding,159streamFormat.getSampleRate(),160streamFormat.getSampleSizeInBits(),161streamFormat.getChannels(),162streamFormat.getFrameSize(),163streamFormat.getFrameRate(), true);164165int fileSize;166if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) {167fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AuFileFormat.AU_HEADERSIZE;168} else {169fileSize = AudioSystem.NOT_SPECIFIED;170}171172return new AuFileFormat(Type.AU, fileSize, format,173(int) stream.getFrameLength());174}175176private InputStream getFileStream(AuFileFormat auFileFormat, AudioInputStream audioStream) throws IOException {177178// private method ... assumes auFileFormat is a supported file type179180AudioFormat format = auFileFormat.getFormat();181182int headerSize = AuFileFormat.AU_HEADERSIZE;183long dataSize = auFileFormat.getFrameLength();184//$$fb fix for Bug 4351296185//int dataSizeInBytes = dataSize * format.getFrameSize();186long dataSizeInBytes = (dataSize==AudioSystem.NOT_SPECIFIED)?UNKNOWN_SIZE:dataSize * format.getFrameSize();187if (dataSizeInBytes>0x7FFFFFFFl) {188dataSizeInBytes=UNKNOWN_SIZE;189}190int auType = auFileFormat.getAuType();191int sampleRate = (int)format.getSampleRate();192int channels = format.getChannels();193194// if we need to do any format conversion, we do it here.195//$$ fb 2001-07-13: Bug 4391108196audioStream = AudioSystem.getAudioInputStream(format, audioStream);197198final byte[] header;199try (ByteArrayOutputStream baos = new ByteArrayOutputStream();200DataOutputStream dos = new DataOutputStream(baos)) {201dos.writeInt(AuFileFormat.AU_SUN_MAGIC);202dos.writeInt(headerSize);203dos.writeInt((int) dataSizeInBytes);204dos.writeInt(auType);205dos.writeInt(sampleRate);206dos.writeInt(channels);207header = baos.toByteArray();208}209// Now create a new InputStream from headerStream and the InputStream210// in audioStream211return new SequenceInputStream(new ByteArrayInputStream(header),212new NoCloseInputStream(audioStream));213}214215private int writeAuFile(AudioInputStream in, AuFileFormat auFileFormat,216OutputStream out) throws IOException {217218int bytesRead = 0;219int bytesWritten = 0;220InputStream fileStream = getFileStream(auFileFormat, in);221byte[] buffer = new byte[bisBufferSize];222int maxLength = auFileFormat.getByteLength();223224while( (bytesRead = fileStream.read( buffer )) >= 0 ) {225if (maxLength>0) {226if( bytesRead < maxLength ) {227out.write( buffer, 0, bytesRead );228bytesWritten += bytesRead;229maxLength -= bytesRead;230} else {231out.write( buffer, 0, maxLength );232bytesWritten += maxLength;233maxLength = 0;234break;235}236} else {237out.write( buffer, 0, bytesRead );238bytesWritten += bytesRead;239}240}241242return bytesWritten;243}244}245246247