title: Performance issues with converting mp3 file input stream to byte output stream
body: <p>I would like to extract byte array from a given mp3 file in order to apply fast fourier transform on the latter. The performed FFT will give me some features for my pet-project musical -- recommendation system. </p>
  
  <p>I have written the following code to extract the bytes from a given mp3 file:</p>
  
  <pre><code>public class TrackSample {
  
  private static byte[] readBytesInPredefinedFormat(TargetDataLine format, InputStream inStream) throws IOException {
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      byte[] buffer = new byte[8192];
      int bytesRead;
      while ((bytesRead = inStream.read(buffer)) &gt; 0) {
          int count = format.read(buffer, 0, buffer.length);
          if (count &gt; 0) {
              byteArrayOutputStream.write(buffer, 0, count);
          }
          byteArrayOutputStream.write(buffer, 0, bytesRead);
      }
      byte[] bytes = byteArrayOutputStream.toByteArray();
      byteArrayOutputStream.close();
      inStream.close();
      return bytes;
  }
  
  public static byte[] getTrackBytes(String pathToTrackSample) throws IOException, LineUnavailableException {
      FileInputStream fileInputStream = new FileInputStream(pathToTrackSample);
      final AudioFormat format = CurrentAudioFormat.getAudioFormat(); //Fill AudioFormat with the wanted settings
      DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
  
      TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
      line.open(format);
      line.start();
  
      return readBytesInPredefinedFormat(line, fileInputStream);
      }
  }
  </code></pre>
  
  <p>And the specified audio format is</p>
  
  <pre><code>public class CurrentAudioFormat {
  
      public static AudioFormat getAudioFormat(){
          float sampleRate = 44100;
          int sampleSizeInBits = 8;
          int channels = 1; //mono
          boolean signed = true;
          boolean bigEndian = true;
          return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
      }
  }
  </code></pre>
  
  <p>I tried to test this code on the following mp3 file:</p>
  
  <pre><code>File type ID:   MPG3
  Num Tracks:     1
  ----
  Data format:     2 ch,  44100 Hz, '.mp3' (0x00000000) 0 bits/channel, 0 bytes/packet, 1152 frames/packet, 0 bytes/frame
                  no channel layout.
  estimated duration: 104.176325 sec
  audio bytes: 4167053
  audio packets: 3988
  bit rate: 320000 bits per second
  packet size upper bound: 1052
  maximum packet size: 1045
  audio data file offset: 3169
  optimized
  audio 4591692 valid frames + 576 priming + 1908 remainder = 4594176
  </code></pre>
  
  <p>The system characteristics are:</p>
  
  <ul>
  <li>processor: Intel core i5, 1.4 GHz;</li>
  <li>RAM: DDR3, 4Gb</li>
  <li>OS: Mac OS X El Captain</li>
  </ul>
  
  <p>It took roughly 5 minutes to extract the byte array from this mp3 file. </p>
  
  <p>What are the possible bottlenecks and how can I improve them?</p>