0

Why does the following code throws NegativeArraySizeException exception?

new BufferedImage(250 * 100, 250 * 100, BufferedImage.TYPE_4BYTE_ABGR);

Output

Exception in thread "main" java.lang.NegativeArraySizeException
        at java.awt.image.DataBufferByte.<init>(Unknown Source)
        at java.awt.image.Raster.createInterleavedRaster(Unknown Source)
        at java.awt.image.BufferedImage.<init>(Unknown Source)
        at Main.main(Main.java:120)

Does it exceed some internal buffer's limit?

FrozenHeart
  • 18,778
  • 30
  • 107
  • 223

1 Answers1

3

(250 * 100) * (250 * 100) * 4 = 2500000000 and the result is larger than the maximum value for int: 2 ** 31 - 1 = 2147483647. The document says that the data will be in single byte array, so it exceeds the limit.

MikeCAT
  • 69,090
  • 10
  • 44
  • 65