matthijsln edited a comment on pull request #214:
URL: https://github.com/apache/commons-compress/pull/214#issuecomment-892573540


   The "no copy buffer" version can be more efficient by only calling 
`Arrays.copyOf()` when hitting EOF. This avoids copying the byte array when it 
is completely filled:
   
   ```
       public static byte[] readRange(final ReadableByteChannel input, final 
int len) throws IOException {
           final ByteBuffer b = ByteBuffer.allocate(len);
           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 b.array();
           }
       }


-- 
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


Reply via email to