How can I write the contents of a SourceDataLine to a file?

I am modifying an application that plays audio data to write the data to a file instead. As it is currently implemented, a byte array is filled dynamically, and the contents of this buffer are written to a SourceDataLine each time it is filled. I basically want to write that buffer out to a file in a specified format.

I have read through this official tutorial and came across this code snipped for writing audio data to a file:

File fileOut = new File(someNewPathName);
AudioFileFormat.Type fileType = fileFormat.getType();
if (AudioSystem.isFileTypeSupported(fileType, 
    audioInputStream)) {
  AudioSystem.write(audioInputStream, fileType, fileOut);
}

I see from the API documentation that I can construct an AudioInputStream using a TargetDataLine, however in my case I have a SourceDataLine. I don't know how to get the data from my byte array into the TargetDataLine since it implements the read() method instead of write(). Other uses of the AudioInputStream in that and other documentation treat it as a way of reading from a file, so I'm a little confused by its use with AudioSystem.write().

So, how can I get the data from a SourceDataLine, or from the buffer directly, into a TargetDataLine or AudioInputStream so that it can be written out to a file?


ANSWERS:


  1. Use the byte[] to establish a ByteArrayInputStream
  2. Provide the BAIS to AudioSystem.getAudioInputStream(InputStream)
  3. Use the AIS in AudioSystem.write(..)


 MORE:


 ? Java detecting an audio file (mp3)
 ? Java ogg to wav conversion
 ? List three different sound (ogg) files with ability to play on click?
 ? How to play Ogg files in Java?
 ? Playing multiple byte arrays simultaneously in Java
 ? Playing sound through SourceDataLine.write - How to use the byte[]-Buffer with double values
 ? First play of ogg file works, LineUnavailableException on second play
 ? First play of ogg file works, LineUnavailableException on second play
 ? First play of ogg file works, LineUnavailableException on second play
 ? LineUnavailableException for playing mp3 with java