title: Java: Extracting bytes from audio file
body: <p>What is the difference between the following two implementations in extracting the bytes of data from an audio file ?</p>
  
  <p>The file is a <code>.wav</code> file and i want to extract only the data, without headers or any other thing.</p>
  
  <p>Implementation 1:</p>
  
  <pre><code>public byte[] extractAudioFromFile(String filePath) {
      try {
          // Get an input stream on the byte array
          // containing the data
          File file = new File(filePath);
          final AudioInputStream audioInputStream = AudioSystem
                  .getAudioInputStream(file);
  
          byte[] buffer = new byte[4096];
          int counter;
          while ((counter = audioInputStream.read(buffer, 0, buffer.length)) != -1) {
              if (counter &gt; 0) {
                  byteOut.write(buffer, 0, counter);
              }
          }
          audioInputStream.close();
          byteOut.close();
      } catch (Exception e) {
          System.out.println(e);
          System.exit(0);
      }// end catch
  
      return ((ByteArrayOutputStream) byteOut).toByteArray();
  }
  </code></pre>
  
  <p>Implementation 2:</p>
  
  <pre><code>public byte[] readAudioFileData(String filePath) throws IOException,
          UnsupportedAudioFileException {
      final AudioInputStream audioInputStream = AudioSystem
              .getAudioInputStream(new File(filePath));
      AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, byteOut);
      audioInputStream.close();
      byteOut.close();
  
      return ((ByteArrayOutputStream) byteOut).toByteArray();
  }
  </code></pre>
  
  <p>Every implementation returns a different size of bytes.
  The first one return <code>byte[]</code> with length less than second implementation.</p>
  
  <p>I trying to extract the bytes of data to visualize the Spectrogram of the file.</p>
  
  <p>Any explanation appreciated.</p>
  
  <p>Thanks,</p>
  
  <p>Samer</p>