title: Convert audio stream to WAV byte array in Java without temp file
body: <p>Given an <code>InputStream</code> called <code>in</code> which contains audio data in a compressed format (such as MP3 or OGG), I wish to create a <code>byte</code> array containing a WAV conversion of the input data. Unfortunately, if you try to do this, JavaSound hands you the following error:</p>
  
  <pre><code>java.io.IOException: stream length not specified
  </code></pre>
  
  <p>I managed to get it to work by writing the wav to a temporary file, then reading it back in, as shown below:</p>
  
  <pre><code>AudioInputStream source = AudioSystem.getAudioInputStream(new BufferedInputStream(in, 1024));
  AudioInputStream pcm = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, source);
  AudioInputStream ulaw = AudioSystem.getAudioInputStream(AudioFormat.Encoding.ULAW, pcm);
  File tempFile = File.createTempFile("wav", "tmp");
  AudioSystem.write(ulaw, AudioFileFormat.Type.WAVE, tempFile);
  // The fileToByteArray() method reads the file
  // into a byte array; omitted for brevity
  byte[] bytes = fileToByteArray(tempFile);
  tempFile.delete();
  return bytes;
  </code></pre>
  
  <p>This is obviously less desirable. Is there a better way?</p>