Skip to content

Commit 39b501a

Browse files
committed
Throw ArithmeticException if the position overflows an int
1 parent 49e6b3d commit 39b501a

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/main/java/org/apache/commons/io/build/AbstractOrigin.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,19 +414,19 @@ public byte[] getByteArray() throws IOException {
414414
* Gets this origin as a byte array, if possible.
415415
*
416416
* @param position the initial index of the range to be copied, inclusive.
417-
* @param length How many bytes to copy.
417+
* @param length How many bytes to copy.
418418
* @return this origin as a byte array, if possible.
419-
* @throws IOException if an I/O error occurs.
420419
* @throws UnsupportedOperationException if the origin cannot be converted to a Path.
420+
* @throws ArithmeticException if the {@code position} overflows an int
421+
* @throws IOException if an I/O error occurs.
421422
* @since 2.13.0
422423
*/
423424
public byte[] getByteArray(final long position, final int length) throws IOException {
424425
final byte[] bytes = getByteArray();
425-
final int start = (int) position;
426-
// We include a separate check for int overflow.
426+
// Checks for int overflow.
427+
final int start = Math.toIntExact(position);
427428
if (start < 0 || length < 0 || start + length < 0 || start + length > bytes.length) {
428-
throw new IllegalArgumentException("Couldn't read array (start: " + start + ", length: " + length
429-
+ ", data length: " + bytes.length + ").");
429+
throw new IllegalArgumentException("Couldn't read array (start: " + start + ", length: " + length + ", data length: " + bytes.length + ").");
430430
}
431431
return Arrays.copyOfRange(bytes, start, start + length);
432432
}

0 commit comments

Comments
 (0)