path.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,564 行 · 第 1/3 页

JAVA
1,564
字号
   * Gets an attribute of the object.   */  public Object getAttribute(String name) throws IOException  {    return null;  }  /**   * Returns a iterator of all attribute names set for this object.   * @return null if path has no attributes.   */  public Iterator getAttributeNames() throws IOException  {    return null;  }  /**   * Opens a resin ReadStream for reading.   */  public final ReadStream openRead() throws IOException  {    StreamImpl impl = openReadImpl();    impl.setPath(this);    return new ReadStream(impl);  }  /**   * Opens a resin WriteStream for writing.   */  public final WriteStream openWrite() throws IOException  {    StreamImpl impl = openWriteImpl();    impl.setPath(this);    return new WriteStream(impl);  }  /**   * Opens a resin ReadWritePair for reading and writing.   *   * <p>A chat channel, for example, would open its socket using this   * interface.   */  public ReadWritePair openReadWrite() throws IOException  {    StreamImpl impl = openReadWriteImpl();    impl.setPath(this);    WriteStream writeStream = new WriteStream(impl);    ReadStream readStream = new ReadStream(impl, writeStream);    return new ReadWritePair(readStream, writeStream);  }  /**   * Opens a resin ReadWritePair for reading and writing.   *   * <p>A chat channel, for example, would open its socket using this   * interface.   *   * @param is pre-allocated ReadStream to be initialized   * @param os pre-allocated WriteStream to be initialized   */  public void openReadWrite(ReadStream is, WriteStream os) throws IOException  {    StreamImpl impl = openReadWriteImpl();    impl.setPath(this);    os.init(impl);    is.init(impl, os);  }  /**   * Opens a resin stream for appending.   */  public WriteStream openAppend() throws IOException  {    StreamImpl impl = openAppendImpl();    return new WriteStream(impl);  }  /**   * Opens a random-access stream.   */  public RandomAccessStream openRandomAccess() throws IOException  {    throw new UnsupportedOperationException(getClass().getName());  }  /**   * Creates the file named by this Path and returns true if the   * file is new.   */  public boolean createNewFile() throws IOException  {    synchronized (LOCK) {      if (! exists()) {        WriteStream s = openWrite();        s.close();        return true;      }    }    return false;  }  /**   * Creates a dependency.   */  public PersistentDependency createDepend()  {    return new Depend(this);  }  /**   * Creates a unique temporary file as a child of this directory.   *   * @param prefix filename prefix   * @param suffix filename suffix, defaults to .tmp   * @return Path to the new file.   */  public Path createTempFile(String prefix, String suffix) throws IOException  {    if (prefix == null || prefix.length () == 0)      prefix = "t";    if (suffix == null)      suffix = ".tmp";    synchronized (LOCK) {      for (int i = 0; i < 32768; i++) {        int r = Math.abs((int) RandomUtil.getRandomLong());        Path file = lookup(prefix + r + suffix);        if (file.createNewFile())          return file;      }    }    throw new IOException("cannot create temp file");  }  /**   * Creates a link named by this path to another path.   *   * @param target the target of the link   * @param hardLink true if the link should be a hard link   */  public boolean createLink(Path target, boolean hardLink)    throws IOException  {    throw new UnsupportedOperationException(getScheme() + ": doesn't support createLink");  }  /**   * Returns the target path from the link.   * Returns null for a non-link.   */  public String readLink()  {    return null;  }  /**   * Returns the actual path from the link.   */  public String realPath()  {    return getFullPath();  }  /**   * Utility to write the contents of this path to the destination stream.   *   * @param os destination stream.   */  public void writeToStream(OutputStream os)    throws IOException  {    StreamImpl is = openReadImpl();    TempBuffer tempBuffer = TempBuffer.allocate();    try {      byte []buffer = tempBuffer.getBuffer();      int length = buffer.length;      int len;      while ((len = is.read(buffer, 0, length)) > 0)        os.write(buffer, 0, len);    } finally {      TempBuffer.free(tempBuffer);      tempBuffer = null;      is.close();    }  }  /**   * Utility to write the contents of this path to the destination stream.   *   * @param os destination stream.   */  public void writeToStream(OutputStreamWithBuffer os)    throws IOException  {    StreamImpl is = openReadImpl();    try {      byte []buffer = os.getBuffer();      int offset = os.getBufferOffset();      int length = buffer.length;      while (true) {        int sublen = length - offset;        if (sublen <= 0) {          buffer = os.nextBuffer(offset);          offset = 0;          sublen = length;        }        sublen = is.read(buffer, offset, sublen);        if (sublen <= 0) {          os.setBufferOffset(offset);          return;        }        offset += sublen;      }    } finally {      is.close();    }  }  /**   * Returns the crc64 code.   */  public long getCrc64()  {    try {      if (isDirectory()) {        String []list = list();        long digest = 0;        for (int i = 0; i < list.length; i++) {          digest = Crc64.generate(digest, list[i]);        }        return digest;      }      else if (canRead()) {        ReadStream is = openRead();        try {          long digest = 0;	  byte []buffer = is.getBuffer();	  while (is.fillBuffer() > 0) {	    int length = is.getLength();            digest = Crc64.generate(digest, buffer, 0, length);          }          return digest;        } finally {          is.close();        }      }      else {        return -1; // Depend requires -1      }    } catch (IOException e) {      // XXX: log      e.printStackTrace();      return -1;    }  }  /**   * Returns the object at this path.  Normally, only paths like JNDI   * will support this.   */  public Object getObject()    throws IOException  {    throw new UnsupportedOperationException(getScheme() + ": doesn't support getObject");  }  /**   * Sets the object at this path.  Normally, only paths like JNDI   * will support this.   */  public void setObject(Object obj)    throws IOException  {    throw new UnsupportedOperationException(getScheme() + ": doesn't support setObject");  }  public int hashCode()  {    return toString().hashCode();  }  public boolean equals(Object o)  {    if (this == o)      return true;    else if (! (o instanceof Path))      return false;    else      return getURL().equals(((Path) o).getURL());  }  public String toString()  {    return getFullPath();  }  public StreamImpl openReadImpl() throws IOException  {    throw new UnsupportedOperationException("openRead:" + getClass().getName());  }  public StreamImpl openWriteImpl() throws IOException  {    throw new UnsupportedOperationException("openWrite:" + getClass().getName());  }  public StreamImpl openReadWriteImpl() throws IOException  {    throw new UnsupportedOperationException("openReadWrite:" + getClass().getName());  }  public StreamImpl openAppendImpl() throws IOException  {    throw new UnsupportedOperationException("openAppend:" + getClass().getName());  }  protected static String escapeURL(String rawURL)  {    CharBuffer cb = null;    int length = rawURL.length();    for (int i = 0; i < length; i++) {      char ch = rawURL.charAt(i);      switch (ch) {      case ' ':        if (cb == null) {          cb = new CharBuffer();          cb.append(rawURL, 0, i);        }        cb.append("%20");        break;      case '#':        if (cb == null) {          cb = new CharBuffer();          cb.append(rawURL, 0, i);        }        cb.append("%23");        break;      case '%':        if (cb == null) {          cb = new CharBuffer();          cb.append(rawURL, 0, i);        }        cb.append("%25");        break;      default:        if (cb != null)          cb.append(ch);        break;      }    }    if (cb != null)      return cb.toString();    else      return rawURL;  }  protected Path copy()  {    return this;  }  /**   * Copy for caching.   */  protected Path cacheCopy()  {    return this;  }  public static final void setDefaultSchemeMap(SchemeMap schemeMap)  {    _defaultSchemeMap = schemeMap;    _pathLookupCache.clear();  }  public static final boolean isWindows()  {    return _separatorChar == '\\' || _isTestWindows;  }  public static final void setTestWindows(boolean isTest)  {    _isTestWindows = isTest;  }  protected static final char getSeparatorChar()  {    return _separatorChar;  }  public static final char getFileSeparatorChar()  {    return _separatorChar;  }  public static final char getPathSeparatorChar()  {    return _pathSeparatorChar;  }  protected static String getUserDir()  {    return System.getProperty("user.dir");  }  public static String getNewlineString()  {    if (_newline == null) {      _newline = System.getProperty("line.separator");      if (_newline == null)        _newline = "\n";    }    return _newline;  }  private class ArrayIterator implements Iterator<String> {    String []list;    int index;    public boolean hasNext() { return index < list.length; }    public String next() { return index < list.length ? list[index++] : null; }    public void remove() { throw new UnsupportedOperationException(); }    ArrayIterator(String []list)    {      this.list = list;      index = 0;    }  }  static class PathKey {    private Path _parent;    private String _lookup;    PathKey()    {    }    PathKey(Path parent, String lookup)    {      _parent = parent;      _lookup = lookup;    }    void init(Path parent, String lookup)    {      _parent = parent;      _lookup = lookup;    }    public int hashCode()    {      if (_parent != null)	return _parent.hashCode() * 65521 + _lookup.hashCode();      else	return _lookup.hashCode();    }    public boolean equals(Object test)    {      if (! (test instanceof PathKey))        return false;      PathKey key = (PathKey) test;      if (_parent != null)	return (_parent.equals(key._parent) && _lookup.equals(key._lookup));      else	return (key._parent == null && _lookup.equals(key._lookup));    }  }  static {    DEFAULT_SCHEME_MAP.put("file", new FilePath(null));    //DEFAULT_SCHEME_MAP.put("jar", new JarScheme(null));    DEFAULT_SCHEME_MAP.put("http", new HttpPath("127.0.0.1", 0));    DEFAULT_SCHEME_MAP.put("https", new HttpsPath("127.0.0.1", 0));    DEFAULT_SCHEME_MAP.put("tcp", new TcpPath(null, null, null, "127.0.0.1", 0));    DEFAULT_SCHEME_MAP.put("tcps", new TcpsPath(null, null, null, "127.0.0.1", 0));    StreamImpl stdout = StdoutStream.create();    StreamImpl stderr = StderrStream.create();    DEFAULT_SCHEME_MAP.put("stdout", stdout.getPath());    DEFAULT_SCHEME_MAP.put("stderr", stderr.getPath());    VfsStream nullStream = new VfsStream(null, null);    DEFAULT_SCHEME_MAP.put("null", new ConstPath(null, nullStream));    DEFAULT_SCHEME_MAP.put("jndi", new JndiPath());  }}

⌨️ 快捷键说明

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