changeset 48825:7c8c4a447fe4

8273756: Enhance BMP image support Reviewed-by: andrew
author bae
date Thu, 23 Sep 2021 13:29:37 +0000
parents 1189d9ea5725
children f75aee4df47a
files jdk/src/share/classes/com/sun/imageio/plugins/bmp/BMPImageReader.java
diffstat 1 files changed, 32 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/share/classes/com/sun/imageio/plugins/bmp/BMPImageReader.java	Thu Jan 06 15:44:41 2022 +0300
+++ b/jdk/src/share/classes/com/sun/imageio/plugins/bmp/BMPImageReader.java	Thu Sep 23 13:29:37 2021 +0000
@@ -66,6 +66,7 @@
 import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 import java.util.StringTokenizer;
 
 import com.sun.imageio.plugins.common.ImageUtil;
@@ -214,6 +215,33 @@
         }
     }
 
+    private void readColorPalette(int sizeOfPalette) throws IOException {
+        final int UNIT_SIZE = 1024000;
+        if (sizeOfPalette < UNIT_SIZE) {
+            palette = new byte[sizeOfPalette];
+            iis.readFully(palette, 0, sizeOfPalette);
+        } else {
+            int bytesToRead = sizeOfPalette;
+            int bytesRead = 0;
+            List<byte[]> bufs = new ArrayList<>();
+            while (bytesToRead != 0) {
+                int sz = Math.min(bytesToRead, UNIT_SIZE);
+                byte[] unit = new byte[sz];
+                iis.readFully(unit, 0, sz);
+                bufs.add(unit);
+                bytesRead += sz;
+                bytesToRead -= sz;
+            }
+            byte[] paletteData = new byte[bytesRead];
+            int copiedBytes = 0;
+            for (byte[] ba : bufs) {
+                System.arraycopy(ba, 0, paletteData, copiedBytes, ba.length);
+                copiedBytes += ba.length;
+            }
+            palette = paletteData;
+        }
+    }
+
     /**
      * Process the image header.
      *
@@ -294,8 +322,7 @@
             // Read in the palette
             int numberOfEntries = (int)((bitmapOffset - 14 - size) / 3);
             int sizeOfPalette = numberOfEntries*3;
-            palette = new byte[sizeOfPalette];
-            iis.readFully(palette, 0, sizeOfPalette);
+            readColorPalette(sizeOfPalette);
             metadata.palette = palette;
             metadata.paletteSize = numberOfEntries;
         } else {
@@ -332,8 +359,7 @@
                     }
                     int numberOfEntries = (int)((bitmapOffset-14-size) / 4);
                     int sizeOfPalette = numberOfEntries * 4;
-                    palette = new byte[sizeOfPalette];
-                    iis.readFully(palette, 0, sizeOfPalette);
+                    readColorPalette(sizeOfPalette);
 
                     metadata.palette = palette;
                     metadata.paletteSize = numberOfEntries;
@@ -387,8 +413,7 @@
                     if (colorsUsed != 0) {
                         // there is a palette
                         sizeOfPalette = (int)colorsUsed*4;
-                        palette = new byte[sizeOfPalette];
-                        iis.readFully(palette, 0, sizeOfPalette);
+                        readColorPalette(sizeOfPalette);
 
                         metadata.palette = palette;
                         metadata.paletteSize = (int)colorsUsed;
@@ -455,8 +480,7 @@
                 // Read in the palette
                 int numberOfEntries = (int)((bitmapOffset-14-size) / 4);
                 int sizeOfPalette = numberOfEntries*4;
-                palette = new byte[sizeOfPalette];
-                iis.readFully(palette, 0, sizeOfPalette);
+                readColorPalette(sizeOfPalette);
                 metadata.palette = palette;
                 metadata.paletteSize = numberOfEntries;