jar.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 953 行 · 第 1/2 页

JAVA
953
字号
  public StreamImpl openReadImpl(Path path) throws IOException  {    String pathName = path.getPath();    if (pathName.length() > 0 && pathName.charAt(0) == '/')      pathName = pathName.substring(1);    ZipFile zipFile = new ZipFile(_backing.getNativePath());    ZipEntry entry;    InputStream is = null;    try {      entry = zipFile.getEntry(pathName);      if (entry != null) {	is = zipFile.getInputStream(entry);	return new ZipStreamImpl(zipFile, is, null, path);      }      else {	throw new FileNotFoundException(path.toString());      }    } finally {      if (is == null) {	zipFile.close();      }    }  }  public String toString()  {    return _backing.toString();  }  /**   * Clears any cached JarFile.   */  public void clearCache()  {    JarFile jarFile = null;    synchronized (this) {      SoftReference<JarFile> jarFileRef = _jarFileRef;      _jarFileRef = null;            if (jarFileRef != null)	jarFile = jarFileRef.get();    }          try {      if (jarFile != null)	jarFile.close();    } catch (Exception e) {    }  }  private ZipEntry getJarEntry(String path)    throws IOException  {    if (path.startsWith("/"))      path = path.substring(1);    JarFile jarFile = getJarFile();    if (jarFile != null)      return jarFile.getJarEntry(path);    else      return null;  }  /**   * Returns the Java ZipFile for this Jar.  Accessing the entries with   * the ZipFile is faster than scanning through them.   *   * getJarFile is not thread safe.   */  private JarFile getJarFile()    throws IOException  {    JarFile jarFile = null;    isCacheValid();        SoftReference<JarFile> jarFileRef = _jarFileRef;    if (jarFileRef != null) {      jarFile = jarFileRef.get();	      if (jarFile != null)        return jarFile;    }    SoftReference<JarFile> oldJarRef = _jarFileRef;    _jarFileRef = null;    JarFile oldFile = null;    if (oldJarRef == null) {    }    else if (_closeJarFileRef == null)      _closeJarFileRef = oldJarRef;    else      oldFile = oldJarRef.get();    if (oldFile != null) {      try {	oldFile.close();      } catch (Throwable e) {	e.printStackTrace();      }    }    if (_backingIsFile) {      try {        jarFile = new JarFile(_backing.getNativePath());      }      catch (IOException ex) {        if (log.isLoggable(Level.FINE))          log.log(Level.FINE, L.l("Error opening jar file '{0}'", _backing.getNativePath()));        throw ex;      }      _jarFileRef = new SoftReference<JarFile>(jarFile);      _jarLastModified = getLastModifiedImpl();    }    return jarFile;  }    /**   * Returns the last modified time for the path.   *   * @param path path into the jar.   *   * @return the last modified time of the jar in milliseconds.   */  private long getLastModifiedImpl()  {    isCacheValid();        return _lastModified;  }    /**   * Returns the last modified time for the path.   *   * @param path path into the jar.   *   * @return the last modified time of the jar in milliseconds.   */  private boolean isCacheValid()  {    long now = Alarm.getCurrentTime();    if ((now - _lastTime < 1000) && ! Alarm.isTest())      return true;    long oldLastModified = _lastModified;    long oldLength = _length;        _lastModified = _backing.getLastModified();    _length = _backing.getLength();    _lastTime = now;    if (_lastModified == oldLastModified && _length == oldLength)      return true;    else {      // If the file has changed, close the old file      SoftReference<JarFile> oldFileRef = _jarFileRef;	      _jarFileRef = null;      _jarLastModified = 0;      _depend = null;      _isSigned = null;      JarFile oldCloseFile = null;      if (_closeJarFileRef != null)	oldCloseFile = _closeJarFileRef.get();      _closeJarFileRef = oldFileRef;      if (oldCloseFile != null) {	try {	  oldCloseFile.close();	} catch (Throwable e) {	}      }      return false;    }  }  /**   * Closes any old jar waiting for close.   */  private void closeJarFile()  {    JarFile jarFile = null;        synchronized (this) {      if (_closeJarFileRef != null)	jarFile = _closeJarFileRef.get();      _closeJarFileRef = null;    }        if (jarFile != null) {      try {	jarFile.close();      } catch (IOException e) {	log.log(Level.WARNING, e.toString(), e);      }    }  }  public void close()  {    removeEvent();  }    public void removeEvent()  {    JarFile jarFile = null;        synchronized (this) {      if (_jarFileRef != null)	jarFile = _jarFileRef.get();      _jarFileRef = null;    }    try {      if (jarFile != null)	jarFile.close();    } catch (Throwable e) {      log.log(Level.FINE, e.toString(), e);    }        closeJarFile();  }  public boolean equals(Object o)  {    if (this == o)      return true;    else if (o == null || ! getClass().equals(o.getClass()))      return false;    Jar jar = (Jar) o;    return _backing.equals(jar._backing);  }  /**   * Clears all the cached files in the jar.  Needed to avoid some   * windows NT issues.   */  public static void clearJarCache()  {    LruCache<Path,Jar> jarCache = _jarCache;        if (jarCache == null)      return;    ArrayList<Jar> jars = new ArrayList<Jar>();        synchronized (jarCache) {      Iterator<Jar> iter = jarCache.values();            while (iter.hasNext())	jars.add(iter.next());    }    for (int i = 0; i < jars.size(); i++) {      Jar jar = jars.get(i);              if (jar != null)	jar.clearCache();    }  }  /**   * StreamImpl to read from a ZIP file.   */  static class ZipStreamImpl extends StreamImpl {    private ZipFile _zipFile;    private InputStream _zis;    private InputStream _is;    /**     * Create the new stream  impl.     *     * @param zis the underlying zip stream.     * @param is the backing stream.     * @param path the path to the jar entry.     */    ZipStreamImpl(ZipFile file, InputStream zis, InputStream is, Path path)    {      _zipFile = file;      _zis = zis;      _is = is;            setPath(path);    }    /**     * Returns true since this is a read stream.     */    public boolean canRead() { return true; }     public int getAvailable() throws IOException    {      if (_zis == null)        return -1;      else        return _zis.available();    }     public int read(byte []buf, int off, int len) throws IOException    {      int readLen = _zis.read(buf, off, len);       return readLen;    }     public void close() throws IOException    {      ZipFile zipFile = _zipFile;      _zipFile = null;            InputStream zis = _zis;      _zis = null;            InputStream is = _is;      _is = null;            try {	if (zis != null)	  zis.close();      } catch (Throwable e) {      }      try {	if (zipFile != null)	  zipFile.close();      } catch (Throwable e) {      }      if (is != null)        is.close();    }    protected void finalize()      throws IOException    {      close();    }  }  static class JarDepend extends CachedDependency    implements PersistentDependency {    private Depend _depend;    private boolean _isDigestModified;        /**     * Create a new dependency.     *     * @param source the source file     */    JarDepend(Depend depend)    {      _depend = depend;    }        /**     * Create a new dependency.     *     * @param source the source file     */    JarDepend(Depend depend, long digest)    {      _depend = depend;      _isDigestModified = _depend.getDigest() != digest;    }    /**     * Returns the underlying depend.     */    Depend getDepend()    {      return _depend;    }    /**     * Returns true if the dependency is modified.     */    public boolean isModifiedImpl()    {      return _isDigestModified || _depend.isModified();    }    /**     * Returns true if the dependency is modified.     */    public boolean logModified(Logger log)    {      return _depend.logModified(log);    }    /**     * Returns the string to recreate the Dependency.     */    public String getJavaCreateString()    {      String sourcePath = _depend.getPath().getPath();      long digest = _depend.getDigest();            return ("new com.caucho.vfs.Jar.createDepend(" +	      "com.caucho.vfs.Vfs.lookup(\"" + sourcePath + "\"), " +	      digest + "L)");    }    public String toString()    {      return "Jar$JarDepend[" + _depend.getPath() + "]";    }  }  static class JarDigestDepend implements PersistentDependency {    private JarDepend _jarDepend;    private Depend _depend;    private boolean _isDigestModified;        /**     * Create a new dependency.     *     * @param source the source file     */    JarDigestDepend(JarDepend jarDepend, long digest)    {      _jarDepend = jarDepend;      _depend = jarDepend.getDepend();      _isDigestModified = _depend.getDigest() != digest;    }    /**     * Returns true if the dependency is modified.     */    public boolean isModified()    {      return _isDigestModified || _jarDepend.isModified();    }    /**     * Returns true if the dependency is modified.     */    public boolean logModified(Logger log)    {      return _depend.logModified(log) || _jarDepend.logModified(log);    }    /**     * Returns the string to recreate the Dependency.     */    public String getJavaCreateString()    {      String sourcePath = _depend.getPath().getPath();      long digest = _depend.getDigest();            return ("new com.caucho.vfs.Jar.createDepend(" +	      "com.caucho.vfs.Vfs.lookup(\"" + sourcePath + "\"), " +	      digest + "L)");    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?