/** * Return an InputStream for reading the contents of this Resource. * @return an InputStream object. * @throws IOException if an error occurs. */ @Override public InputStream getInputStream() throws IOException { return isReference() ? getCheckedRef().getInputStream() : Files.newInputStream(getNotNullFile().toPath()); }
/** * Learn whether the resource is a directory. * @return boolean flag indicating if the resource is a directory. */ @Override public boolean isDirectory() { return isReference() ? getCheckedRef().isDirectory() : getNotNullFile().isDirectory(); }
/** * Learn whether this file exists. * @return true if this resource exists. */ @Override public boolean isExists() { return isReference() ? getCheckedRef().isExists() : getNotNullFile().exists(); }
/** * Get the size of this Resource. * @return the size, as a long, 0 if the Resource does not exist. */ @Override public long getSize() { return isReference() ? getCheckedRef().getSize() : getNotNullFile().length(); }
/** * Get the modification time in milliseconds since 01.01.1970 . * @return 0 if the resource does not exist. */ @Override public long getLastModified() { return isReference() ? getCheckedRef().getLastModified() : getNotNullFile().lastModified(); }
/** * Get the string representation of this Resource. * @return this FileResource formatted as a String. */ @Override public String toString() { if (isReference()) { return getCheckedRef().toString(); } if (file == null) { return "(unbound file resource)"; } String absolutePath = file.getAbsolutePath(); return FILE_UTILS.normalize(absolutePath).getAbsolutePath(); }
/** * Get the hash code for this Resource. * @return hash code as int. */ @Override public int hashCode() { if (isReference()) { return getCheckedRef().hashCode(); } return MAGIC * (getFile() == null ? NULL_FILE : getFile().hashCode()); }
/** * Implement the Touchable interface. * @param modTime new last modification time. */ @Override public void touch(long modTime) { if (isReference()) { getCheckedRef().touch(modTime); return; } if (!getNotNullFile().setLastModified(modTime)) { log("Failed to change file modification time", Project.MSG_WARN); } }
/** * Return the basedir to which the name is relative. * @return the basedir as File. */ public File getBaseDir() { if (isReference()) { return getCheckedRef().getBaseDir(); } dieOnCircularReference(); return baseDir; }
/** * Fulfill the ResourceCollection contract. * @return whether this Resource is a FileResource. */ @Override public boolean isFilesystemOnly() { if (isReference()) { return getCheckedRef().isFilesystemOnly(); } dieOnCircularReference(); return true; }
/** * {@inheritDoc} */ @Override public OutputStream getAppendOutputStream() throws IOException { if (isReference()) { return getCheckedRef().getAppendOutputStream(); } return getOutputStream(true); }
/** * 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. */ @Override public OutputStream getOutputStream() throws IOException { if (isReference()) { return getCheckedRef().getOutputStream(); } return getOutputStream(false); }
/** * Compare another Object to this FileResource for equality. * @param another the other Object to compare. * @return true if another is a FileResource representing the same file. */ @Override public boolean equals(Object another) { if (this == another) { return true; } if (isReference()) { return getCheckedRef().equals(another); } if (another == null || !(another.getClass().equals(getClass()))) { return false; } FileResource otherfr = (FileResource) another; return getFile() == null ? otherfr.getFile() == null : getFile().equals(otherfr.getFile()) && getName().equals(otherfr.getName()); }
/** * Get the name of this FileResource. If the basedir is set, * the name will be relative to that. Otherwise the basename * only will be returned. * @return the name of this resource. */ @Override public String getName() { if (isReference()) { return getCheckedRef().getName(); } File b = getBaseDir(); return b == null ? getNotNullFile().getName() : FILE_UTILS.removeLeadingPath(b, getNotNullFile()); }
/** * Get the file represented by this FileResource. * @return the File. */ @Override public File getFile() { if (isReference()) { return getCheckedRef().getFile(); } dieOnCircularReference(); synchronized (this) { if (file == null) { //try to resolve file set via basedir/name property setters: File d = getBaseDir(); String n = super.getName(); if (n != null) { setFile(FILE_UTILS.resolveFile(d, n)); } } } return file; }
/** * Compare this FileResource to another Resource. * @param another the other Resource against which to compare. * @return a negative integer, zero, or a positive integer as this FileResource * is less than, equal to, or greater than the specified Resource. */ @Override public int compareTo(Resource another) { if (isReference()) { return getCheckedRef().compareTo(another); } if (this.equals(another)) { return 0; } FileProvider otherFP = another.as(FileProvider.class); if (otherFP != null) { File f = getFile(); if (f == null) { return -1; } File of = otherFP.getFile(); if (of == null) { return 1; } int compareFiles = f.compareTo(of); return compareFiles != 0 ? compareFiles : getName().compareTo(another.getName()); } return super.compareTo(another); }