8135088: Typo in AuFileReader
authorserb
Tue, 29 Dec 2015 16:43:17 +0300
changeset 35656 e5582272e65b
parent 35655 92d3ad496b67
child 35657 5ee7a31d7923
8135088: Typo in AuFileReader Reviewed-by: prr, amenkov
jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileFormat.java
jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java
jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java
jdk/src/java.desktop/share/classes/com/sun/media/sound/SunFileWriter.java
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileFormat.java	Tue Dec 29 16:41:34 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileFormat.java	Tue Dec 29 16:43:17 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,24 +25,18 @@
 
 package com.sun.media.sound;
 
-
 import javax.sound.sampled.AudioFileFormat;
 import javax.sound.sampled.AudioFormat;
 
-
 /**
  * AU file format.
  *
  * @author Jan Borgersen
  */
-
 final class AuFileFormat extends AudioFileFormat {
 
     // magic numbers
-    static final int AU_SUN_MAGIC =     0x2e736e64;
-    static final int AU_SUN_INV_MAGIC = 0x646e732e;
-    static final int AU_DEC_MAGIC =         0x2e736400;
-    static final int AU_DEC_INV_MAGIC = 0x0064732e;
+    static final int AU_SUN_MAGIC = 0x2e736e64; // ".snd"
 
     // encodings
     static final int AU_ULAW_8       = 1;  /* 8-bit ISDN u-law */
@@ -62,11 +56,6 @@
 
     private int auType;
 
-    AuFileFormat( AudioFileFormat aff ) {
-
-        this( aff.getType(), aff.getByteLength(), aff.getFormat(), aff.getFrameLength() );
-    }
-
     AuFileFormat(AudioFileFormat.Type type, int lengthInBytes, AudioFormat format, int lengthInFrames) {
 
         super(type,lengthInBytes,format,lengthInFrames);
@@ -94,12 +83,9 @@
                 auType = AU_LINEAR_32;
             }
         }
-
     }
 
     public int getAuType() {
-
         return auType;
     }
-
 }
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java	Tue Dec 29 16:41:34 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java	Tue Dec 29 16:43:17 2015 +0300
@@ -46,45 +46,25 @@
     @Override
     public AudioFileFormat getAudioFileFormatImpl(final InputStream stream)
             throws UnsupportedAudioFileException, IOException {
-        boolean bigendian  = false;
-        int headerSize     = -1;
-        int dataSize       = -1;
-        int encoding_local = -1;
-        int sampleRate     = -1;
-        int frameRate      = -1;
-        int frameSize      = -1;
-        int channels       = -1;
-        final int sampleSizeInBits;
-        int length = 0;
-        int nread = 0;
-        AudioFormat.Encoding encoding = null;
+        final DataInputStream dis = new DataInputStream(stream);
+        final int magic = dis.readInt();
 
-        DataInputStream dis = new DataInputStream( stream );
-
-        final int magic = dis.readInt(); nread += 4;
-
-        if (! (magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC) ||
-            (magic == AuFileFormat.AU_SUN_INV_MAGIC) || (magic == AuFileFormat.AU_DEC_INV_MAGIC) ) {
-
+        if (magic != AuFileFormat.AU_SUN_MAGIC) {
             // not AU, throw exception
             throw new UnsupportedAudioFileException("not an AU file");
         }
 
-        if ((magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC)) {
-            bigendian = true;        // otherwise little-endian
-        }
-
-        headerSize     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
-        dataSize       = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
-        encoding_local = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
-        sampleRate     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
-        channels       = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
+        final int headerSize = dis.readInt();
+        final int dataSize = dis.readInt();
+        final int encoding_local = dis.readInt();
+        final int sampleRate = dis.readInt();
+        final int channels = dis.readInt();
         if (channels <= 0) {
             throw new UnsupportedAudioFileException("Invalid number of channels");
         }
 
-        frameRate = sampleRate;
-
+        final int sampleSizeInBits;
+        final AudioFormat.Encoding encoding;
         switch (encoding_local) {
         case AuFileFormat.AU_ULAW_8:
             encoding = AudioFormat.Encoding.ULAW;
@@ -138,24 +118,24 @@
                         break;
             */
         default:
-                // unsupported filetype, throw exception
-                throw new UnsupportedAudioFileException("not a valid AU file");
+            // unsupported filetype, throw exception
+            throw new UnsupportedAudioFileException("not a valid AU file");
         }
 
-        frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
+        final int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
         //$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files
-        if( dataSize < 0 ) {
+        final int length;
+        if (dataSize < 0) {
             length = AudioSystem.NOT_SPECIFIED;
         } else {
             //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
             length = dataSize / frameSize;
         }
         // now seek past the header
-        dis.skipBytes(headerSize - nread);
-        AudioFormat format = new AudioFormat(encoding, sampleRate,
-                                             sampleSizeInBits, channels,
-                                             frameSize, (float) frameRate,
-                                             bigendian);
+        dis.skipBytes(headerSize - AuFileFormat.AU_HEADERSIZE);
+        final AudioFormat format = new AudioFormat(encoding, sampleRate,
+                                                   sampleSizeInBits, channels,
+                                                   frameSize, sampleRate, true);
         return new AuFileFormat(AudioFileFormat.Type.AU, dataSize + headerSize,
                                 format, length);
     }
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java	Tue Dec 29 16:41:34 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java	Tue Dec 29 16:43:17 2015 +0300
@@ -25,26 +25,24 @@
 
 package com.sun.media.sound;
 
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.IOException;
-
-import java.io.BufferedOutputStream;
-import java.io.DataOutputStream;
-import java.io.FileOutputStream;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.RandomAccessFile;
 import java.io.SequenceInputStream;
 import java.util.Objects;
 
 import javax.sound.sampled.AudioFileFormat;
+import javax.sound.sampled.AudioFormat;
 import javax.sound.sampled.AudioInputStream;
-import javax.sound.sampled.AudioFormat;
 import javax.sound.sampled.AudioSystem;
 
-
 /**
  * AU file writer.
  *
@@ -52,8 +50,10 @@
  */
 public final class AuFileWriter extends SunFileWriter {
 
-    //$$fb value for length field if length is not known
-    public static final int UNKNOWN_SIZE=-1;
+    /**
+     * Value for length field if length is not known.
+     */
+    private static final int UNKNOWN_SIZE = -1;
 
     /**
      * Constructs a new AuFileWriter object.
@@ -62,6 +62,7 @@
         super(new AudioFileFormat.Type[]{AudioFileFormat.Type.AU});
     }
 
+    @Override
     public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
 
         AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
@@ -82,7 +83,7 @@
         return new AudioFileFormat.Type[0];
     }
 
-
+    @Override
     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
         Objects.requireNonNull(stream);
         Objects.requireNonNull(fileType);
@@ -96,13 +97,10 @@
 
         // throws IllegalArgumentException if not supported
         AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
-
-        int bytesWritten = writeAuFile(stream, auFileFormat, out);
-        return bytesWritten;
+        return writeAuFile(stream, auFileFormat, out);
     }
 
-
-
+    @Override
     public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
         Objects.requireNonNull(stream);
         Objects.requireNonNull(fileType);
@@ -137,7 +135,6 @@
         return bytesWritten;
     }
 
-
     // -------------------------------------------------------------
 
     /**
@@ -154,11 +151,7 @@
         AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
 
 
-        float sampleRate;
         int sampleSizeInBits;
-        int channels;
-        int frameSize;
-        float frameRate;
         int fileSize;
 
         if( !types[0].equals(type) ) {
@@ -206,14 +199,12 @@
         return fileFormat;
     }
 
-
-    private InputStream getFileStream(AuFileFormat auFileFormat, InputStream audioStream) throws IOException {
+    private InputStream getFileStream(AuFileFormat auFileFormat, AudioInputStream audioStream) throws IOException {
 
         // private method ... assumes auFileFormat is a supported file type
 
         AudioFormat format            = auFileFormat.getFormat();
 
-        int magic          = AuFileFormat.AU_SUN_MAGIC;
         int headerSize     = AuFileFormat.AU_HEADERSIZE;
         long dataSize       = auFileFormat.getFrameLength();
         //$$fb fix for Bug 4351296
@@ -225,9 +216,6 @@
         int encoding_local = auFileFormat.getAuType();
         int sampleRate     = (int)format.getSampleRate();
         int channels       = format.getChannels();
-        //$$fb below is the fix for 4297100.
-        //boolean bigendian      = format.isBigEndian();
-        boolean bigendian      = true;                  // force bigendian
 
         byte header[] = null;
         ByteArrayInputStream headerStream = null;
@@ -243,54 +231,37 @@
 
         codedAudioStream = audioStream;
 
-        if( audioStream instanceof AudioInputStream ) {
+        audioStreamFormat = audioStream.getFormat();
+        encoding = audioStreamFormat.getEncoding();
+
+        //$$ fb 2001-07-13: Bug 4391108
+        if( (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ||
+            (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)
+             && !audioStreamFormat.isBigEndian()) ) {
+            // We always write big endian au files, this is by far the standard
+            codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat (
+                                                                                 AudioFormat.Encoding.PCM_SIGNED,
+                                                                                 audioStreamFormat.getSampleRate(),
+                                                                                 audioStreamFormat.getSampleSizeInBits(),
+                                                                                 audioStreamFormat.getChannels(),
+                                                                                 audioStreamFormat.getFrameSize(),
+                                                                                 audioStreamFormat.getFrameRate(),
+                                                                                 true),
+                                                                                 audioStream );
 
 
-            audioStreamFormat = ((AudioInputStream)audioStream).getFormat();
-            encoding = audioStreamFormat.getEncoding();
-
-            //$$ fb 2001-07-13: Bug 4391108
-            if( (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ||
-                (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)
-                 && bigendian != audioStreamFormat.isBigEndian()) ) {
-
-                                // plug in the transcoder to convert to PCM_SIGNED, bigendian
-                                // NOTE: little endian AU is not common, so we're always converting
-                                //       to big endian unless the passed in audioFileFormat is little.
-                                // $$fb this NOTE is superseded. We always write big endian au files, this is by far the standard.
-                codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat (
-                                                                                     AudioFormat.Encoding.PCM_SIGNED,
-                                                                                     audioStreamFormat.getSampleRate(),
-                                                                                     audioStreamFormat.getSampleSizeInBits(),
-                                                                                     audioStreamFormat.getChannels(),
-                                                                                     audioStreamFormat.getFrameSize(),
-                                                                                     audioStreamFormat.getFrameRate(),
-                                                                                     bigendian),
-                                                                    (AudioInputStream)audioStream );
-
-
-            }
         }
 
         baos = new ByteArrayOutputStream();
         dos = new DataOutputStream(baos);
 
 
-        if (bigendian) {
-            dos.writeInt(AuFileFormat.AU_SUN_MAGIC);
-            dos.writeInt(headerSize);
-            dos.writeInt((int)dataSizeInBytes);
-            dos.writeInt(encoding_local);
-            dos.writeInt(sampleRate);
-            dos.writeInt(channels);
-        } else {
-            dos.writeInt(AuFileFormat.AU_SUN_INV_MAGIC);
-            dos.writeInt(big2little(headerSize));
-            dos.writeInt(big2little((int)dataSizeInBytes));
-            dos.writeInt(big2little(encoding_local));
-            dos.writeInt(big2little(sampleRate));
-            dos.writeInt(big2little(channels));
-        }
+        dos.writeInt(AuFileFormat.AU_SUN_MAGIC);
+        dos.writeInt(headerSize);
+        dos.writeInt((int)dataSizeInBytes);
+        dos.writeInt(encoding_local);
+        dos.writeInt(sampleRate);
+        dos.writeInt(channels);
 
         // Now create a new InputStream from headerStream and the InputStream
         // in audioStream
@@ -304,7 +275,7 @@
         return auStream;
     }
 
-    private int writeAuFile(InputStream in, AuFileFormat auFileFormat, OutputStream out) throws IOException {
+    private int writeAuFile(AudioInputStream in, AuFileFormat auFileFormat, OutputStream out) throws IOException {
 
         int bytesRead = 0;
         int bytesWritten = 0;
@@ -332,6 +303,4 @@
 
         return bytesWritten;
     }
-
-
 }
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/SunFileWriter.java	Tue Dec 29 16:41:34 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/SunFileWriter.java	Tue Dec 29 16:43:17 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,19 +25,13 @@
 
 package com.sun.media.sound;
 
-import java.io.File;
+import java.io.DataInputStream;
+import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.IOException;
-import java.io.DataInputStream;
 
 import javax.sound.sampled.AudioFileFormat;
-import javax.sound.sampled.AudioInputStream;
 import javax.sound.sampled.spi.AudioFileWriter;
 
-
-
-
 /**
  * Abstract File Writer class.
  *
@@ -75,14 +69,6 @@
         return localArray;
     }
 
-
-    public abstract AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream);
-
-    public abstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException;
-
-    public abstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException;
-
-
     // HELPER METHODS