Java Understanding ByteArrayOutputStream and ByteArrayInputStream
<p>I was reading this article:
<a href="http://www.java-tips.org/java-se-tips-100019/120-javax-sound/917-capturing-audio-with-java-sound-api.html" rel="nofollow">http://www.java-tips.org/java-se-tips-100019/120-javax-sound/917-capturing-audio-with-java-sound-api.html</a>
I don't want to write all Code from before article...</p>

<p>I need to clarify my understand and I need explanation about uses of <em>ByteArrayInputStream</em> and <em>ByteArrayOutputStream</em>...</p>

<p>Based on Complete code:</p>

<p>In the <strong>captureAudio()</strong> method focusing on the loop while </p>

<pre><code>while (running) {
  int count = line.read(buffer, 0, buffer.length);
    if (count &gt; 0) {
      out.write(buffer, 0, count);
    }
}
out.close();
</code></pre>

<p>By definition (check the line 64 and 65):</p>

<pre><code>final TargetDataLine line = (TargetDataLine)
    AudioSystem.getLine(info);
</code></pre>

<p>on the line 79: the Line is the Microphone and // Reads audio data from the data line's input buffer. In other words, the bytes incoming from microphone are locate or stored in bytes <code>buffer</code>.</p>

<p>On the line 81:</p>

<pre><code>      out.write(buffer, 0, count);
</code></pre>

<p><code>out</code> is a ByteArrayOutputStream object..</p>

<blockquote>
  <p>The ByteArrayOutputStream class of the Java IO API allows you to
  capture data written to a stream in an array. You write your data to
  the ByteArrayOutputStream and when you are done you call the
  ByteArrayOutputStream's method toByteArray()  to obtain all the
  written data in a byte array.  The buffer automatically grows as data
  is written to it.</p>
</blockquote>

<p>In my words: <code>ByteArrayOutputStream</code> will be growing taking the bytes from buffer defined in quantity <code>count</code></p>

<p>On the other side:</p>

<p>In the <strong>playAudio()</strong> method.</p>

<p>I can see that the first line (line 101 of complete code) all bytes are taken!!!</p>

<pre><code>byte audio[] = out.toByteArray();
</code></pre>

<p><a href="https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html#toByteArray()" rel="nofollow">https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html#toByteArray()</a></p>

<blockquote>
  <p>Creates a newly allocated byte array. Its size is the current size of
  this output stream and the valid contents of the buffer have been
  copied into it.</p>
</blockquote>

<p>Now in the lines (102 and 103)</p>

<pre><code>InputStream input = 
    new ByteArrayInputStream(audio);
</code></pre>

<p>On the lines (105 until 107 ) the bytes are passed throught:</p>

<pre><code>  final AudioInputStream ais = 
    new AudioInputStream(input, format, 
    audio.length / format.getFrameSize());
</code></pre>

<p>focusing in the while loop and near lines </p>

<pre><code>int count;
while ((count = ais.read(
  buffer, 0, buffer.length)) != -1) {
    if (count &gt; 0) {
      line.write(buffer, 0, count);
    }
  }
line.drain();
line.close();
</code></pre>

<p>The bytes are taken from <code>ais</code></p>

<p>And the line (lines 110 and 111) is representing the speakers</p>

<pre><code>  final SourceDataLine line = (SourceDataLine)
    AudioSystem.getLine(info);
</code></pre>

<p>The <strong>Question 1</strong> is:</p>

<p><code>out</code>, from <strong>captureAudio</strong> method, will be taking bytes infinitely, but How <code>input</code>, from <strong>playAudio</strong> method, takes exactly the bytes required to sound them consistently?</p>

<p>Remember: <code>out.toByteArray();</code> take all bytes, but speakers not sound repeatedly the same bytes...</p>

<p>The <strong>Question 2</strong> is:</p>

<p>Can I to handle this situation read from microphone(TargetDataLine) and write to speaker(SourceDataLine) without using these two objects (ByteArrayOutputStream and ByteArrayInputStream) like related article?</p>

<p>Like next code:</p>

<pre><code>while (running) {
  int count = microphone.read(buffer, 0, buffer.length);
    if (count &gt; 0) {
      speaker.write(buffer, 0, count);
    }
}
speaker.drain();
speaker.close();
</code></pre>

<p>The <strong>Question 3</strong> is:</p>

<p>How I can implement a Repeater (Capture sound from the microphone and play it on the speakers, infinitely, 1 or 2 hours)?</p>

<p>Note: Without worrying about storage problems bytes in memory (without storing on file) without playback delays.</p>