< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/AiffFileWriter.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2014, 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 
  41 import javax.sound.sampled.AudioFileFormat;
  42 import javax.sound.sampled.AudioInputStream;
  43 import javax.sound.sampled.AudioFormat;
  44 import javax.sound.sampled.AudioSystem;
  45 
  46 //$$fb this class is buggy. Should be replaced in future.
  47 
  48 /**
  49  * AIFF file writer.
  50  *
  51  * @author Jan Borgersen
  52  */
  53 public final class AiffFileWriter extends SunFileWriter {
  54 
  55     /**
  56      * Constructs a new AiffFileWriter object.
  57      */
  58     public AiffFileWriter() {
  59         super(new AudioFileFormat.Type[]{AudioFileFormat.Type.AIFF});


  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 
  88         //$$fb the following check must come first ! Otherwise
  89         // the next frame length check may throw an IOException and
  90         // interrupt iterating File Writers. (see bug 4351296)
  91 
  92         // throws IllegalArgumentException if not supported
  93         AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);
  94 
  95         // we must know the total data length to calculate the file length
  96         if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
  97             throw new IOException("stream length not specified");
  98         }
  99 
 100         int bytesWritten = writeAiffFile(stream, aiffFileFormat, out);
 101         return bytesWritten;
 102     }
 103 
 104 
 105     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {



 106 
 107         // throws IllegalArgumentException if not supported
 108         AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);
 109 
 110         // first write the file without worrying about length fields
 111         FileOutputStream fos = new FileOutputStream( out );     // throws IOException
 112         BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
 113         int bytesWritten = writeAiffFile(stream, aiffFileFormat, bos );
 114         bos.close();
 115 
 116         // now, if length fields were not specified, calculate them,
 117         // open as a random access file, write the appropriate fields,
 118         // close again....
 119         if( aiffFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
 120 
 121             // $$kk: 10.22.99: jan: please either implement this or throw an exception!
 122             // $$fb: 2001-07-13: done. Fixes Bug 4479981
 123             int ssndBlockSize           = (aiffFileFormat.getFormat().getChannels() * aiffFileFormat.getFormat().getSampleSizeInBits());
 124 
 125             int aiffLength=bytesWritten;


   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 //$$fb this class is buggy. Should be replaced in future.
  48 
  49 /**
  50  * AIFF file writer.
  51  *
  52  * @author Jan Borgersen
  53  */
  54 public final class AiffFileWriter extends SunFileWriter {
  55 
  56     /**
  57      * Constructs a new AiffFileWriter object.
  58      */
  59     public AiffFileWriter() {
  60         super(new AudioFileFormat.Type[]{AudioFileFormat.Type.AIFF});


  68         AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
  69         System.arraycopy(types, 0, filetypes, 0, types.length);
  70 
  71         // make sure we can write this stream
  72         AudioFormat format = stream.getFormat();
  73         AudioFormat.Encoding encoding = format.getEncoding();
  74 
  75         if( (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 
  80             return filetypes;
  81         }
  82 
  83         return new AudioFileFormat.Type[0];
  84     }
  85 
  86 
  87     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
  88         Objects.requireNonNull(stream);
  89         Objects.requireNonNull(fileType);
  90         Objects.requireNonNull(out);
  91 
  92         //$$fb the following check must come first ! Otherwise
  93         // the next frame length check may throw an IOException and
  94         // interrupt iterating File Writers. (see bug 4351296)
  95 
  96         // throws IllegalArgumentException if not supported
  97         AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);
  98 
  99         // we must know the total data length to calculate the file length
 100         if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
 101             throw new IOException("stream length not specified");
 102         }
 103 
 104         int bytesWritten = writeAiffFile(stream, aiffFileFormat, out);
 105         return bytesWritten;
 106     }
 107 
 108 
 109     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
 110         Objects.requireNonNull(stream);
 111         Objects.requireNonNull(fileType);
 112         Objects.requireNonNull(out);
 113 
 114         // throws IllegalArgumentException if not supported
 115         AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);
 116 
 117         // first write the file without worrying about length fields
 118         FileOutputStream fos = new FileOutputStream( out );     // throws IOException
 119         BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
 120         int bytesWritten = writeAiffFile(stream, aiffFileFormat, bos );
 121         bos.close();
 122 
 123         // now, if length fields were not specified, calculate them,
 124         // open as a random access file, write the appropriate fields,
 125         // close again....
 126         if( aiffFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
 127 
 128             // $$kk: 10.22.99: jan: please either implement this or throw an exception!
 129             // $$fb: 2001-07-13: done. Fixes Bug 4479981
 130             int ssndBlockSize           = (aiffFileFormat.getFormat().getChannels() * aiffFileFormat.getFormat().getSampleSizeInBits());
 131 
 132             int aiffLength=bytesWritten;


< prev index next >