1 /* 2 * Copyright (c) 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 26 import javax.sound.sampled.AudioFileFormat; 27 import javax.sound.sampled.AudioFormat; 28 import javax.sound.sampled.AudioInputStream; 29 import javax.sound.sampled.AudioSystem; 30 31 /** 32 * @test 33 * @bug 8132782 34 */ 35 public final class RecognizeHugeWaveExtFiles { 36 37 /** 38 * The maximum size in bytes per WAVE specification. 39 */ 40 private static final /*unsigned int */ long MAX_UNSIGNED_INT = 0xffffffffL; 41 42 /** 43 * The supported wave ext format and sample size in bits. 44 */ 45 private static final int[][] waveTypeBits = { 46 {0xFFFE/*WAVE_FORMAT_EXTENSIBLE*/, 8} 47 }; 48 49 /** 50 * The list of supported sample rates(stored as unsigned int). 51 */ 52 private static final int[] sampleRates = { 53 8000, 11025, 16000, 22050, 32000, 37800, 44056, 44100, 47250, 48000, 54 50000, 50400, 88200, 96000, 176400, 192000, 352800, 2822400, 55 5644800, Integer.MAX_VALUE 56 }; 57 58 /** 59 * The list of supported channels (stored as unsigned int). 60 */ 61 private static final int[] channels = { 62 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 63 }; 64 65 /** 66 * The list of supported size of data (stored as unsigned int). 67 * <p> 68 * The {@code MAX_UNSIGNED_INT} is a maximum size. 69 */ 70 private static final long[] dataSizes = { 71 0, 1, 2, 3, Integer.MAX_VALUE - 1, Integer.MAX_VALUE, 72 (long) Integer.MAX_VALUE + 1, MAX_UNSIGNED_INT - 1, MAX_UNSIGNED_INT 73 }; 74 75 public static void main(final String[] args) throws Exception { 76 for (final int[] type : waveTypeBits) { 77 for (final int sampleRate : sampleRates) { 78 for (final int channel : channels) { 79 for (final long dataSize : dataSizes) { 80 testAFF(type, sampleRate, channel, dataSize); 81 testAIS(type, sampleRate, channel, dataSize); 82 } 83 } 84 } 85 } 86 } 87 88 /** 89 * Tests the {@code AudioFileFormat} fetched from the fake header. 90 * <p> 91 * Note that the frameLength and byteLength are stored as int which means 92 * that {@code AudioFileFormat} will store the data above {@code MAX_INT} as 93 * NOT_SPECIFIED. 94 */ 95 private static void testAFF(final int[] type, final int rate, 96 final int channel, final long size) 97 throws Exception { 98 final byte[] header = createHeader(type, rate, channel, size); 99 final ByteArrayInputStream fake = new ByteArrayInputStream(header); 100 final AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake); 101 final AudioFormat format = aff.getFormat(); 102 103 if (aff.getType() != AudioFileFormat.Type.WAVE) { 104 throw new RuntimeException("Error"); 105 } 106 107 final long frameLength = size / format.getFrameSize(); 108 if (frameLength <= Integer.MAX_VALUE) { 109 if (aff.getFrameLength() != frameLength) { 110 System.err.println("Expected: " + frameLength); 111 System.err.println("Actual: " + aff.getFrameLength()); 112 throw new RuntimeException(); 113 } 114 } else { 115 if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) { 116 System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED); 117 System.err.println("Actual: " + aff.getFrameLength()); 118 throw new RuntimeException(); 119 } 120 } 121 validateFormat(type[1], rate, channel, aff.getFormat()); 122 } 123 124 /** 125 * Tests the {@code AudioInputStream} fetched from the fake header. 126 * <p> 127 * Note that the frameLength is stored as long which means that {@code 128 * AudioInputStream} must store all possible data from au file. 129 */ 130 private static void testAIS(final int[] type, final int rate, 131 final int channel, final long size) 132 throws Exception { 133 final byte[] header = createHeader(type, rate, channel, size); 134 final ByteArrayInputStream fake = new ByteArrayInputStream(header); 135 final AudioInputStream ais = AudioSystem.getAudioInputStream(fake); 136 final AudioFormat format = ais.getFormat(); 137 final long frameLength = size / format.getFrameSize(); 138 if (frameLength != ais.getFrameLength()) { 139 System.err.println("Expected: " + frameLength); 140 System.err.println("Actual: " + ais.getFrameLength()); 141 throw new RuntimeException(); 142 } 143 if (ais.available() < 0) { 144 System.err.println("available should be >=0: " + ais.available()); 145 throw new RuntimeException(); 146 } 147 148 validateFormat(type[1], rate, channel, format); 149 } 150 151 /** 152 * Tests that format contains the same data as were provided to the fake 153 * stream. 154 */ 155 private static void validateFormat(final int bits, final int rate, 156 final int channel, 157 final AudioFormat format) { 158 159 if (Float.compare(format.getSampleRate(), rate) != 0) { 160 System.err.println("Expected: " + rate); 161 System.err.println("Actual: " + format.getSampleRate()); 162 throw new RuntimeException(); 163 } 164 if (format.getChannels() != channel) { 165 System.err.println("Expected: " + channel); 166 System.err.println("Actual: " + format.getChannels()); 167 throw new RuntimeException(); 168 } 169 if (format.getFrameSize() != ((bits + 7) / 8) * channel) { 170 System.err.println("Expected: " + (bits * channel + 1) / 8); 171 System.err.println("Actual: " + format.getFrameSize()); 172 throw new RuntimeException(); 173 } 174 } 175 176 /** 177 * Creates the custom header of the WAVE file. It is expected that all 178 * passed data are supported. 179 */ 180 private static byte[] createHeader(final int[] type, final int rate, 181 final int channel, final long size) { 182 final int frameSize = ((type[1] + 7) / 8) * channel; 183 return new byte[]{ 184 // RIFF_MAGIC 185 0x52, 0x49, 0x46, 0x46, 186 // fileLength 187 -1, -1, -1, -1, 188 // waveMagic 189 0x57, 0x41, 0x56, 0x45, 190 // FMT_MAGIC 191 0x66, 0x6d, 0x74, 0x20, 192 // size 193 40, 0, 0, 0, 194 // wav_type WAVE_FORMAT_EXTENSIBLE 195 (byte) (type[0]), (byte) (type[0] >> 8), 196 // channels 197 (byte) (channel), (byte) (channel >> 8), 198 // samplerate 199 (byte) (rate), (byte) (rate >> 8), (byte) (rate >> 16), 200 (byte) (rate >> 24), 201 // framerate 202 1, 0, 0, 0, 203 // framesize 204 (byte) (frameSize), (byte) (frameSize >> 8), 205 // bits 206 (byte) type[1], 0, 207 // cbsize 208 22, 0, 209 // validBitsPerSample 210 8, 0, 211 // channelMask 212 0, 0, 0, 0, 213 // SUBTYPE_IEEE_FLOAT 214 // i1 215 0x3, 0x0, 0x0, 0x0, 216 //s1 217 0x0, 0x0, 218 //s2 219 0x10, 0, 220 //x1 221 (byte) 0x80, 222 //x2 223 0x0, 224 //x3 225 0x0, 226 //x4 227 (byte) 0xaa, 228 //x5 229 0x0, 230 //x6 231 0x38, 232 //x7 233 (byte) 0x9b, 234 //x8 235 0x71, 236 // DATA_MAGIC 237 0x64, 0x61, 0x74, 0x61, 238 // data size 239 (byte) (size), (byte) (size >> 8), (byte) (size >> 16), 240 (byte) (size >> 24) 241 // data 242 , 0, 0, 0, 0, 0 243 }; 244 } 245 }