1 /*
   2  * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.sound;
  27 
  28 import java.io.File;
  29 import java.io.InputStream;
  30 import java.io.OutputStream;
  31 import java.io.IOException;
  32 
  33 import java.io.BufferedOutputStream;
  34 import java.io.DataOutputStream;
  35 import java.io.FileOutputStream;
  36 import java.io.ByteArrayInputStream;
  37 import java.io.ByteArrayOutputStream;
  38 import java.io.RandomAccessFile;
  39 import java.io.SequenceInputStream;
  40 import java.util.Objects;
  41 
  42 import javax.sound.sampled.AudioFileFormat;
  43 import javax.sound.sampled.AudioInputStream;
  44 import javax.sound.sampled.AudioFormat;
  45 import javax.sound.sampled.AudioSystem;
  46 
  47 
  48 /**
  49  * AU file writer.
  50  *
  51  * @author Jan Borgersen
  52  */
  53 public final class AuFileWriter extends SunFileWriter {
  54 
  55     //$$fb value for length field if length is not known
  56     public final static int UNKNOWN_SIZE=-1;
  57 
  58     /**
  59      * Constructs a new AuFileWriter object.
  60      */
  61     public AuFileWriter() {
  62         super(new AudioFileFormat.Type[]{AudioFileFormat.Type.AU});
  63     }
  64 
  65     public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
  66 
  67         AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
  68         System.arraycopy(types, 0, filetypes, 0, types.length);
  69 
  70         // make sure we can write this stream
  71         AudioFormat format = stream.getFormat();
  72         AudioFormat.Encoding encoding = format.getEncoding();
  73 
  74         if( (AudioFormat.Encoding.ALAW.equals(encoding)) ||
  75             (AudioFormat.Encoding.ULAW.equals(encoding)) ||
  76             (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) ||
  77             (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ) {
  78 
  79             return filetypes;
  80         }
  81 
  82         return new AudioFileFormat.Type[0];
  83     }
  84 
  85 
  86     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
  87         Objects.requireNonNull(stream);
  88         Objects.requireNonNull(fileType);
  89         Objects.requireNonNull(out);
  90 
  91         // we must know the total data length to calculate the file length
  92         //$$fb 2001-07-13: fix for bug 4351296: do not throw an exception
  93         //if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
  94         //      throw new IOException("stream length not specified");
  95         //}
  96 
  97         // throws IllegalArgumentException if not supported
  98         AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
  99 
 100         int bytesWritten = writeAuFile(stream, auFileFormat, out);
 101         return bytesWritten;
 102     }
 103 
 104 
 105 
 106     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
 107         Objects.requireNonNull(stream);
 108         Objects.requireNonNull(fileType);
 109         Objects.requireNonNull(out);
 110 
 111         // throws IllegalArgumentException if not supported
 112         AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
 113 
 114         // first write the file without worrying about length fields
 115         FileOutputStream fos = new FileOutputStream( out );     // throws IOException
 116         BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
 117         int bytesWritten = writeAuFile(stream, auFileFormat, bos );
 118         bos.close();
 119 
 120         // now, if length fields were not specified, calculate them,
 121         // open as a random access file, write the appropriate fields,
 122         // close again....
 123         if( auFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
 124 
 125             // $$kk: 10.22.99: jan: please either implement this or throw an exception!
 126             // $$fb: 2001-07-13: done. Fixes Bug 4479981
 127             RandomAccessFile raf=new RandomAccessFile(out, "rw");
 128             if (raf.length()<=0x7FFFFFFFl) {
 129                 // skip AU magic and data offset field
 130                 raf.skipBytes(8);
 131                 raf.writeInt(bytesWritten-AuFileFormat.AU_HEADERSIZE);
 132                 // that's all
 133             }
 134             raf.close();
 135         }
 136 
 137         return bytesWritten;
 138     }
 139 
 140 
 141     // -------------------------------------------------------------
 142 
 143     /**
 144      * Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.
 145      * Throws IllegalArgumentException if not supported.
 146      */
 147     private AudioFileFormat getAudioFileFormat(AudioFileFormat.Type type, AudioInputStream stream) {
 148 
 149         AudioFormat format = null;
 150         AuFileFormat fileFormat = null;
 151         AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
 152 
 153         AudioFormat streamFormat = stream.getFormat();
 154         AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
 155 
 156 
 157         float sampleRate;
 158         int sampleSizeInBits;
 159         int channels;
 160         int frameSize;
 161         float frameRate;
 162         int fileSize;
 163 
 164         if( !types[0].equals(type) ) {
 165             throw new IllegalArgumentException("File type " + type + " not supported.");
 166         }
 167 
 168         if( (AudioFormat.Encoding.ALAW.equals(streamEncoding)) ||
 169             (AudioFormat.Encoding.ULAW.equals(streamEncoding)) ) {
 170 
 171             encoding = streamEncoding;
 172             sampleSizeInBits = streamFormat.getSampleSizeInBits();
 173 
 174         } else if ( streamFormat.getSampleSizeInBits()==8 ) {
 175 
 176             encoding = AudioFormat.Encoding.PCM_SIGNED;
 177             sampleSizeInBits=8;
 178 
 179         } else {
 180 
 181             encoding = AudioFormat.Encoding.PCM_SIGNED;
 182             sampleSizeInBits=streamFormat.getSampleSizeInBits();
 183         }
 184 
 185 
 186         format = new AudioFormat( encoding,
 187                                   streamFormat.getSampleRate(),
 188                                   sampleSizeInBits,
 189                                   streamFormat.getChannels(),
 190                                   streamFormat.getFrameSize(),
 191                                   streamFormat.getFrameRate(),
 192                                   true);        // AU is always big endian
 193 
 194 
 195         if( stream.getFrameLength()!=AudioSystem.NOT_SPECIFIED ) {
 196             fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AuFileFormat.AU_HEADERSIZE;
 197         } else {
 198             fileSize = AudioSystem.NOT_SPECIFIED;
 199         }
 200 
 201         fileFormat = new AuFileFormat( AudioFileFormat.Type.AU,
 202                                        fileSize,
 203                                        format,
 204                                        (int)stream.getFrameLength() );
 205 
 206         return fileFormat;
 207     }
 208 
 209 
 210     private InputStream getFileStream(AuFileFormat auFileFormat, InputStream audioStream) throws IOException {
 211 
 212         // private method ... assumes auFileFormat is a supported file type
 213 
 214         AudioFormat format            = auFileFormat.getFormat();
 215 
 216         int magic          = AuFileFormat.AU_SUN_MAGIC;
 217         int headerSize     = AuFileFormat.AU_HEADERSIZE;
 218         long dataSize       = auFileFormat.getFrameLength();
 219         //$$fb fix for Bug 4351296
 220         //int dataSizeInBytes = dataSize * format.getFrameSize();
 221         long dataSizeInBytes = (dataSize==AudioSystem.NOT_SPECIFIED)?UNKNOWN_SIZE:dataSize * format.getFrameSize();
 222         if (dataSizeInBytes>0x7FFFFFFFl) {
 223             dataSizeInBytes=UNKNOWN_SIZE;
 224         }
 225         int encoding_local = auFileFormat.getAuType();
 226         int sampleRate     = (int)format.getSampleRate();
 227         int channels       = format.getChannels();
 228         //$$fb below is the fix for 4297100.
 229         //boolean bigendian      = format.isBigEndian();
 230         boolean bigendian      = true;                  // force bigendian
 231 
 232         byte header[] = null;
 233         ByteArrayInputStream headerStream = null;
 234         ByteArrayOutputStream baos = null;
 235         DataOutputStream dos = null;
 236         SequenceInputStream auStream = null;
 237 
 238         AudioFormat audioStreamFormat = null;
 239         AudioFormat.Encoding encoding = null;
 240         InputStream codedAudioStream = audioStream;
 241 
 242         // if we need to do any format conversion, do it here.
 243 
 244         codedAudioStream = audioStream;
 245 
 246         if( audioStream instanceof AudioInputStream ) {
 247 
 248 
 249             audioStreamFormat = ((AudioInputStream)audioStream).getFormat();
 250             encoding = audioStreamFormat.getEncoding();
 251 
 252             //$$ fb 2001-07-13: Bug 4391108
 253             if( (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ||
 254                 (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)
 255                  && bigendian != audioStreamFormat.isBigEndian()) ) {
 256 
 257                                 // plug in the transcoder to convert to PCM_SIGNED, bigendian
 258                                 // NOTE: little endian AU is not common, so we're always converting
 259                                 //       to big endian unless the passed in audioFileFormat is little.
 260                                 // $$fb this NOTE is superseded. We always write big endian au files, this is by far the standard.
 261                 codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat (
 262                                                                                      AudioFormat.Encoding.PCM_SIGNED,
 263                                                                                      audioStreamFormat.getSampleRate(),
 264                                                                                      audioStreamFormat.getSampleSizeInBits(),
 265                                                                                      audioStreamFormat.getChannels(),
 266                                                                                      audioStreamFormat.getFrameSize(),
 267                                                                                      audioStreamFormat.getFrameRate(),
 268                                                                                      bigendian),
 269                                                                     (AudioInputStream)audioStream );
 270 
 271 
 272             }
 273         }
 274 
 275         baos = new ByteArrayOutputStream();
 276         dos = new DataOutputStream(baos);
 277 
 278 
 279         if (bigendian) {
 280             dos.writeInt(AuFileFormat.AU_SUN_MAGIC);
 281             dos.writeInt(headerSize);
 282             dos.writeInt((int)dataSizeInBytes);
 283             dos.writeInt(encoding_local);
 284             dos.writeInt(sampleRate);
 285             dos.writeInt(channels);
 286         } else {
 287             dos.writeInt(AuFileFormat.AU_SUN_INV_MAGIC);
 288             dos.writeInt(big2little(headerSize));
 289             dos.writeInt(big2little((int)dataSizeInBytes));
 290             dos.writeInt(big2little(encoding_local));
 291             dos.writeInt(big2little(sampleRate));
 292             dos.writeInt(big2little(channels));
 293         }
 294 
 295         // Now create a new InputStream from headerStream and the InputStream
 296         // in audioStream
 297 
 298         dos.close();
 299         header = baos.toByteArray();
 300         headerStream = new ByteArrayInputStream( header );
 301         auStream = new SequenceInputStream(headerStream,
 302                         new NoCloseInputStream(codedAudioStream));
 303 
 304         return auStream;
 305     }
 306 
 307     private int writeAuFile(InputStream in, AuFileFormat auFileFormat, OutputStream out) throws IOException {
 308 
 309         int bytesRead = 0;
 310         int bytesWritten = 0;
 311         InputStream fileStream = getFileStream(auFileFormat, in);
 312         byte buffer[] = new byte[bisBufferSize];
 313         int maxLength = auFileFormat.getByteLength();
 314 
 315         while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
 316             if (maxLength>0) {
 317                 if( bytesRead < maxLength ) {
 318                     out.write( buffer, 0, bytesRead );
 319                     bytesWritten += bytesRead;
 320                     maxLength -= bytesRead;
 321                 } else {
 322                     out.write( buffer, 0, maxLength );
 323                     bytesWritten += maxLength;
 324                     maxLength = 0;
 325                     break;
 326                 }
 327             } else {
 328                 out.write( buffer, 0, bytesRead );
 329                 bytesWritten += bytesRead;
 330             }
 331         }
 332 
 333         return bytesWritten;
 334     }
 335 
 336 
 337 }