title: Elegant way to read file into byte[] array in Java
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/858980/file-to-byte-in-java">File to byte[] in Java</a>  </p>
  </blockquote>
  
  
  
  <p>I want to read data from file and unmarshal it to Parcel.
  In documentation it is not clear, that FileInputStream has method to read all its content. To implement this, I do folowing:</p>
  
  <pre><code>FileInputStream filein = context.openFileInput(FILENAME);
  
  
  int read = 0;
  int offset = 0;
  int chunk_size = 1024;
  int total_size = 0;
  
  ArrayList&lt;byte[]&gt; chunks = new ArrayList&lt;byte[]&gt;();
  chunks.add(new byte[chunk_size]);
  //first I read data from file chunk by chunk
  while ( (read = filein.read(chunks.get(chunks.size()-1), offset, buffer_size)) != -1) {
      total_size+=read;
      if (read == buffer_size) {
           chunks.add(new byte[buffer_size]);
      }
  }
  int index = 0;
  
  // then I create big buffer        
  byte[] rawdata = new byte[total_size];
  
  // then I copy data from every chunk in this buffer
  for (byte [] chunk: chunks) {
      for (byte bt : chunk) {
           index += 0;
           rawdata[index] = bt;
           if (index &gt;= total_size) break;
      }
      if (index&gt;= total_size) break;
  }
  
  // and clear chunks array
  chunks.clear();
  
  // finally I can unmarshall this data to Parcel
  Parcel parcel = Parcel.obtain();
  parcel.unmarshall(rawdata,0,rawdata.length);
  </code></pre>
  
  <p>I think this code looks ugly, and my question is:
  How to do read data from file into byte[] beautifully? :)</p>