Home | History | Annotate | Download | only in impl
      1 /*
      2  *******************************************************************************
      3  * Copyright (C) 2004-2009, International Business Machines Corporation and    *
      4  * others. All Rights Reserved.                                                *
      5  *******************************************************************************
      6  *
      7  * Created on Feb 4, 2004
      8  *
      9  */
     10 package com.ibm.icu.impl;
     11 
     12 import java.io.InputStream;
     13 import java.net.URL;
     14 import java.security.AccessController;
     15 import java.security.PrivilegedAction;
     16 import java.util.MissingResourceException;
     17 
     18 /**
     19  * Provides access to ICU data files as InputStreams.  Implements security checking.
     20  */
     21 public final class ICUData {
     22     /*
     23      * Return a URL to the ICU resource names resourceName.  The
     24      * resource name should either be an absolute path, or a path relative to
     25      * com.ibm.icu.impl (e.g., most likely it is 'data/foo').  If required
     26      * is true, throw an MissingResourceException instead of returning a null result.
     27      */
     28     public static boolean exists(final String resourceName) {
     29         URL i = null;
     30         if (System.getSecurityManager() != null) {
     31             i = AccessController.doPrivileged(new PrivilegedAction<URL>() {
     32                     public URL run() {
     33                         return ICUData.class.getResource(resourceName);
     34                     }
     35                 });
     36         } else {
     37             i = ICUData.class.getResource(resourceName);
     38         }
     39         return i != null;
     40     }
     41 
     42     private static InputStream getStream(final Class<?> root, final String resourceName, boolean required) {
     43         InputStream i = null;
     44 
     45         if (System.getSecurityManager() != null) {
     46             i = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {
     47                     public InputStream run() {
     48                         return root.getResourceAsStream(resourceName);
     49                     }
     50                 });
     51         } else {
     52             i = root.getResourceAsStream(resourceName);
     53         }
     54 
     55         if (i == null && required) {
     56             throw new MissingResourceException("could not locate data " +resourceName, root.getPackage().getName(), resourceName);
     57         }
     58         return i;
     59     }
     60 
     61     private static InputStream getStream(final ClassLoader loader, final String resourceName, boolean required) {
     62         InputStream i = null;
     63         if (System.getSecurityManager() != null) {
     64             i = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {
     65                     public InputStream run() {
     66                         return loader.getResourceAsStream(resourceName);
     67                     }
     68                 });
     69         } else {
     70             i = loader.getResourceAsStream(resourceName);
     71         }
     72         if (i == null && required) {
     73             throw new MissingResourceException("could not locate data", loader.toString(), resourceName);
     74         }
     75         return i;
     76     }
     77 
     78     public static InputStream getStream(ClassLoader loader, String resourceName){
     79         return getStream(loader,resourceName, false);
     80     }
     81 
     82     public static InputStream getRequiredStream(ClassLoader loader, String resourceName){
     83         return getStream(loader, resourceName, true);
     84     }
     85 
     86     /*
     87      * Convenience override that calls getStream(ICUData.class, resourceName, false);
     88      */
     89     public static InputStream getStream(String resourceName) {
     90         return getStream(ICUData.class, resourceName, false);
     91     }
     92 
     93     /*
     94      * Convenience method that calls getStream(ICUData.class, resourceName, true).
     95      */
     96     public static InputStream getRequiredStream(String resourceName) {
     97         return getStream(ICUData.class, resourceName, true);
     98     }
     99 
    100     /*
    101      * Convenience override that calls getStream(root, resourceName, false);
    102      */
    103     public static InputStream getStream(Class<?> root, String resourceName) {
    104         return getStream(root, resourceName, false);
    105     }
    106 
    107     /*
    108      * Convenience method that calls getStream(root, resourceName, true).
    109      */
    110     public static InputStream getRequiredStream(Class<?> root, String resourceName) {
    111         return getStream(root, resourceName, true);
    112     }
    113 }
    114