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 | 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 wave 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 int frameSize = ((bits + 7) / 8) * channel; 170 if (format.getFrameSize() != frameSize) { 171 System.err.println("Expected: " + frameSize); 172 System.err.println("Actual: " + format.getFrameSize()); 173 throw new RuntimeException(); 174 } 175 } 176 177 /** 178 * Creates the custom header of the WAVE file. It is expected that all 179 * passed data are supported. 180 */ 181 private static byte[] createHeader(final int[] type, final int rate, 182 final int channel, final long size) { 183 final int frameSize = ((type[1] + 7) / 8) * channel; 184 return new byte[]{ 185 // RIFF_MAGIC 186 0x52, 0x49, 0x46, 0x46, 187 // fileLength 188 -1, -1, -1, -1, 189 // waveMagic 190 0x57, 0x41, 0x56, 0x45, 191 // FMT_MAGIC |