< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.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 
  47 /**
  48  * AU file writer.
  49  *
  50  * @author Jan Borgersen
  51  */
  52 public final class AuFileWriter extends SunFileWriter {
  53 
  54     //$$fb value for length field if length is not known
  55     public final static int UNKNOWN_SIZE=-1;
  56 
  57     /**
  58      * Constructs a new AuFileWriter object.
  59      */


  66         AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
  67         System.arraycopy(types, 0, filetypes, 0, types.length);
  68 
  69         // make sure we can write this stream
  70         AudioFormat format = stream.getFormat();
  71         AudioFormat.Encoding encoding = format.getEncoding();
  72 
  73         if( (AudioFormat.Encoding.ALAW.equals(encoding)) ||
  74             (AudioFormat.Encoding.ULAW.equals(encoding)) ||
  75             (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) ||
  76             (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ) {
  77 
  78             return filetypes;
  79         }
  80 
  81         return new AudioFileFormat.Type[0];
  82     }
  83 
  84 
  85     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {



  86 
  87         // we must know the total data length to calculate the file length
  88         //$$fb 2001-07-13: fix for bug 4351296: do not throw an exception
  89         //if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
  90         //      throw new IOException("stream length not specified");
  91         //}
  92 
  93         // throws IllegalArgumentException if not supported
  94         AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
  95 
  96         int bytesWritten = writeAuFile(stream, auFileFormat, out);
  97         return bytesWritten;
  98     }
  99 
 100 
 101 
 102     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {



 103 
 104         // throws IllegalArgumentException if not supported
 105         AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
 106 
 107         // first write the file without worrying about length fields
 108         FileOutputStream fos = new FileOutputStream( out );     // throws IOException
 109         BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
 110         int bytesWritten = writeAuFile(stream, auFileFormat, bos );
 111         bos.close();
 112 
 113         // now, if length fields were not specified, calculate them,
 114         // open as a random access file, write the appropriate fields,
 115         // close again....
 116         if( auFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
 117 
 118             // $$kk: 10.22.99: jan: please either implement this or throw an exception!
 119             // $$fb: 2001-07-13: done. Fixes Bug 4479981
 120             RandomAccessFile raf=new RandomAccessFile(out, "rw");
 121             if (raf.length()<=0x7FFFFFFFl) {
 122                 // skip AU magic and data offset field


   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      */


  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


< prev index next >