FlacAudioFileReader extends AudioFileReader { public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException { InputStream inputStream = null; try { inputStream = new FileInputStream(file); return getAudioFileFormat(inputStream, (int) file.length()); } finally { if (inputStream != null) inputStream.close(); } }  AudioFileFormat getAudioFileFormat(File file); AudioFileFormat getAudioFileFormat(URL url); AudioFileFormat getAudioFileFormat(InputStream stream); AudioInputStream getAudioInputStream(File file); AudioInputStream getAudioInputStream(URL url); AudioInputStream getAudioInputStream(final InputStream stream); static final String KEY_DURATION; }
@Test public void testGetAudioFileFormatWithBufferedFlacStream() throws UnsupportedAudioFileException, IOException { final FlacAudioFileReader flacAudioFileReader = new FlacAudioFileReader(); final File flacTestFile = getFlacTestFile("cymbals.flac"); InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(flacTestFile)); assertTrue("For this test the stream MUST support mark()", in.markSupported()); final AudioFileFormat fileFormat = flacAudioFileReader.getAudioFileFormat(in); assertNotNull(fileFormat); final AudioFormat format = fileFormat.getFormat(); assertEquals(44100f, format.getSampleRate(), 0); assertEquals(16, format.getSampleSizeInBits()); assertEquals(2, format.getChannels()); assertEquals("FLAC", format.getEncoding().toString()); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
@Test public void testGetAudioFileFormatWithUnbufferedFlacStream() throws IOException, UnsupportedAudioFileException { final FlacAudioFileReader flacAudioFileReader = new FlacAudioFileReader(); final File flacTestFile = getFlacTestFile("cymbals.flac"); InputStream in = null; try { in = new FileInputStream(flacTestFile); assertFalse("For this test the stream MUST NOT support mark()", in.markSupported()); flacAudioFileReader.getAudioFileFormat(in); fail("Expected an IOException, because the stream didn't support mark. See AudioSystem#getAudioFileFormat(InputStream stream) javadocs for contract"); } catch (IOException e) { } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
@Test public void testGetAudioFileFormatWithFlacFile() throws UnsupportedAudioFileException, IOException { final FlacAudioFileReader flacAudioFileReader = new FlacAudioFileReader(); final AudioFileFormat audioFileFormat = flacAudioFileReader.getAudioFileFormat(getFlacTestFile("cymbals.flac")); assertNotNull(audioFileFormat); assertEquals("flac", audioFileFormat.getType().getExtension()); assertEquals(new Long(9338775), audioFileFormat.getProperty("duration")); assertEquals(411840, audioFileFormat.getFrameLength()); final AudioFormat format = audioFileFormat.getFormat(); assertEquals(44100f, format.getSampleRate(), 0); assertEquals(16, format.getSampleSizeInBits()); assertEquals(2, format.getChannels()); assertEquals("FLAC", format.getEncoding().toString()); }