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 RecognizeHugeWaveFloatFiles { 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 pcm_float format and sample size in bits. 44 */ 45 private static final byte[][] waveTypeBits = { 46 {0x0003/*WAVE_FORMAT_IEEE_FLOAT*/, 32} 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). 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 byte[] 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 byte 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 byte[] 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 | 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 6729836 34 */ 35 public final class RecognizeHugeWaveFiles { 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 pcm_float format and sample size in bits. 44 */ 45 private static final byte[][] waveTypeBits = { 46 {0x0001/*WAVE_FORMAT_PCM*/,1}, 47 {0x0001/*WAVE_FORMAT_PCM*/,2}, 48 {0x0001/*WAVE_FORMAT_PCM*/,4}, 49 {0x0001/*WAVE_FORMAT_PCM*/,8}, 50 {0x0001/*WAVE_FORMAT_PCM*/,16}, 51 {0x0001/*WAVE_FORMAT_PCM*/,20}, 52 {0x0001/*WAVE_FORMAT_PCM*/,24}, 53 {0x0001/*WAVE_FORMAT_PCM*/,32}, 54 {0x0003/*WAVE_FORMAT_IEEE_FLOAT*/, 32}, 55 {0x0006/*WAVE_FORMAT_ALAW*/, 8}, 56 {0x0007/*WAVE_FORMAT_MULAW*/, 8} 57 }; 58 59 /** 60 * The list of supported sample rates(stored as unsigned int). 61 */ 62 private static final int[] sampleRates = { 63 8000, 11025, 16000, 22050, 32000, 37800, 44056, 44100, 47250, 48000, 64 50000, 50400, 88200, 96000, 176400, 192000, 352800, 2822400, 65 5644800, Integer.MAX_VALUE 66 }; 67 68 /** 69 * The list of supported channels (stored as unsigned int). 70 */ 71 private static final int[] channels = { 72 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 73 }; 74 75 /** 76 * The list of supported size of data (stored as unsigned int). 118 if (frameLength <= Integer.MAX_VALUE) { 119 if (aff.getFrameLength() != frameLength) { 120 System.err.println("Expected: " + frameLength); 121 System.err.println("Actual: " + aff.getFrameLength()); 122 throw new RuntimeException(); 123 } 124 } else { 125 if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) { 126 System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED); 127 System.err.println("Actual: " + aff.getFrameLength()); 128 throw new RuntimeException(); 129 } 130 } 131 validateFormat(type[1], rate, channel, aff.getFormat()); 132 } 133 134 /** 135 * Tests the {@code AudioInputStream} fetched from the fake header. 136 * <p> 137 * Note that the frameLength is stored as long which means that {@code 138 * AudioInputStream} must store all possible data from wave file. 139 */ 140 private static void testAIS(final byte[] type, final int rate, 141 final int channel, final long size) 142 throws Exception { 143 final byte[] header = createHeader(type, rate, channel, size); 144 final ByteArrayInputStream fake = new ByteArrayInputStream(header); 145 final AudioInputStream ais = AudioSystem.getAudioInputStream(fake); 146 final AudioFormat format = ais.getFormat(); 147 final long frameLength = size / format.getFrameSize(); 148 if (frameLength != ais.getFrameLength()) { 149 System.err.println("Expected: " + frameLength); 150 System.err.println("Actual: " + ais.getFrameLength()); 151 throw new RuntimeException(); 152 } 153 if (ais.available() < 0) { 154 System.err.println("available should be >=0: " + ais.available()); 155 throw new RuntimeException(); 156 } 157 158 validateFormat(type[1], rate, channel, format); 159 } 160 161 /** 162 * Tests that format contains the same data as were provided to the fake 163 * stream. 164 */ 165 private static void validateFormat(final byte bits, final int rate, 166 final int channel, 167 final AudioFormat format) { 168 169 if (Float.compare(format.getSampleRate(), rate) != 0) { 170 System.err.println("Expected: " + rate); 171 System.err.println("Actual: " + format.getSampleRate()); 172 throw new RuntimeException(); 173 } 174 if (format.getChannels() != channel) { 175 System.err.println("Expected: " + channel); 176 System.err.println("Actual: " + format.getChannels()); 177 throw new RuntimeException(); 178 } 179 int frameSize = ((bits + 7) / 8) * channel; 180 if (format.getFrameSize() != frameSize) { 181 System.err.println("Expected: " + frameSize); 182 System.err.println("Actual: " + format.getFrameSize()); 183 throw new RuntimeException(); 184 } 185 } 186 187 /** 188 * Creates the custom header of the WAVE file. It is expected that all 189 * passed data are supported. 190 */ 191 private static byte[] createHeader(final byte[] type, final int rate, 192 final int channel, final long size) { 193 final int frameSize = ((type[1] + 7) / 8) * channel; 194 return new byte[]{ 195 // RIFF_MAGIC 196 0x52, 0x49, 0x46, 0x46, 197 // fileLength 198 -1, -1, -1, -1, 199 // waveMagic 200 0x57, 0x41, 0x56, 0x45, 201 // FMT_MAGIC |