title: How does the AudioInputStream.read(byte[] b) function work?
body: <p>I have a problem with <a href="https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioInputStream.html#read(byte[])" rel="nofollow noreferrer">this</a> method.
  In the following i have written a test code that reads an audio stream into a byte array (and does a convertion [i have mp3plugin.jar as external jar, if you should wonder why this works] and some other stuff, but i have only let that in to make the code executable).
  <strong>The problem is that the method seams not allways to do the same thing. Sometimes the AudioInputStream is getting smaller and sometimes it is not. My question is why?</strong></p>
  
  <pre><code>package doej.test;
  
  import java.io.File;
  
  import javax.sound.sampled.AudioFormat;
  import javax.sound.sampled.AudioInputStream;
  import javax.sound.sampled.AudioSystem;
  
  public class Test2 {
  
      public static void main(String[] args) {
          @SuppressWarnings("unused")
          int totalFramesRead = 0;
          File fileIn = new File("D:/User/Music/eclipseTestfiles/test.mp3");
          try {
              AudioInputStream mp3Stream = AudioSystem.getAudioInputStream(fileIn);
              AudioFormat sourceFormat = mp3Stream.getFormat();
              AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false);
              AudioInputStream converted = AudioSystem.getAudioInputStream(convertFormat, mp3Stream);
              int bytesPerFrame = converted.getFormat().getFrameSize();
              int numBytes = 1024 * bytesPerFrame;
              byte[] audioBytes = new byte[numBytes];
              try {
                  int numBytesRead = 0;
  
                  //My simplified Test part
                  System.out.println(converted.available());
  </code></pre>
  
  <p>Ok, so this is the original size of the audio stream. (Until here everything is fine)</p>
  
  <pre><code>                System.out.println("read " + converted.read(audioBytes));
                  System.out.println(converted.available());
  </code></pre>
  
  <p>Why <strong>is</strong> my Stream getting smaller? (in the documentation they say it is reading into the array so i kinda understand that part)</p>
  
  <pre><code>                System.out.println("read " + converted.read(audioBytes));
                  System.out.println(converted.available());
  </code></pre>
  
  <p>Why <strong>is</strong> my Stream <strong>not</strong> getting smaller, as it did the first time? (for this is the part i have no clue)</p>
  
  <pre><code>                while ((numBytesRead = converted.read(audioBytes)) != -1) {
                      System.out.println(converted.available());
  </code></pre>
  
  <p>Why <strong>is</strong> the Stream now getting smaller <strong>every eleven times</strong>? (here I don't understand the world anymore)</p>
  
  <pre><code>                }
                  //End of my test part
  
              } catch (Exception ex) {
              }
              System.out.println("finished");
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Finally this is the output i get:</p>
  
  <pre><code>6043688
  read 4096
  6035592
  read 4096
  6035592
  6035592
  6035592
  6035592
  6035592
  6035592
  6035592
  6035592
  6035592
  6035592
  6029324
  6029324
  6029324
  [...]
  4444
  4444
  4444
  0
  0
  0
  0
  0
  0
  0
  0
  0
  0
  0
  0
  finished
  </code></pre>
  
  <p>Thanks for your help.</p>
  
  <p>John</p>