title: Getting a portion of file as byte[] at a time
body: <p>I'm trying to handle large files by 10MB byte arrays at a time.
  I'm trying to get the byte arrays one at a time (not get entire byte array for the huge file and split the byte array, after all the problem was due to memory)</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>private byte[] readFile(File file, int offset) throws IOException
  {
      BufferedInputStream inStream = null;
      ByteArrayOutputStream outStream = null;
      byte[] buf = new byte[1048576];
      int read = 0;
  
      try
      {
          inStream = new BufferedInputStream(new FileInputStream(file));
          outStream = new ByteArrayOutputStream();
          long skipped = inStream.skip(offset);
          read = inStream.read(buf);
          if (read != -1)
          {
              outStream.write(buf, 0, read);
              return outStream.toByteArray();
          }
      }
      finally
      {
          if (inStream != null) {try {inStream.close();} catch (IOException e) {}}
          if (outStream != null) {try {outStream.close();} catch (IOException e) {}}
      }
  
      return null;
  </code></pre>
  
  <p>the parameter <code>offset</code> will be in 10MB increments as well.</p>
  
  <p>So the problem I'm having is that, even tho the <code>skipped</code> long variable gives me 1048576 bytes skipped, the second 10MB i'm suppose to receive from calling <code>readFile(file, 1048576)</code> is the same as the first byte array from the first 10MB. Thus it didn't really skip the first 10MB at all.</p>
  
  <p>What's the problem here? Is there another way of implementing this idea?</p>