1 /*
   2  * Copyright (c) 2002, 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.File;
  26 import java.io.FileInputStream;
  27 import java.io.InputStream;
  28 
  29 import javax.sound.sampled.AudioFileFormat;
  30 import javax.sound.sampled.AudioSystem;
  31 
  32 /**
  33  * @test
  34  * @bug 4629669
  35  * @summary AU file reader: problems with empty files
  36  */
  37 public class AuZeroLength {
  38 
  39     public static String getString(byte b) {
  40         //String res = Integer.toHexString(b & 0xFF).toUpperCase();
  41         //while (res.length()<2) res="0"+res;
  42         //return res;
  43         return String.valueOf(b);
  44     }
  45 
  46 
  47     public static void printFile(String filename) throws Exception {
  48         File file = new File(filename);
  49         FileInputStream fis = new FileInputStream(file);
  50         byte[] data = new byte[(int) file.length()];
  51         fis.read(data);
  52         String s = "";
  53         for (int i=0; i<data.length; i++) {
  54             s+=getString(data[i])+", ";
  55             if (s.length()>72) {
  56                 System.out.println(s);
  57                 s="";
  58             }
  59         }
  60         System.out.println(s);
  61     }
  62 
  63     public static void test(byte[] file) throws Exception {
  64         InputStream inputStream = new ByteArrayInputStream(file);
  65         AudioFileFormat aff = AudioSystem.getAudioFileFormat(inputStream);
  66 
  67         if (aff.getFrameLength() != 0) {
  68             throw new Exception("File length is "+aff.getFrameLength()+" instead of 0. test FAILED");
  69         }
  70         System.out.println(aff.getType()+" file length is 0.");
  71     }
  72 
  73     public static void main(String[] args) throws Exception {
  74         test(ZERO_AU);
  75         test(ZERO_WAV);
  76         test(ZERO_AIFF);
  77 
  78         System.out.println("Test passed.");
  79     }
  80 
  81     public static byte[] ZERO_AU = {
  82         46, 115, 110, 100, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, -84, 68, 0,
  83         0, 0, 1, 116, 101, 115, 116, 46, 119, 97, 118
  84     };
  85 
  86     public static byte[] ZERO_WAV = {
  87         82, 73, 70, 70, 36, 0, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32, 16, 0, 0,
  88         0, 1, 0, 1, 0, 68, -84, 0, 0, -120, 88, 1, 0, 2, 0, 16, 0, 100, 97, 116,
  89         97, 0, 0, 0, 0
  90     };
  91 
  92     public static byte[] ZERO_AIFF = {
  93         70, 79, 82, 77, 0, 0, 0, 46, 65, 73, 70, 70, 67, 79, 77, 77, 0, 0, 0, 18,
  94         0, 1, 0, 0, 0, 0, 0, 16, 64, 14, -84, 68, 0, 0, 0, 0, 0, 0, 83, 83, 78, 68,
  95         0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0
  96     };
  97 
  98 }