title: BufferedInputStream into byte[] to be send over a Socket to a Database
body: <p>I have been looking around for an answer to this, but couldn't really find anything on it. Earlier today, I asked how I could make a File into a String through a byte array, and then back again, for retrieval later.</p>
  
  <p>What people told me, was that I had to just store the byte array, to avoid nasty encoding issues. So now I've started working on that, but I have now hit a wall.</p>
  
  <p>Basically, I used unbuffered streams before, to turn a file into a byte array. This works good in theory, but it takes up a lot of memory which eventually will cast the heap size exception. I should use buffered streams instead (or so I am told), and the problem I have now, is going from a BufferedInputStream to a byte[]. I've tried to copy and use the methods found in this documentation</p>
  
  <p><a href="http://docs.guava-libraries.googlecode.com/git/javadoc/index.html?com/google/common/io/package-summary.html" rel="nofollow noreferrer">http://docs.guava-libraries.googlecode.com/git/javadoc/index.html?com/google/common/io/package-summary.html</a></p>
  
  <p>Where I exchange unbuffered streams for buffered streams. The only issue, is that I can't directly turn a buffered output stream into a byte array, as I can with an unbuffered stream.</p>
  
  <p>Help? :)</p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  
  public final class BufferedByteStream {
  
      private static final int BUF_SIZE = 1024000;
  
      public static long copy(BufferedInputStream from, BufferedOutputStream to) throws IOException { 
          byte[] buf = new byte[BUF_SIZE];
          long total = 0;
          while(true) {
              int r = from.read(buf);
              if(r == -1) {
                  break;
              }
              to.write(buf, 0, r);
              total += r;
          }
          return total;
      }
  
      public static byte[] toByteArray(BufferedInputStream in) throws IOException {
          BufferedOutputStream out = new BufferedOutputStream(new ByteArrayOutputStream());
          copy(in, out);
          return out. // &lt;--- Problem is here
      }
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I am still getting Heap Space errors. So I will now post all the code:</p>
  
  <p>main.java</p>
  
  <pre><code>import java.io.*;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import jserver.io.BufferedByteStream;
  /**
  *
  * @author Vipar
  */
  public class main {
      public static void main(String[] args) {
      File f = new File("&lt;doesn't matter&gt;");
          try {
              byte[] buf;
              try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) {
                  buf = BufferedByteStream.toByteArray(bis);
                  bis.close();
              }
              File f2 = new File("&lt;doesn't matter&gt;");
              try (FileOutputStream fos = new FileOutputStream(f2)) {
                  fos.write(buf);
                  fos.close();
              }
          } catch (FileNotFoundException ex) {
              Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
          } catch (IOException ex) {
              Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  </code></pre>
  
  <p>BufferedByteStream.java</p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  
  public final class BufferedByteStream {
  
      private static final int BUF_SIZE = 1024000;
  
      public static long copy(BufferedInputStream from, BufferedOutputStream to) throws IOException { 
          byte[] buf = new byte[BUF_SIZE];
          long total = 0;
          while(true) {
              int r = from.read(buf);
              if(r == -1) {
                  break;
              }
              to.write(buf, 0, r);
              total += r;
          }
          return total;
      }
  
      public static byte[] toByteArray(BufferedInputStream in) throws IOException {
          ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
          BufferedOutputStream out = new BufferedOutputStream(bytesOut);
          copy(in, out);
          return bytesOut.toByteArray();
      }
  }
  </code></pre>