jdk/test/javax/sound/sampled/spi/AudioFileWriter/WaveBigEndian.java
changeset 41905 e8e5df013c6e
equal deleted inserted replaced
41904:524d908e49ea 41905:e8e5df013c6e
       
     1 /*
       
     2  * Copyright (c) 2006, 2016, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.io.ByteArrayInputStream;
       
    25 import java.io.ByteArrayOutputStream;
       
    26 import java.io.InputStream;
       
    27 
       
    28 import javax.sound.sampled.AudioFileFormat;
       
    29 import javax.sound.sampled.AudioFormat;
       
    30 import javax.sound.sampled.AudioInputStream;
       
    31 import javax.sound.sampled.AudioSystem;
       
    32 
       
    33 /**
       
    34  * @test
       
    35  * @bug 5001952
       
    36  * @summary Writing WAVE with big endian data produces corrupt file. WAVE should
       
    37  *          always write signed 16-bit, little endian, regardless of the
       
    38  *          endianness of the input data.
       
    39  */
       
    40 public class WaveBigEndian {
       
    41 
       
    42     static boolean failed = false;
       
    43 
       
    44     public static byte[] writeDataAndGetAIS(boolean bigEndian) throws Exception {
       
    45         if (bigEndian) {
       
    46                 out("Create WAVE file from big endian data...");
       
    47         } else {
       
    48                 out("Create WAVE file from little endian data...");
       
    49         }
       
    50         byte[] data = new byte[3000];
       
    51         for (int i = 0; i < data.length; i+=2) {
       
    52                 if (bigEndian) {
       
    53                         data[i] = (byte) i;
       
    54                         data[i+1] = (byte) (i+1);
       
    55                 } else {
       
    56                         data[i] = (byte) (i+1);
       
    57                         data[i+1] = (byte) i;
       
    58                 }
       
    59         }
       
    60         AudioFormat format = new AudioFormat(44100.0f, 16, 1, true, bigEndian);
       
    61         InputStream is = new ByteArrayInputStream(data);
       
    62         AudioInputStream ais = new AudioInputStream(is, format, data.length);
       
    63         ByteArrayOutputStream os = new ByteArrayOutputStream();
       
    64         int written = AudioSystem.write(ais, AudioFileFormat.Type.WAVE, os);
       
    65         data = os.toByteArray();
       
    66         out("Wrote "+written+" bytes, got "+data.length+" bytes in written file.");
       
    67         is = new ByteArrayInputStream(data);
       
    68         ais = AudioSystem.getAudioInputStream(is);
       
    69         out("Got AIS with length = "+ais.getFrameLength()+" frames.");
       
    70         return data;
       
    71     }
       
    72 
       
    73 
       
    74     public static void main(String args[]) throws Exception {
       
    75         byte[] data1 = writeDataAndGetAIS(false);
       
    76         byte[] data2 = writeDataAndGetAIS(true);
       
    77 
       
    78         if (data1.length != data2.length) {
       
    79                 out("# data1.length != data2.length!");
       
    80                 failed = true;
       
    81         } else {
       
    82                 for (int i = 0 ; i < data1.length; i++) {
       
    83                         if (data1[i] != data2[i]) {
       
    84                                 out("# At index "+i+": le="+(data1[i] & 0xFF)+" be="+(data2[i] & 0xFF)+" !");
       
    85                                 failed = true;
       
    86                         }
       
    87                 }
       
    88         }
       
    89 
       
    90         if (failed) throw new Exception("Test FAILED!");
       
    91         out("Files are identical.");
       
    92         out("test passed");
       
    93     }
       
    94 
       
    95     static void out(String s) {
       
    96         System.out.println(s);
       
    97     }
       
    98 }