[XADISK-128] NativeXAFileInputStream skip method throws an unexpected IllegalArgumentException with message "New position cannot be negative or more than file size" Created: 23/Jan/13  Updated: 27/Aug/13  Resolved: 09/Aug/13

Status: Resolved
Project: xadisk
Component/s: filesystem
Affects Version/s: 1.2.1
Fix Version/s: 1.2.2

Type: Bug Priority: Critical
Reporter: Marco Quaranta Assignee: Nitin Verma
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Tags: NativeXAFileInputStream, inputstream, skip


Calling skip method on XAFileInputStream generate IllegalArgumentException due to wrongly calculated readPositionAfterSkip.
This variable is evaluated with this expression:

(NativeXAFileInputStream.java line 175)
long readPositionAfterSkip = (position - byteBuffer.remaining()) + n;

Using a position equals to 0 and skipping 200 bytes with a generic file larger than 200 bytes this expression return a negative value and than, the subsequential calling to "position(readPositionAfterSkip)" method, will throws IllegalArgumentException.

Why don't simply use this?
long readPositionAfterSkip = position + n;

Thank you

Comment by Nitin Verma [ 07/Feb/13 ]

Thanks Marco.

I could reproduce the issue if I try to skip some bytes just after opening the input stream, before any call to read etc. (so, a workaround is to read one byte anyway for the first time, xafis.read())

The logic is failing because byteBuffer is not yet filled. There needs to be a check on flag filledAtleastOnce.

We need to take into account the value of byteBuffer.remaining(); the "position" is not the position of the next byte in the file (to be returned to the user), it is an internal variable which is used to track from where in the file the buffer is to be filled next.

Thanks Again...

Comment by Nitin Verma [ 09/Aug/13 ]

Checked-in the changes to trunk.

Generated at Fri Oct 09 00:25:43 UTC 2015 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.