FlacAudioFileReader extends AudioFileReader { public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException { InputStream inputStream = new FileInputStream(file); try { return getAudioInputStream(inputStream, (int) file.length()); } catch (UnsupportedAudioFileException e) { try { inputStream.close(); } catch (IOException e1) { e1.printStackTrace(); } throw e; } catch (IOException e) { try { inputStream.close(); } catch (IOException e1) { e1.printStackTrace(); } throw e; } }  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 testGetAudioInputStreamWithFlacFile() throws UnsupportedAudioFileException, IOException { final FlacAudioFileReader flacAudioFileReader = new FlacAudioFileReader(); final AudioInputStream stream = flacAudioFileReader.getAudioInputStream(getFlacTestFile("cymbals.flac")); assertNotNull(stream); }
@Test public void testGetAudioInputStreamWithBufferedFlacStream() 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 AudioInputStream stream = flacAudioFileReader.getAudioInputStream(in); assertNotNull(stream); final AudioFormat format = stream.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 testGetAudioInputStreamWithUnbufferedFlacStream() 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.getAudioInputStream(in); fail("Expected an IOException, because the stream didn't support mark. See AudioSystem#getAudioInputStream(InputStream stream) javadocs for contract"); } catch (IOException e) { } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
@Test public void testGetAudioInputStreamWithUnsupportedFile() throws IOException { final FlacAudioFileReader flacAudioFileReader = new FlacAudioFileReader(); final File file = File.createTempFile("flacTest", ".wav"); final OutputStream out = new FileOutputStream(file); out.write(new byte[2048]); out.close(); try { flacAudioFileReader.getAudioInputStream(file); fail("Expected UnsupportedAudioFileException"); } catch (UnsupportedAudioFileException e) { } finally { file.delete(); } }
@Test public void testGetAudioFileFormatWithUnsupportedFile() throws IOException { final FlacAudioFileReader flacAudioFileReader = new FlacAudioFileReader(); final File file = File.createTempFile("flacTest", ".wav"); final OutputStream out = new FileOutputStream(file); out.write(new byte[2048]); out.close(); try { flacAudioFileReader.getAudioInputStream(file); fail("Expected UnsupportedAudioFileException"); } catch (UnsupportedAudioFileException e) { } finally { file.delete(); } }