You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Peter Alfred Lee (Jira)" <ji...@apache.org> on 2019/12/19 02:28:00 UTC
[jira] [Comment Edited] (COMPRESS-499) SeekableInMemoryByteChannel
sets position incorrectly if > size
[ https://issues.apache.org/jira/browse/COMPRESS-499?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16999675#comment-16999675 ]
Peter Alfred Lee edited comment on COMPRESS-499 at 12/19/19 2:27 AM:
---------------------------------------------------------------------
Hi [~trejkaz].
I don't think this is a bug(if I'm correctly understanding the docs of SeekableByteChannel).
Basing the docs:
[https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/channels/SeekableByteChannel.html#truncate(long])
{quote}If the given size is less than the current size then the entity is truncated, discarding any bytes beyond the new end. If the given size is greater than or equal to the current size then the entity is not modified. In either case, if the current position is greater than the given size then it is set to that size.
{quote}
My understanding of this document is :
The method truncate may influence the size and position.
For the part of size, if the input parameter newSize is :
# smaller than the current size : the current size will be truncated, which means the current size would be set to the newSize.
# greater than the current size : the current size would not be modified, which means we don't need to do anything on the size.
For the part of position, it's much easier : if the current position is greater than the newSize, then it is set to that size.
And that's what the code in SeekableByteChannel.truncate do :
{code:java}
//
@Override public SeekableByteChannel truncate(long newSize) { if (size > newSize) { size = (int) newSize; } repositionIfNecessary(); return this; } private void repositionIfNecessary() { if (position > size) { position = size; } }
{code}
In your case :
{quote}If the size of the file/channel is 10 and I call truncate(12), the current behaviour is to set the position back to 10.
{quote}
The original size of the file/channel is 10. You didn't mention about the original position, but it could not be greater than 10 since it's the size of the file/channel.
When calling truncate(12), since the newSize 12 is greater than current size 10, the size would not be modified. And the position would not be changed either since it must be smaller than the newSize 12. I don't see anything wrong in this.
was (Author: peter alfred lee):
Hi [~trejkaz].
I don't think this is a bug(if I'm correctly understanding the docs of SeekableByteChannel).
Basing the docs:
[https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/channels/SeekableByteChannel.html#truncate(long])
{quote}If the given size is less than the current size then the entity is truncated, discarding any bytes beyond the new end. If the given size is greater than or equal to the current size then the entity is not modified. In either case, if the current position is greater than the given size then it is set to that size.
{quote}
My understanding of this document is :
The method truncate may influence the size and position.
For the part of size, if the input parameter newSize is :
# smaller than the current size : the current size will be truncated, which means the current size would be set to the newSize.
# greater than the current size : the current size would not be modified, which means we don't need to do anything on the size.
For the part of position, it's much easier : if the current position is greater than the newSize, then it is set to that size.
And that's what the code in SeekableByteChannel.truncate do :
{code:java}
//代码占位符{code}
@Override
public SeekableByteChannel truncate(long newSize) {
if (size > newSize) {
size = (int) newSize;
}
repositionIfNecessary();
return this;
}
private void repositionIfNecessary() {
if (position > size) {
position = size;
}
}
In your case :
{quote}If the size of the file/channel is 10 and I call truncate(12), the current behaviour is to set the position back to 10.
{quote}
The original size of the file/channel is 10. You didn't mention about the original position, but it could not be greater than 10 since it's the size of the file/channel.
When calling truncate(12), since the newSize 12 is greater than current size 10, the size would not be modified. And the position would not be changed either since it must be smaller than the newSize 12. I don't see anything wrong in this.
> SeekableInMemoryByteChannel sets position incorrectly if > size
> ---------------------------------------------------------------
>
> Key: COMPRESS-499
> URL: https://issues.apache.org/jira/browse/COMPRESS-499
> Project: Commons Compress
> Issue Type: Bug
> Affects Versions: 1.19
> Reporter: Trejkaz
> Priority: Minor
>
> Something we noticed in passing when attempting to use this class as a reference for implementing a new channel class of our own:
> https://github.com/apache/commons-compress/blob/c03704d773dfa0dfc5b2e53b4c198a95d0213ca0/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java#L102
> If the size of the file/channel is 10 and I call truncate(12), the current behaviour is to set the position back to 10.
> The docs of SeekableByteChannel, however, require that this not be done:
> https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/channels/SeekableByteChannel.html#truncate(long)
> {quote}
> If the given size is greater than or equal to the current size then the entity is not modified. In either case, if the current position is greater than the given size then *it is set to that size*.
> {quote}
> I'm not sure whether this bug means there is a bug in the compression library proper, because anyone using a channel sensibly would not be setting the position past the length anyway, but I thought I would raise the issue regardless, in case that were not the case.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)