/** * Get the name of this URLResource * (its file component minus the leading separator). * @return the name of this resource. */ public synchronized String getName() { if (isReference()) { return getCheckedRef().getName(); } String name = getURL().getFile(); return name.isEmpty() ? name : name.substring(1); }
/** * Get the URL used by this URLResource. * @return a URL object. */ public synchronized URL getURL() { if (isReference()) { return ((URLResource) getCheckedRef()).getURL(); } if (url == null) { if (baseURL != null) { if (relPath == null) { throw new BuildException("must provide relativePath" + " attribute when using baseURL."); } try { url = new URL(baseURL, relPath); } catch (MalformedURLException e) { throw new BuildException(e); } } } return url; }
/** * Return this URLResource formatted as a String. * @return a String representation of this URLResource. */ public synchronized String toString() { return isReference() ? getCheckedRef().toString() : String.valueOf(getURL()); }
/** * Tells if the resource is a directory. * @return boolean whether the resource is a directory. */ public synchronized boolean isDirectory() { return isReference() ? getCheckedRef().isDirectory() : getName().endsWith("/"); }
/** * Get the hash code for this Resource. * @return hash code as int. */ public synchronized int hashCode() { if (isReference()) { return getCheckedRef().hashCode(); } return MAGIC * ((getURL() == null) ? NULL_URL : getURL().hashCode()); }
/** * Find out whether the URL exists . * @return true if this resource exists. */ public synchronized boolean isExists() { if (isReference()) { return getCheckedRef().isExists(); } return isExists(false); }
/** * Get an OutputStream for the Resource. * @return an OutputStream to which content can be written. * @throws IOException if unable to provide the content of this * Resource as a stream. * @throws UnsupportedOperationException if OutputStreams are not * supported for this Resource type. * @throws IOException if the URL cannot be opened. */ public synchronized OutputStream getOutputStream() throws IOException { if (isReference()) { return getCheckedRef().getOutputStream(); } connect(); try { return conn.getOutputStream(); } finally { conn = null; } }
/** * Get an InputStream for the Resource. * @return an InputStream containing this Resource's content. * @throws IOException if unable to provide the content of this * Resource as a stream. * @throws UnsupportedOperationException if InputStreams are not * supported for this Resource type. */ public synchronized InputStream getInputStream() throws IOException { if (isReference()) { return getCheckedRef().getInputStream(); } connect(); try { return conn.getInputStream(); } finally { conn = null; } }
/** * Get the size of this Resource. * @return the size, as a long, 0 if the Resource does not exist (for * compatibility with java.io.File), or UNKNOWN_SIZE if not known. */ public synchronized long getSize() { if (isReference()) { return getCheckedRef().getSize(); } if (!isExists(false)) { return 0L; } return withConnection(c -> conn.getContentLength(), UNKNOWN_SIZE); }
/** * Tells the modification time in milliseconds since 01.01.1970 . * * @return 0 if the resource does not exist to mirror the behavior * of {@link java.io.File File}. */ public synchronized long getLastModified() { if (isReference()) { return getCheckedRef().getLastModified(); } if (!isExists(false)) { return UNKNOWN_DATETIME; } return withConnection(c -> conn.getLastModified(), UNKNOWN_DATETIME); }
/** * Test whether an Object equals this URLResource. * @param another the other Object to compare. * @return true if the specified Object is equal to this Resource. */ public synchronized boolean equals(Object another) { if (this == another) { return true; } if (isReference()) { return getCheckedRef().equals(another); } if (another == null || another.getClass() != getClass()) { return false; } URLResource other = (URLResource) another; return getURL() == null ? other.getURL() == null : getURL().equals(other.getURL()); }