How do you get successive slices out of a ByteBuffer?

Percy picture Percy · Jul 5, 2011 · Viewed 9.1k times · Source

I have a ByteBuffer that contains a large file (100 MB):

    java.nio.ByteBuffer byteBuffer = ByteBuffer.wrap(multipartFile.getBytes());
    writeChannel.write(byteBuffer);
    writeChannel.closeFinally();

I can only legally write 1 MB to the writeChannel at a time.

How do I slice up the contents of the ByteBuffer and write only a 1 MB slice at a time into the writeChannel?

Answer

seh picture seh · Jul 5, 2011

You can use ByteBuffer#slice() to get a duplicate view of your base ByteBuffer instance, then bump the position along to expose a sliding window of content. Alternately, you can just do the same to your base buffer if you don't need to expose it to any other consumers.

You can change the starting position of your view of the content via the single-argument Buffer#position(int) method, and change the end position of your view via Buffer#limit(int). So long as you're mindful not to push the view beyond the limit of the underlying buffer, you can do the following:

final ByteBuffer view = base.slice();
for (int start = base.position(), end = base.limit(), stride = 1000000;
     start != end;
     start = view.limit())
  consume(view.position(start)
              .limit(start + Math.min(end - start, stride)));

I didn't test it, but it looks correct. It's possible to rewrite to avoid the initial setting of the position, which isn't strictly necessary here, but it incurs either some repetition or more awkward special case treatment of the first time through.

I left it this way to preserve the basic for loop structure.