[ 
https://issues.apache.org/jira/browse/COMPRESS-595?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17439574#comment-17439574
 ] 

Peter Lee commented on COMPRESS-595:
------------------------------------

_This is currently not the case, because there is a call to rewind()_
_which results in a buffer whose remaining() is reset to `len` if_
_`readNow` < `len`._

I'm not quite clear about this part. Here is the current implemention of 
readRange :

 
{code:java}
public static byte[] readRange(final ReadableByteChannel input, final int len) 
throws IOException {
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    final ByteBuffer b = ByteBuffer.allocate(Math.min(len, COPY_BUF_SIZE));
    int read = 0;
    while (read < len) {
        // Make sure we never read more than len bytes
        b.limit(Math.min(len - read, b.capacity()));
        final int readNow = input.read(b);
        if (readNow <= 0) {
            break;
        }
        output.write(b.array(), 0, readNow);
        b.rewind();
        read += readNow;
    }
    return output.toByteArray();
}
{code}
As you can see, there's only one call to _rewind()_ of _ByteBuffer b_. The 
_ByteBuffer b_ is a locale buffer which is used to copy the bytes from input to 
output. The remaining() of b is never called.

 

 

Besides that, recently we had a fix in readRange() for partical read in 
COMPRESS-584(Github PR 
[#214|[https://github.com/apache/commons-compress/pull/214/files])|https://github.com/apache/commons-compress/pull/214/files]).].
 Maybe you're testing with Compress 1.21?

> IOUtils.readRange(ReadableByteChannel input, int len) reads more than len 
> when input.read() returns < len (ie. there is a partial read)
> ---------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: COMPRESS-595
>                 URL: https://issues.apache.org/jira/browse/COMPRESS-595
>             Project: Commons Compress
>          Issue Type: Bug
>    Affects Versions: 1.21
>            Reporter: NP
>            Priority: Major
>         Attachments: IOUtilsTest.kt
>
>          Time Spent: 40m
>  Remaining Estimate: 0h
>
> When `input.read(b)` returns `readNow` < `len`, then it means
>  `input.read(b)` will need to be called again with the same
>  buffer, whose `remaining()` is now the old `remaining()` - `readNow`.
> This way the ReadableByteChannel knows how many bytes are to be
>  read in subsequent iterations of the `while (read < len)` loop.
> This is currently not the case, because there is a call to rewind()
>  which results in a buffer whose remaining() is reset to `len` if
>  `readNow` < `len`.
> I suspect the readRange() method has only been used with channels that never 
> do partial reads (such In Memory Byte Channels backed by an array), and hence 
> the problem has not been experienced until now.
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to