//============================================================================== // FFile.java //============================================================================== package tribble.search.disk; // System imports import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.Exception; import java.lang.Long; import java.lang.String; import java.util.Date; // Local imports import tribble.search.DocumentI; import tribble.search.WritableDocumentI; /******************************************************************************* * File object. * * * @version $Revision: 1.7 $ $Date: 2001/07/16 02:10:12 $ * @since 2001-05-14 * @author * David R. Tribble * (david@tribble.com). *
* Copyright * ©2001 by David R. Tribble, all rights reserved.
* * @see FDirectory */ public class FFile implements tribble.search.WritableDocumentI { // Identification /** Revision information. */ static final String REV = "@(#)tribble/search/disk/FFile.java $Revision: 1.7 $ $Date: 2001/07/16 02:10:12 $\n"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public constants /** Series number. */ public static final int SERIES = 300; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected constants /** Attribute names. */ protected static final String[] s_attrNames = { "file", "fullpath", "length", "modified", "name", "parent", "path", }; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected variables /** File name. */ protected File m_fname; /** The directory searcher that found and created this file entry. */ protected FDirectory m_search; /** Input stream for this file. */ protected InputStream m_in; /** Output stream for this file. */ protected OutputStream m_out; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Public methods /*************************************************************************** * Determine if this directory file exists. * * @return * True if the file exists, otherwise false. * * @since 1.2, 2001-06-15 */ public boolean exists() //implements tribble.search.WritableDocumentI { // Check if this file name exists return (m_fname.exists()); } /*************************************************************************** * Determine if this directory file specifies a directory. * * @return * True if this file is a directory, otherwise false. * * @since 1.2, 2001-06-15 */ public boolean isDirectory() //implements tribble.search.WritableDocumentI { // Check if this file is a directory return (m_fname.isDirectory()); } /*************************************************************************** * Determine if this directory file is readable. * * @return * True if this file exists and can be read, otherwise false. * * @since 1.2, 2001-06-15 * * @see #getInputStream */ public boolean canRead() //implements tribble.search.WritableDocumentI { // Check if this file is readable return (m_fname.canRead()); } /*************************************************************************** * Determine if this directory file is writable. * * @return * True if the file exists and can be written to, otherwise false. * * @since 1.2, 2001-06-15 * * @see #getOutputStream */ public boolean canWrite() //implements tribble.search.WritableDocumentI { // Check if this file is writable return (m_fname.canWrite()); } /*************************************************************************** * Set the contents of this directory file to be writable. * *

* Note: * Once a file has been set to be unwritable (i.e., to be read-only), it * cannot be reset to writable. (This is a limitation of the underlying * {@link java.io.File} class.) * * @param flag * True if the file is to be made writable, false if it is not. * * @throws IOException * Thrown if the access mode of the file cannot be altered, or if some * other error occurs. * * @see #canWrite * * @since 1.6, 2001-07-02 */ public void makeWritable(boolean flag) throws IOException //implements tribble.search.WritableDocumentI { if (!flag) { // Set the file to be read-only if (!m_fname.setReadOnly()) throw new IOException("Can't make file read-only: " + m_fname.getPath()); } } /*************************************************************************** * Determine the length of this directory file. * * @return * The length of this file, in bytes. * * @since 1.2, 2001-06-15 */ public long length() //implements tribble.search.WritableDocumentI { // Retrieve the length of this file return (m_fname.length()); } /*************************************************************************** * Set the length of the contents of this directory file. * *

* This operation is not supported. * * @throws UnsupportedOperationException (unchecked) * Always thrown, because this method is not implemented for this document * type. * * @see #length * * @since 1.6, 2001-07-02 */ public void setLength(long len) throws UnsupportedOperationException //implements tribble.search.WritableDocumentI { // Not supported throw new UnsupportedOperationException("setLength() not supported"); } /*************************************************************************** * Determine the date that this directory file was last modified. * * @return * The date that the file was last modified, or null if the date is unknown. * * @since 1.2, 2001-06-15 */ public Date lastModified() //implements tribble.search.WritableDocumentI { long time; // Retrieve the modification date of this file time = m_fname.lastModified(); if (time > 0) return (new Date(time)); else return (null); } /*************************************************************************** * Set the date that this directory file was last modified. * * @param when * The new date that this file was last modified. If this is null, no action * it taken. * * @throws IOException * Thrown if the file time cannot be modified, or if some other error occurs. * * @see #lastModified * * @since 1.6, 2001-07-02 */ public void setLastModified(Date when) throws IOException //implements tribble.search.WritableDocumentI { if (when == null) return; // Set the file modification time if (!m_fname.setLastModified(when.getTime())) throw new IOException("Can't set modification time of file: " + m_fname.getPath()); } /*************************************************************************** * Retrieve the name for this directory file. * * @return * The filename (pathname) of this directory file. * * @since 1.2, 2001-06-15 */ public String getName() //implements tribble.search.WritableDocumentI { // Retrieve the full absolute path name for this file entry return (m_fname.getName()); } /*************************************************************************** * Retrieve the type of this directory file. * * @return * The type of the file (which is the filename extension or suffix, which can * be empty ""). Such a type name is typically suitable for display * by some user interface. * * @since 1.2, 2001-06-15 */ public String getType() //implements tribble.search.WritableDocumentI { String ext; int off; // Retrieve the filename type (extension/suffix) of this file entry ext = m_fname.getName(); off = ext.lastIndexOf('.'); if (off >= 0) ext = ext.substring(off+1); else ext = ""; return (ext); } /*************************************************************************** * Set the type of this directory file. * *

* This operation is not supported. * * @throws UnsupportedOperationException (unchecked) * Always thrown, because this method is not implemented for this document * type. * * @see #getType * * @since 1.6, 2001-07-02 */ public void setType(String type) throws UnsupportedOperationException //implements tribble.search.WritableDocumentI { // Unsupported operation throw new UnsupportedOperationException("setType() not supported"); } /*************************************************************************** * Retrieve the attribute names for this directory file. * * @return * An array containing the names of the attributes associated with this file, * or null if it has none. * * @throws Exception * Thrown if the information about the file is unobtainable, or if some * other error occurs. * * @since 1.5, 2001-06-30 */ public String[] getAttributeNames() //implements tribble.search.WritableDocumentI { return (s_attrNames); } /*************************************************************************** * Add a new attribute name to this document. * *

* This operation is not supported. * * @throws UnsupportedOperationException (unchecked) * Always thrown, because this method is not implemented for this document * type. * * @see #getAttribute * @see #setAttribute * @see #getAttributeNames * * @since 1.6, 2001-07-02 */ public void addAttribute(String name) throws UnsupportedOperationException //implements tribble.search.WritableDocumentI { throw new UnsupportedOperationException("addAttribute() not supported"); } /*************************************************************************** * Retrieve the value of an attribute for this directory file. * * @param key * The name of an attribute associated with this directory file. * The following attribute names are supported: *

    *    "file"          File
    *    "fullpath"      String
    *    "length"        Long
    *    "modified"      Date
    *    "name"          String
    *    "parent"        String
    *    "path"          String 
* * @return * The value of the specified attribute, or null if this file has no such * attribute. Note that the value is returned as a generic Object, * meaning that it must be cast into the appropriate type. * * @throws Exception * Thrown if the information about the file is unobtainable, or if some * other error occurs. * * @since 1.5, 2001-06-30 */ public Object getAttribute(String key) //implements tribble.search.WritableDocumentI { // Retrieve the value of the named attribute if (key == "file") return (m_fname); // File else if (key == "fullpath") return (m_fname.getAbsolutePath()); // String else if (key == "length") return (new Long(length())); // Long else if (key == "modified") return (lastModified()); // Date else if (key == "name") return (m_fname.getName()); // String else if (key == "parent") return (m_fname.getParent()); // String else if (key == "path") return (m_fname.getPath()); // String else if (key.equalsIgnoreCase("file")) return (getAttribute("file")); else if (key.equalsIgnoreCase("length")) return (getAttribute("length")); else if (key.equalsIgnoreCase("modified")) return (getAttribute("modified")); else if (key.equalsIgnoreCase("name")) return (getAttribute("name")); else if (key.equalsIgnoreCase("parent")) return (getAttribute("parent")); else if (key.equalsIgnoreCase("path")) return (getAttribute("path")); else throw new IllegalArgumentException("Unknown attribute: '" + key + "'"); } /*************************************************************************** * Set the value of an attribute for this document. * This method is not supported. * * @throws UnsupportedOperationException (unchecked) * Always thrown, because this method is not implemented for this document * type. * * @since 1.5, 2001-06-30 */ public void setAttribute(String attr, Object val) throws UnsupportedOperationException //implements tribble.search.WritableDocumentI { // Not supported throw new UnsupportedOperationException("setAttribute"); } /*************************************************************************** * Get a readable input stream for this directory file. * * @return * An input stream, from which the data contents of this directory file can * be read. * * @throws IOException * Thrown if the file cannot be accessed, or if some other error occurs. * * @since 1.1, 2001-05-14 */ public synchronized InputStream getInputStream() throws IOException //implements tribble.search.WritableDocumentI { FileInputStream in; // Close the previously opened stream, if any if (m_in != null) { m_in.close(); m_in = null; } // Open a writable output stream for this file in = new FileInputStream(m_fname); m_in = in; return (m_in); } /*************************************************************************** * Get a writable output stream for this directory file. * * @return * An output stream, to which the data contents of this directory file can * be written. * * @throws IOException * Thrown if the file cannot be accessed, or if some other error occurs. * * @since 1.1, 2001-05-14 */ public synchronized OutputStream getOutputStream() throws IOException //implements tribble.search.WritableDocumentI { FileOutputStream out; // Close the previously opened stream, if any if (m_out != null) { m_out.close(); m_out = null; } // Open a writable output stream for this file out = new FileOutputStream(m_fname); m_out = out; return (m_out); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Protected constructors /*************************************************************************** * Constructor. * * @param dir * The directory searcher that created (and owns) this file. * * @param fname * The filename of this file. * * @since 1.2, 2001-06-15 */ protected FFile(FDirectory dir, File fname) { // Establish this directory file m_fname = fname; m_search = dir; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Package private methods /*************************************************************************** * Establish the input stream for this file. * * @param in * The input stream to associate with this file. * * @since 1.1, 2001-05-14 */ void setInput(InputStream in) { // Establish the input stream for this file m_in = in; } /*************************************************************************** * Establish the output stream for this file. * * @param out * The output stream to associate with this file. * * @since 1.1, 2001-05-14 */ void setOutput(OutputStream out) { // Establish the output stream for this file m_out = out; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Private constructors /*************************************************************************** * Default constructor. * * @since 1.1, 2001-05-14 */ private FFile() { // Do nothing } } // End FFile.java