RFR: 4617681: constructor of BufferedImage throws unexpected IllegalArgumentException

Phil Race prr at openjdk.org
Tue Oct 7 19:17:30 UTC 2025


On Mon, 6 Oct 2025 19:08:05 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:

>> Specifying the behaviour of BufferedImage constructors for invalid dimensions is long overdue.
>> 
>> The behaviour for image types and sizes <= 0 is unchanged by this PR.
>> Also in many cases the behaviour for sizes that are too large is also unchanged.
>> In some cases, the behaviour is changed from "accidental" NegativeArraySizeException to a consistent IllegalArgumentException.
>> 
>> In no case is anything changed that would affect the possibility to construct a BufferedImage.
>> 
>> A test is provided to ensure the behaviour.
>> 
>> A CSR is provided too : https://bugs.openjdk.org/browse/JDK-8369155
>
> src/java.desktop/share/classes/java/awt/image/BufferedImage.java line 314:
> 
>> 312:      * @throws IllegalArgumentException if the multiplication product of
>> 313:      *          {@code width}, {@code height}, and the number of samples per pixel
>> 314:      *          for the specified format exceeds the maximum length of a Java array.
> 
> This check seems too strict. It is possible to implement a BufferedImage that splits its internal data into multiple surfaces/arrays. It might be better to phrase this as optional:
> “for the specified format exceeds the maximum supported length”

I had looked at using a BandedRaster but there were a decent number of places that relied on the current format, even inside the JDK.
The work and compatibility concerns to address these are out of all proportion to simply documenting what has been true since the very beginning - 27 years plus - without a compelling reason.

The specification has the word "may" so it is not disallowed, but I think it would be risky.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/27640#discussion_r2411640132


More information about the client-libs-dev mailing list