changeset 57693:553cb2204361

8227816: More Colorful ICC profiles Reviewed-by: serb, psadhukhan, mschoene, rhalade
author prr
date Tue, 01 Oct 2019 08:47:32 -0700
parents 9bd776693ccf
children 4c13ae80aa8e
files src/java.desktop/share/classes/java/awt/color/ICC_Profile.java
diffstat 1 files changed, 13 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/share/classes/java/awt/color/ICC_Profile.java	Thu Sep 26 18:13:20 2019 +0800
+++ b/src/java.desktop/share/classes/java/awt/color/ICC_Profile.java	Tue Oct 01 08:47:32 2019 -0700
@@ -35,6 +35,7 @@
 
 package java.awt.color;
 
+import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -1017,42 +1018,25 @@
 
 
     static byte[] getProfileDataFromStream(InputStream s) throws IOException {
-    byte[] profileData;
-    int profileSize;
 
-        byte[] header = new byte[128];
-        int bytestoread = 128;
-        int bytesread = 0;
-        int n;
+        BufferedInputStream bis = new BufferedInputStream(s);
+        bis.mark(128);
 
-        while (bytestoread != 0) {
-            if ((n = s.read(header, bytesread, bytestoread)) < 0) {
-                return null;
-            }
-            bytesread += n;
-            bytestoread -= n;
-        }
+        byte[] header = bis.readNBytes(128);
         if (header[36] != 0x61 || header[37] != 0x63 ||
             header[38] != 0x73 || header[39] != 0x70) {
             return null;   /* not a valid profile */
         }
-        profileSize = ((header[0] & 0xff) << 24) |
-                      ((header[1] & 0xff) << 16) |
-                      ((header[2] & 0xff) <<  8) |
-                       (header[3] & 0xff);
-        profileData = new byte[profileSize];
-        System.arraycopy(header, 0, profileData, 0, 128);
-        bytestoread = profileSize - 128;
-        bytesread = 128;
-        while (bytestoread != 0) {
-            if ((n = s.read(profileData, bytesread, bytestoread)) < 0) {
-                return null;
-            }
-            bytesread += n;
-            bytestoread -= n;
+        int profileSize = ((header[0] & 0xff) << 24) |
+                          ((header[1] & 0xff) << 16) |
+                          ((header[2] & 0xff) << 8) |
+                          (header[3] & 0xff);
+        bis.reset();
+        try {
+            return bis.readNBytes(profileSize);
+        } catch (OutOfMemoryError e) {
+            throw new IOException("Color profile is too big");
         }
-
-        return profileData;
     }
 
     /**