matthijsln commented on pull request #214: URL: https://github.com/apache/commons-compress/pull/214#issuecomment-892573540
Wrapping a byte array in a `ByteBuffer` is the most efficient. In the following implementation an array copy is only needed when hitting EOF: ``` public static byte[] readRange(final ReadableByteChannel input, final int len) throws IOException { final byte[] out = new byte[len]; final ByteBuffer b = ByteBuffer.wrap(out); while (b.hasRemaining()) { if (input.read(b) <= 0) { break; } } if (b.hasRemaining()) { // We hit EOF and read less bytes, return a smaller array than len return Arrays.copyOf(b.array(), b.position()); } else { return out; } } -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org