< prev index next >

src/java.base/share/classes/sun/misc/JarIndex.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 import java.io.*;
  29 import java.util.*;
  30 import java.util.jar.*;
  31 import java.util.zip.*;
















  32 
  33 /**
  34  * This class is used to maintain mappings from packages, classes
  35  * and resources to their enclosing JAR files. Mappings are kept
  36  * at the package level except for class or resource files that
  37  * are located at the root directory. URLClassLoader uses the mapping
  38  * information to determine where to fetch an extension class or
  39  * resource from.
  40  *
  41  * @author  Zhenghua Li
  42  * @since   1.3
  43  */
  44 
  45 public class JarIndex {
  46 
  47     /**
  48      * The hash map that maintains mappings from
  49      * package/classe/resource to jar file list(s)
  50      */
  51     private HashMap<String,LinkedList<String>> indexMap;


  57     private HashMap<String,LinkedList<String>> jarMap;
  58 
  59     /*
  60      * An ordered list of jar file names.
  61      */
  62     private String[] jarFiles;
  63 
  64     /**
  65      * The index file name.
  66      */
  67     public static final String INDEX_NAME = "META-INF/INDEX.LIST";
  68 
  69     /**
  70      * true if, and only if, sun.misc.JarIndex.metaInfFilenames is set to true.
  71      * If true, the names of the files in META-INF, and its subdirectories, will
  72      * be added to the index. Otherwise, just the directory names are added.
  73      */
  74     private static final boolean metaInfFilenames =
  75         "true".equals(System.getProperty("sun.misc.JarIndex.metaInfFilenames"));
  76 



  77     /**
  78      * Constructs a new, empty jar index.
  79      */
  80     public JarIndex() {
  81         indexMap = new HashMap<>();
  82         jarMap = new HashMap<>();
  83     }
  84 
  85     /**
  86      * Constructs a new index from the specified input stream.
  87      *
  88      * @param is the input stream containing the index data
  89      */
  90     public JarIndex(InputStream is) throws IOException {
  91         this();
  92         read(is);
  93     }
  94 
  95     /**
  96      * Constructs a new index for the specified list of jar files.


 117     }
 118 
 119     /**
 120      * Returns the jar index, or <code>null</code> if none.
 121      *
 122      * @param jar the JAR file to get the index from.
 123      * @exception IOException if an I/O error has occurred.
 124      */
 125     public static JarIndex getJarIndex(JarFile jar, MetaIndex metaIndex) throws IOException {
 126         JarIndex index = null;
 127         /* If metaIndex is not null, check the meta index to see
 128            if META-INF/INDEX.LIST is contained in jar file or not.
 129         */
 130         if (metaIndex != null &&
 131             !metaIndex.mayContain(INDEX_NAME)) {
 132             return null;
 133         }
 134         JarEntry e = jar.getJarEntry(INDEX_NAME);
 135         // if found, then load the index
 136         if (e != null) {








 137             index = new JarIndex(jar.getInputStream(e));

 138         }
 139         return index;
 140     }
 141 
 142     /**
 143      * Returns the jar files that are defined in this index.
 144      */
 145     public String[] getJarFiles() {
 146         return jarFiles;
 147     }
 148 
 149     /*
 150      * Add the key, value pair to the hashmap, the value will
 151      * be put in a linked list which is created if necessary.
 152      */
 153     private void addToList(String key, String value,
 154                            HashMap<String,LinkedList<String>> t) {
 155         LinkedList<String> list = t.get(key);
 156         if (list == null) {
 157             list = new LinkedList<>();




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.BufferedWriter;
  30 import java.io.ByteArrayInputStream;
  31 import java.io.File;
  32 import java.io.IOException;
  33 import java.io.InputStream;
  34 import java.io.InputStreamReader;
  35 import java.io.OutputStream;
  36 import java.io.OutputStreamWriter;
  37 import java.nio.ByteBuffer;
  38 import java.util.Enumeration;
  39 import java.util.HashMap;
  40 import java.util.Iterator;
  41 import java.util.LinkedList;
  42 import java.util.Map;
  43 import java.util.Vector;
  44 import java.util.jar.JarEntry;
  45 import java.util.jar.JarFile;
  46 import java.util.zip.ZipEntry;
  47 import java.util.zip.ZipFile;
  48 
  49 /**
  50  * This class is used to maintain mappings from packages, classes
  51  * and resources to their enclosing JAR files. Mappings are kept
  52  * at the package level except for class or resource files that
  53  * are located at the root directory. URLClassLoader uses the mapping
  54  * information to determine where to fetch an extension class or
  55  * resource from.
  56  *
  57  * @author  Zhenghua Li
  58  * @since   1.3
  59  */
  60 
  61 public class JarIndex {
  62 
  63     /**
  64      * The hash map that maintains mappings from
  65      * package/classe/resource to jar file list(s)
  66      */
  67     private HashMap<String,LinkedList<String>> indexMap;


  73     private HashMap<String,LinkedList<String>> jarMap;
  74 
  75     /*
  76      * An ordered list of jar file names.
  77      */
  78     private String[] jarFiles;
  79 
  80     /**
  81      * The index file name.
  82      */
  83     public static final String INDEX_NAME = "META-INF/INDEX.LIST";
  84 
  85     /**
  86      * true if, and only if, sun.misc.JarIndex.metaInfFilenames is set to true.
  87      * If true, the names of the files in META-INF, and its subdirectories, will
  88      * be added to the index. Otherwise, just the directory names are added.
  89      */
  90     private static final boolean metaInfFilenames =
  91         "true".equals(System.getProperty("sun.misc.JarIndex.metaInfFilenames"));
  92 
  93     private static final sun.misc.JavaUtilZipFileAccess zipAccess
  94             = sun.misc.SharedSecrets.getJavaUtilZipFileAccess();
  95 
  96     /**
  97      * Constructs a new, empty jar index.
  98      */
  99     public JarIndex() {
 100         indexMap = new HashMap<>();
 101         jarMap = new HashMap<>();
 102     }
 103 
 104     /**
 105      * Constructs a new index from the specified input stream.
 106      *
 107      * @param is the input stream containing the index data
 108      */
 109     public JarIndex(InputStream is) throws IOException {
 110         this();
 111         read(is);
 112     }
 113 
 114     /**
 115      * Constructs a new index for the specified list of jar files.


 136     }
 137 
 138     /**
 139      * Returns the jar index, or <code>null</code> if none.
 140      *
 141      * @param jar the JAR file to get the index from.
 142      * @exception IOException if an I/O error has occurred.
 143      */
 144     public static JarIndex getJarIndex(JarFile jar, MetaIndex metaIndex) throws IOException {
 145         JarIndex index = null;
 146         /* If metaIndex is not null, check the meta index to see
 147            if META-INF/INDEX.LIST is contained in jar file or not.
 148         */
 149         if (metaIndex != null &&
 150             !metaIndex.mayContain(INDEX_NAME)) {
 151             return null;
 152         }
 153         JarEntry e = jar.getJarEntry(INDEX_NAME);
 154         // if found, then load the index
 155         if (e != null) {
 156             long size = e.getSize();
 157             if (size > 0 && size < 512 * 1024) {
 158                 byte[] bytes = new byte[(int) size];
 159                 ByteBuffer bb = ByteBuffer.wrap(bytes);
 160                 zipAccess.fillByteBuffer(jar, e, bb);
 161                 index = new JarIndex(new ByteArrayInputStream(bytes, 0, bb.position()));
 162             } else {
 163                 // Unknown or too large ZipEntry size to use a array
 164                 index = new JarIndex(jar.getInputStream(e));
 165             }
 166         }
 167         return index;
 168     }
 169 
 170     /**
 171      * Returns the jar files that are defined in this index.
 172      */
 173     public String[] getJarFiles() {
 174         return jarFiles;
 175     }
 176 
 177     /*
 178      * Add the key, value pair to the hashmap, the value will
 179      * be put in a linked list which is created if necessary.
 180      */
 181     private void addToList(String key, String value,
 182                            HashMap<String,LinkedList<String>> t) {
 183         LinkedList<String> list = t.get(key);
 184         if (list == null) {
 185             list = new LinkedList<>();


< prev index next >