1 /*
   2  * Copyright (c) 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.
   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.IOException;
  26 import java.io.InputStream;
  27 
  28 import javax.sound.sampled.AudioSystem;
  29 import javax.sound.sampled.UnsupportedAudioFileException;
  30 
  31 /**
  32  * @test
  33  * @bug 8133677
  34  * @summary Subsequent read from the same stream should work
  35  */
  36 public final class RepeatedFormatReader {
  37 
  38     // Stubs
  39 
  40     private static byte[] headerMIDI = {0x4d, 0x54, 0x68, 0x64, // MThd
  41                                         0, 0, 0, 6, // read header length
  42                                         0, 0, // type
  43                                         0, 0, // numtracks
  44                                         0, 1, // timing
  45     };
  46 
  47     private static byte[] headerAU = {0x2e, 0x73, 0x6e, 0x64, // AU_SUN_MAGIC
  48                                       0, 0, 0, 0, // headerSize
  49                                       0, 0, 0, 0, // dataSize
  50                                       0, 0, 0, 1, // encoding
  51                                       0, 0, 0, 0, // sampleRate
  52                                       0, 0, 0, 1  // channels
  53     };
  54 
  55     private static byte[] headerWAV = {0x52, 0x49, 0x46, 0x46, // RIFF_MAGIC
  56                                        1, 1, 1, 1, // fileLength
  57                                        0x57, 0x41, 0x56, 0x45, //  waveMagic
  58                                        0x66, 0x6d, 0x74, 0x20, // FMT_MAGIC
  59                                        3, 0, 0, 0, // length
  60                                        1, 0, // wav_type  WAVE_FORMAT_PCM
  61                                        0, 1, // channels
  62                                        0, 0, 0, 0, // sampleRate
  63                                        0, 0, 0, 0, // avgBytesPerSec
  64                                        0, 0, // blockAlign
  65                                        1, 0, // sampleSizeInBits
  66                                        0x64, 0x61, 0x74, 0x61, // DATA_MAGIC
  67                                        0, 0, 0, 0, // dataLength
  68     };
  69 
  70     private static final byte[][] data = {headerMIDI, headerAU, headerWAV};
  71 
  72     public static void main(final String[] args)
  73             throws IOException, UnsupportedAudioFileException {
  74         for (final byte[] bytes : data) {
  75             test(bytes);
  76         }
  77     }
  78 
  79     private static void test(final byte[] buffer)
  80             throws IOException, UnsupportedAudioFileException {
  81         final InputStream is = new ByteArrayInputStream(buffer);
  82         for (int i = 0; i < 10; ++i) {
  83             AudioSystem.getAudioFileFormat(is);
  84         }
  85     }
  86 }