jdk/src/share/classes/java/awt/color/ICC_Profile.java
changeset 4206 8925b5c1334b
parent 3288 db82a42da273
child 4209 e2e5a973b879
equal deleted inserted replaced
4205:89ffcfc5fc26 4206:8925b5c1334b
   861             break;
   861             break;
   862 
   862 
   863         case ColorSpace.CS_PYCC:
   863         case ColorSpace.CS_PYCC:
   864             synchronized(ICC_Profile.class) {
   864             synchronized(ICC_Profile.class) {
   865                 if (PYCCprofile == null) {
   865                 if (PYCCprofile == null) {
   866                     if (getProfileFile("PYCC.pf") != null) {
   866                     if (!sun.jkernel.DownloadManager.isJREComplete() ||
       
   867                         standardProfileExists("PYCC.pf"))
       
   868                     {
   867                         ProfileDeferralInfo pInfo =
   869                         ProfileDeferralInfo pInfo =
   868                             new ProfileDeferralInfo("PYCC.pf",
   870                             new ProfileDeferralInfo("PYCC.pf",
   869                                                     ColorSpace.TYPE_3CLR, 3,
   871                                                     ColorSpace.TYPE_3CLR, 3,
   870                                                     CLASS_DISPLAY);
   872                                                     CLASS_DISPLAY);
   871                         PYCCprofile = getDeferredInstance(pInfo);
   873                         PYCCprofile = getDeferredInstance(pInfo);
   959      *
   961      *
   960      * @exception SecurityException If a security manager is installed
   962      * @exception SecurityException If a security manager is installed
   961      * and it does not permit read access to the given file.
   963      * and it does not permit read access to the given file.
   962      */
   964      */
   963     public static ICC_Profile getInstance(String fileName) throws IOException {
   965     public static ICC_Profile getInstance(String fileName) throws IOException {
   964     ICC_Profile thisProfile;
   966         ICC_Profile thisProfile;
   965     FileInputStream fis;
   967         FileInputStream fis = null;
   966 
   968 
   967         SecurityManager security = System.getSecurityManager();
   969 
   968         if (security != null) {
   970         File f = getProfileFile(fileName);
   969             security.checkRead(fileName);
   971         if (f != null) {
   970         }
   972             fis = new FileInputStream(f);
   971 
   973         }
   972         if ((fis = openProfile(fileName)) == null) {
   974         if (fis == null) {
   973             throw new IOException("Cannot open file " + fileName);
   975             throw new IOException("Cannot open file " + fileName);
   974         }
   976         }
   975 
   977 
   976         thisProfile = getInstance(fis);
   978         thisProfile = getInstance(fis);
   977 
   979 
  1079 
  1081 
  1080 
  1082 
  1081     void activateDeferredProfile() throws ProfileDataException {
  1083     void activateDeferredProfile() throws ProfileDataException {
  1082         byte profileData[];
  1084         byte profileData[];
  1083         FileInputStream fis;
  1085         FileInputStream fis;
  1084         String fileName = deferralInfo.filename;
  1086         final String fileName = deferralInfo.filename;
  1085 
  1087 
  1086         profileActivator = null;
  1088         profileActivator = null;
  1087         deferralInfo = null;
  1089         deferralInfo = null;
  1088         if ((fis = openProfile(fileName)) == null) {
  1090         PrivilegedAction<FileInputStream> pa = new PrivilegedAction<FileInputStream>() {
       
  1091             public FileInputStream run() {
       
  1092                 File f = getStandardProfileFile(fileName);
       
  1093                 if (f != null) {
       
  1094                     try {
       
  1095                         return new FileInputStream(f);
       
  1096                     } catch (FileNotFoundException e) {}
       
  1097                 }
       
  1098                 return null;
       
  1099             }
       
  1100         };
       
  1101         if ((fis = AccessController.doPrivileged(pa)) == null) {
  1089             throw new ProfileDataException("Cannot open file " + fileName);
  1102             throw new ProfileDataException("Cannot open file " + fileName);
  1090         }
  1103         }
  1091         try {
  1104         try {
  1092             profileData = getProfileDataFromStream(fis);
  1105             profileData = getProfileDataFromStream(fis);
  1093             fis.close();    /* close the file */
  1106             fis.close();    /* close the file */
  1782      * second, relative to any directories specified by the java.class.path
  1795      * second, relative to any directories specified by the java.class.path
  1783      * property; finally, in a directory used to store profiles always
  1796      * property; finally, in a directory used to store profiles always
  1784      * available, such as a profile for sRGB.  Built-in profiles use .pf as
  1797      * available, such as a profile for sRGB.  Built-in profiles use .pf as
  1785      * the file name extension for profiles, e.g. sRGB.pf.
  1798      * the file name extension for profiles, e.g. sRGB.pf.
  1786      */
  1799      */
  1787     private static FileInputStream openProfile(final String fileName) {
  1800     private static File getProfileFile(String fileName) {
  1788         return (FileInputStream)java.security.AccessController.doPrivileged(
       
  1789             new java.security.PrivilegedAction() {
       
  1790             public Object run() {
       
  1791                 File f = privilegedGetProfileFile(fileName);
       
  1792                 if (f != null) {
       
  1793                     try {
       
  1794                         return new FileInputStream(f);
       
  1795                     } catch (FileNotFoundException e) {
       
  1796                     }
       
  1797                 }
       
  1798                 return null;
       
  1799             }
       
  1800         });
       
  1801     }
       
  1802 
       
  1803     private static File getProfileFile(final String fileName) {
       
  1804         return (File)java.security.AccessController.doPrivileged(
       
  1805             new java.security.PrivilegedAction() {
       
  1806             public Object run() {
       
  1807                 return privilegedGetProfileFile(fileName);
       
  1808             }
       
  1809         });
       
  1810     }
       
  1811 
       
  1812     /*
       
  1813      * this version is called from doPrivileged in openProfile
       
  1814      * or getProfileFile, so the whole method is privileged!
       
  1815      */
       
  1816 
       
  1817     private static File privilegedGetProfileFile(String fileName) {
       
  1818         String path, dir, fullPath;
  1801         String path, dir, fullPath;
  1819 
  1802 
  1820         File f = new File(fileName); /* try absolute file name */
  1803         File f = new File(fileName); /* try absolute file name */
  1821 
  1804         if (f.isAbsolute()) {
       
  1805             /* Rest of code has little sense for an absolute pathname,
       
  1806                so return here. */
       
  1807             return f.isFile() ? f : null;
       
  1808         }
  1822         if ((!f.isFile()) &&
  1809         if ((!f.isFile()) &&
  1823                 ((path = System.getProperty("java.iccprofile.path")) != null)){
  1810                 ((path = System.getProperty("java.iccprofile.path")) != null)){
  1824                                     /* try relative to java.iccprofile.path */
  1811                                     /* try relative to java.iccprofile.path */
  1825                 StringTokenizer st =
  1812                 StringTokenizer st =
  1826                     new StringTokenizer(path, File.pathSeparator);
  1813                     new StringTokenizer(path, File.pathSeparator);
  1827                 while (st.hasMoreTokens() && (!f.isFile())) {
  1814                 while (st.hasMoreTokens() && ((f == null) || (!f.isFile()))) {
  1828                     dir = st.nextToken();
  1815                     dir = st.nextToken();
  1829                         fullPath = dir + File.separatorChar + fileName;
  1816                         fullPath = dir + File.separatorChar + fileName;
  1830                     f = new File(fullPath);
  1817                     f = new File(fullPath);
       
  1818                     if (!isChildOf(f, dir)) {
       
  1819                         f = null;
       
  1820                     }
  1831                 }
  1821                 }
  1832             }
  1822             }
  1833 
  1823 
  1834         if ((!f.isFile()) &&
  1824         if (((f == null) || (!f.isFile())) &&
  1835                 ((path = System.getProperty("java.class.path")) != null)) {
  1825                 ((path = System.getProperty("java.class.path")) != null)) {
  1836                                     /* try relative to java.class.path */
  1826                                     /* try relative to java.class.path */
  1837                 StringTokenizer st =
  1827                 StringTokenizer st =
  1838                     new StringTokenizer(path, File.pathSeparator);
  1828                     new StringTokenizer(path, File.pathSeparator);
  1839                 while (st.hasMoreTokens() && (!f.isFile())) {
  1829                 while (st.hasMoreTokens() && ((f == null) || (!f.isFile()))) {
  1840                     dir = st.nextToken();
  1830                     dir = st.nextToken();
  1841                         fullPath = dir + File.separatorChar + fileName;
  1831                         fullPath = dir + File.separatorChar + fileName;
  1842                     f = new File(fullPath);
  1832                     f = new File(fullPath);
       
  1833                     if (!isChildOf(f, dir)) {
       
  1834                         f = null;
       
  1835                     }
  1843                 }
  1836                 }
  1844             }
  1837             }
  1845 
  1838         if ((f == null) || (!f.isFile())) {
  1846         if (!f.isFile()) { /* try the directory of built-in profiles */
  1839             /* try the directory of built-in profiles */
  1847             dir = System.getProperty("java.home") +
  1840             f = getStandardProfileFile(fileName);
  1848                 File.separatorChar + "lib" + File.separatorChar + "cmm";
  1841         }
  1849             fullPath = dir + File.separatorChar + fileName;
  1842         if (f != null && f.isFile()) {
  1850                 f = new File(fullPath);
  1843             return f;
  1851                 if (!f.isFile()) {
  1844         }
  1852                     //make sure file was installed in the kernel mode
  1845         return null;
  1853                     try {
  1846     }
  1854                         //kernel uses platform independent paths =>
  1847 
  1855                         //   should not use platform separator char
  1848     /**
  1856                         sun.jkernel.DownloadManager.downloadFile("lib/cmm/"+fileName);
  1849      * Returns a file object corresponding to a built-in profile
  1857                     } catch (IOException ioe) {}
  1850      * specified by fileName.
       
  1851      * If there is no built-in profile with such name, then the method
       
  1852      * returns null.
       
  1853      */
       
  1854     private static File getStandardProfileFile(String fileName) {
       
  1855         String dir = System.getProperty("java.home") +
       
  1856             File.separatorChar + "lib" + File.separatorChar + "cmm";
       
  1857         String fullPath = dir + File.separatorChar + fileName;
       
  1858         File f = new File(fullPath);
       
  1859         if (!f.isFile()) {
       
  1860             //make sure file was installed in the kernel mode
       
  1861             try {
       
  1862                 //kernel uses platform independent paths =>
       
  1863                 //   should not use platform separator char
       
  1864                 sun.jkernel.DownloadManager.downloadFile("lib/cmm/"+fileName);
       
  1865             } catch (IOException ioe) {}
       
  1866         }
       
  1867         return (f.isFile() && isChildOf(f, dir)) ? f : null;
       
  1868     }
       
  1869 
       
  1870     /**
       
  1871      * Checks whether given file resides inside give directory.
       
  1872      */
       
  1873     private static boolean isChildOf(File f, String dirName) {
       
  1874         try {
       
  1875             File dir = new File(dirName);
       
  1876             String canonicalDirName = dir.getCanonicalPath();
       
  1877             if (!canonicalDirName.endsWith(File.separator)) {
       
  1878                 canonicalDirName += File.separator;
       
  1879             }
       
  1880             String canonicalFileName = f.getCanonicalPath();
       
  1881             return canonicalFileName.startsWith(canonicalDirName);
       
  1882         } catch (IOException e) {
       
  1883             /* we do not expect the IOException here, because invocation
       
  1884              * of this function is always preceeded by isFile() call.
       
  1885              */
       
  1886             return false;
       
  1887         }
       
  1888     }
       
  1889 
       
  1890     /**
       
  1891      * Checks whether built-in profile specified by fileName exists.
       
  1892      */
       
  1893     private static boolean standardProfileExists(final String fileName) {
       
  1894         return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
       
  1895                 public Boolean run() {
       
  1896                     return getStandardProfileFile(fileName) != null;
  1858                 }
  1897                 }
  1859             }
  1898             });
  1860 
       
  1861         if (f.isFile()) {
       
  1862             return f;
       
  1863         }
       
  1864         return null;
       
  1865     }
  1899     }
  1866 
  1900 
  1867 
  1901 
  1868     /*
  1902     /*
  1869      * Serialization support.
  1903      * Serialization support.