OutOfMemoryError: Java heap space error
<p>I am experiencing a memory problem that I do not understand.
I have the following case</p>

<h2>Case 1</h2>

<pre><code>public  byte[] getBytes(InputStream is) throws IOException {

            int len;
            int size = 1024;
            byte[] buf;

              ByteArrayOutputStream bos = new ByteArrayOutputStream();
              buf = new byte[size];
              while ((len = is.read(buf, 0, size)) != -1)
              {
                bos.write(buf, 0, len);
              }
              buf = bos.toByteArray();

            return buf;
          }

Public void dosomething()
{
 //instructions
InputStream is = new ByteArrayInputStream(getBytes(bodyPart.getInputStream()));

}
</code></pre>

<p>Work fine without error</p>

<p>but this</p>

<h2>Case 2</h2>

<pre><code>Public void dosomething()
{
 //instructions
ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    int len;
                    int size = 1024;
                    byte[] bufferFichierEntree = new byte[size];
                     while ((len = bodyPart.getInputStream().read(bufferFichierEntree, 0, size)) != -1)
                     {
                         bos.write(bufferFichierEntree, 0, len);
                     }
InputStream is = new ByteArrayInputStream(bufferFichierEntree);

}
</code></pre>

<p>return a java.lang.OutOfMemoryError: Java heap space and a don t know why ?
The only difference is that in the first case i use a function unlike the second case</p>
