jdk/test/javax/sound/sampled/spi/AudioFileWriter/RIFFHeader.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 4636355
       
    36  * @summary Check that RIFF headers are written with extra data length field.
       
    37  */
       
    38 public class RIFFHeader {
       
    39 
       
    40     public static void main(String args[]) throws Exception {
       
    41         System.out.println();
       
    42         System.out.println();
       
    43         System.out.println("4636355: Check that RIFF headers are written with extra data length field.");
       
    44         byte[] fakedata=new byte[1234];
       
    45         MyByteArrayInputStream is = new MyByteArrayInputStream(fakedata);
       
    46         AudioFormat inFormat = new AudioFormat(AudioFormat.Encoding.ULAW, 8000, 8, 1, 1, 8000, true);
       
    47 
       
    48         AudioInputStream ais = new AudioInputStream((InputStream) is, inFormat, fakedata.length);
       
    49         ByteArrayOutputStream out = new ByteArrayOutputStream(1500);
       
    50         System.out.println("  ulaw data will be written as WAVE to stream...");
       
    51         int t = AudioSystem.write(ais, AudioFileFormat.Type.WAVE, out);
       
    52         byte[] writtenData = out.toByteArray();
       
    53         // now header must have at least 46 bytes
       
    54         System.out.println("  Length should be "+(fakedata.length+46)+" bytes: "+writtenData.length);
       
    55         // re-read this file
       
    56         is = new MyByteArrayInputStream(writtenData);
       
    57         System.out.println("  Get AudioFileFormat of written file");
       
    58         AudioFileFormat fileformat = AudioSystem.getAudioFileFormat(is);
       
    59         AudioFileFormat.Type type = fileformat.getType();
       
    60         System.out.println("  The file format type: "+type);
       
    61         if (fileformat.getFrameLength()!=fakedata.length
       
    62             && fileformat.getFrameLength()!=AudioSystem.NOT_SPECIFIED) {
       
    63             throw new Exception("The written file's frame length is "+fileformat.getFrameLength()+" but should be "+fakedata.length+" !");
       
    64         }
       
    65         ais = AudioSystem.getAudioInputStream(is);
       
    66         System.out.println("  Got Stream with format: "+ais.getFormat());
       
    67         if (is.getPos()<46) {
       
    68             throw new Exception("After reading the header, stream position must be at least 46, but is "+is.getPos()+" !");
       
    69         }
       
    70         System.out.println("  test passed.");
       
    71     }
       
    72 
       
    73     static class MyByteArrayInputStream extends ByteArrayInputStream {
       
    74 
       
    75         MyByteArrayInputStream(byte[] data) {
       
    76             super(data);
       
    77         }
       
    78 
       
    79         int getPos() {
       
    80             return pos;
       
    81         }
       
    82     }
       
    83 }