Package org.apache.commons.vfs.provider
Class AbstractFileObject
- java.lang.Object
-
- org.apache.commons.vfs.provider.AbstractFileObject
-
- All Implemented Interfaces:
FileObject
- Direct Known Subclasses:
CompressedFileFileObject,DelegateFileObject,FtpFileObject,HttpFileObject,LocalFile,RamFileObject,SftpFileObject,TarFileObject,UrlFileObject,ZipFileObject
public abstract class AbstractFileObject extends java.lang.Object implements FileObject
A partial file object implementation.- Version:
- $Revision: 484648 $ $Date: 2006-12-08 17:18:36 +0100 (Fri, 08 Dec 2006) $
- Author:
- Adam Murdoch, Gary D. Gregory
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractFileObject(FileName name, AbstractFileSystem fs)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleancanRenameTo(FileObject newfile)Queries the object if a simple rename to the filename ofnewfileis possible.protected voidchildrenChanged()Deprecated.protected voidchildrenChanged(FileName childName, FileType newType)Notifies the file that its children have changed.voidclose()Closes this file, and its content.voidcopyFrom(FileObject file, FileSelector selector)Copies another file to this file.voidcreateFile()Creates this file, if it does not exist.voidcreateFolder()Creates this folder, if it does not exist.booleandelete()Deletes this file.intdelete(FileSelector selector)Deletes this file, and all children.protected voiddoAttach()Attaches this file object to its file resource.protected voiddoCreateFolder()Creates this file as a folder.protected voiddoDelete()Deletes the file.protected voiddoDetach()Detaches this file object from its file resource.protected java.util.MapdoGetAttributes()Returns the attributes of this file.protected java.security.cert.Certificate[]doGetCertificates()Returns the certificates used to sign this file.protected abstract longdoGetContentSize()Returns the size of the file content (in bytes).protected abstract java.io.InputStreamdoGetInputStream()Creates an input stream to read the file content from.protected longdoGetLastModifiedTime()Returns the last modified time of this file.protected java.io.OutputStreamdoGetOutputStream(boolean bAppend)Creates an output stream to write the file content to.protected RandomAccessContentdoGetRandomAccessContent(RandomAccessMode mode)Creates access to the file for random i/o.protected abstract FileTypedoGetType()Determines the type of this file.protected booleandoIsHidden()Determines if this file is hidden.protected booleandoIsReadable()Determines if this file can be read.protected booleandoIsSameFile(FileObject destFile)Checks if this fileObject is the same file asdestFilejust with a different name.
E.g.protected booleandoIsWriteable()Determines if this file can be written to.protected abstract java.lang.String[]doListChildren()Lists the children of this file.protected FileObject[]doListChildrenResolved()Lists the children of this file.protected voiddoRename(FileObject newfile)Renames the file.protected voiddoSetAttribute(java.lang.String atttrName, java.lang.Object value)Sets an attribute of this file.protected voiddoSetLastModifiedTime(long modtime)Sets the last modified time of this file.protected voidendOutput()Called when the ouput stream for this file is closed.booleanexists()Determines if the file exists.protected voidfinalize()FileObject[]findFiles(FileSelector selector)Finds the set of matching descendents of this file, in depthwise order.voidfindFiles(FileSelector selector, boolean depthwise, java.util.List selected)Traverses the descendents of this file, and builds a list of selected files.FileObjectgetChild(java.lang.String name)Returns a child of this file.FileObject[]getChildren()Returns the children of the file.FileContentgetContent()Returns the file's content.protected FileContentInfoFactorygetFileContentInfoFactory()create the filecontentinfo implementationFileOperationsgetFileOperations()FileSystemgetFileSystem()Returns the file system this file belongs to.java.io.InputStreamgetInputStream()Returns an input stream to use to read the content of the file.FileNamegetName()Returns the name of the file.java.io.OutputStreamgetOutputStream()Prepares this file for writing.java.io.OutputStreamgetOutputStream(boolean bAppend)Prepares this file for writing.FileObjectgetParent()Returns the parent of the file.RandomAccessContentgetRandomAccessContent(RandomAccessMode mode)Returns an input/output stream to use to read and write the content of the file in and random manner.FileTypegetType()Returns the file's type.java.net.URLgetURL()Returns a URL representation of the file.protected voidhandleChanged()Called when this file is changed.
This will only happen if you monitor the file usingFileMonitor.protected voidhandleCreate(FileType newType)Called when this file is created.protected voidhandleDelete()Called when this file is deleted.voidholdObject(java.lang.Object strongRef)This method is meant to add a object where this object holds a strong reference then.protected voidinjectType(FileType fileType)booleanisAttached()Check if the internal state is "attached"booleanisContentOpen()Check if the content stream is openbooleanisHidden()Determines if this file can be read.booleanisReadable()Determines if this file can be read.protected booleanisSameFile(FileObject destFile)Checks if this fileObject is the same file asdestFilejust with a different name.
E.g.booleanisWriteable()Determines if this file can be written to.voidmoveTo(FileObject destFile)Moves (rename) the file to another oneprotected voidnotifyAllStreamsClosed()will be called after this file-object closed all its streams.protected voidonChange()Called when the type or content of this file changes.protected voidonChildrenChanged(FileName child, FileType newType)Called when the children of this file change.voidrefresh()This will prepare the fileObject to get resynchronized with the underlaying filesystem if requiredFileObjectresolveFile(java.lang.String path)Finds a file, relative to this file.FileObjectresolveFile(java.lang.String name, NameScope scope)Returns a child by name.java.lang.StringtoString()Returns the URI of the file.
-
-
-
Constructor Detail
-
AbstractFileObject
protected AbstractFileObject(FileName name, AbstractFileSystem fs)
-
-
Method Detail
-
doAttach
protected void doAttach() throws java.lang.ExceptionAttaches this file object to its file resource. This method is called before any of the doBlah() or onBlah() methods. Sub-classes can use this method to perform lazy initialisation. This implementation does nothing.- Throws:
java.lang.Exception
-
doDetach
protected void doDetach() throws java.lang.ExceptionDetaches this file object from its file resource.Called when this file is closed. Note that the file object may be reused later, so should be able to be reattached.
This implementation does nothing.- Throws:
java.lang.Exception
-
doGetType
protected abstract FileType doGetType() throws java.lang.Exception
Determines the type of this file. Must not return null. The return value of this method is cached, so the implementation can be expensive.- Throws:
java.lang.Exception
-
doIsHidden
protected boolean doIsHidden() throws java.lang.ExceptionDetermines if this file is hidden. Is only called ifdoGetType()does not returnFileType.IMAGINARY. This implementation always returns false.- Throws:
java.lang.Exception
-
doIsReadable
protected boolean doIsReadable() throws java.lang.ExceptionDetermines if this file can be read. Is only called ifdoGetType()does not returnFileType.IMAGINARY. This implementation always returns true.- Throws:
java.lang.Exception
-
doIsWriteable
protected boolean doIsWriteable() throws java.lang.ExceptionDetermines if this file can be written to. Is only called ifdoGetType()does not returnFileType.IMAGINARY. This implementation always returns true.- Throws:
java.lang.Exception
-
doListChildren
protected abstract java.lang.String[] doListChildren() throws java.lang.ExceptionLists the children of this file. Is only called ifdoGetType()returnsFileType.FOLDER. The return value of this method is cached, so the implementation can be expensive.- Throws:
java.lang.Exception
-
doListChildrenResolved
protected FileObject[] doListChildrenResolved() throws java.lang.Exception
Lists the children of this file. Is only called ifdoGetType()returnsFileType.FOLDER. The return value of this method is cached, so the implementation can be expensive.
Other thandoListChildrenyou could return FileObject's to e.g. reinitialize the type of the file.
(Introduced for Webdav: "permission denied on resource" during getType())- Throws:
java.lang.Exception
-
doDelete
protected void doDelete() throws java.lang.ExceptionDeletes the file. Is only called when:doGetType()does not returnFileType.IMAGINARY.doIsWriteable()returns true.- This file has no children, if a folder.
- Throws:
java.lang.Exception
-
doRename
protected void doRename(FileObject newfile) throws java.lang.Exception
Renames the file. Is only called when:doIsWriteable()returns true.
- Throws:
java.lang.Exception
-
doCreateFolder
protected void doCreateFolder() throws java.lang.ExceptionCreates this file as a folder. Is only called when:doGetType()returnsFileType.IMAGINARY.- The parent folder exists and is writeable, or this file is the root of the file system.
- Throws:
java.lang.Exception
-
onChildrenChanged
protected void onChildrenChanged(FileName child, FileType newType) throws java.lang.Exception
Called when the children of this file change. Allows subclasses to refresh any cached information about the children of this file. This implementation does nothing.- Throws:
java.lang.Exception
-
onChange
protected void onChange() throws java.lang.ExceptionCalled when the type or content of this file changes. This implementation does nothing.- Throws:
java.lang.Exception
-
doGetLastModifiedTime
protected long doGetLastModifiedTime() throws java.lang.ExceptionReturns the last modified time of this file. Is only called ifdoGetType()does not returnFileType.IMAGINARY. This implementation throws an exception.- Throws:
java.lang.Exception
-
doSetLastModifiedTime
protected void doSetLastModifiedTime(long modtime) throws java.lang.ExceptionSets the last modified time of this file. Is only called ifdoGetType()does not returnFileType.IMAGINARY. This implementation throws an exception.- Throws:
java.lang.Exception
-
doGetAttributes
protected java.util.Map doGetAttributes() throws java.lang.ExceptionReturns the attributes of this file. Is only called ifdoGetType()does not returnFileType.IMAGINARY. This implementation always returns an empty map.- Throws:
java.lang.Exception
-
doSetAttribute
protected void doSetAttribute(java.lang.String atttrName, java.lang.Object value) throws java.lang.ExceptionSets an attribute of this file. Is only called ifdoGetType()does not returnFileType.IMAGINARY. This implementation throws an exception.- Throws:
java.lang.Exception
-
doGetCertificates
protected java.security.cert.Certificate[] doGetCertificates() throws java.lang.ExceptionReturns the certificates used to sign this file. Is only called ifdoGetType()does not returnFileType.IMAGINARY. This implementation always returns null.- Throws:
java.lang.Exception
-
doGetContentSize
protected abstract long doGetContentSize() throws java.lang.ExceptionReturns the size of the file content (in bytes). Is only called ifdoGetType()returnsFileType.FILE.- Throws:
java.lang.Exception
-
doGetInputStream
protected abstract java.io.InputStream doGetInputStream() throws java.lang.ExceptionCreates an input stream to read the file content from. Is only called ifdoGetType()returnsFileType.FILE.It is guaranteed that there are no open output streams for this file when this method is called.
The returned stream does not have to be buffered.
- Throws:
java.lang.Exception
-
doGetRandomAccessContent
protected RandomAccessContent doGetRandomAccessContent(RandomAccessMode mode) throws java.lang.Exception
Creates access to the file for random i/o. Is only called ifdoGetType()returnsFileType.FILE.It is guaranteed that there are no open output streams for this file when this method is called.
- Throws:
java.lang.Exception
-
doGetOutputStream
protected java.io.OutputStream doGetOutputStream(boolean bAppend) throws java.lang.ExceptionCreates an output stream to write the file content to. Is only called if:doIsWriteable()returns true.doGetType()returnsFileType.FILE, ordoGetType()returnsFileType.IMAGINARY, and the file's parent exists and is a folder.
It is guaranteed that there are no open stream (input or output) for this file when this method is called.
The returned stream does not have to be buffered.
This implementation throws an exception.- Throws:
java.lang.Exception
-
toString
public java.lang.String toString()
Returns the URI of the file.- Overrides:
toStringin classjava.lang.Object
-
getName
public FileName getName()
Returns the name of the file.- Specified by:
getNamein interfaceFileObject
-
getFileSystem
public FileSystem getFileSystem()
Returns the file system this file belongs to.- Specified by:
getFileSystemin interfaceFileObject- Returns:
- The file system.
-
getURL
public java.net.URL getURL() throws FileSystemExceptionReturns a URL representation of the file.- Specified by:
getURLin interfaceFileObject- Throws:
FileSystemException
-
exists
public boolean exists() throws FileSystemExceptionDetermines if the file exists.- Specified by:
existsin interfaceFileObject- Returns:
trueif this file exists,falseif not.- Throws:
FileSystemException- On error determining if this file exists.
-
getType
public FileType getType() throws FileSystemException
Returns the file's type.- Specified by:
getTypein interfaceFileObject- Returns:
- One of the
FileTypeconstants. Never returns null. - Throws:
FileSystemException- On error determining the file's type.
-
isHidden
public boolean isHidden() throws FileSystemExceptionDetermines if this file can be read.- Specified by:
isHiddenin interfaceFileObject- Returns:
trueif this file is hidden,falseif not.- Throws:
FileSystemException- On error determining if this file exists.
-
isReadable
public boolean isReadable() throws FileSystemExceptionDetermines if this file can be read.- Specified by:
isReadablein interfaceFileObject- Returns:
trueif this file is readable,falseif not.- Throws:
FileSystemException- On error determining if this file exists.
-
isWriteable
public boolean isWriteable() throws FileSystemExceptionDetermines if this file can be written to.- Specified by:
isWriteablein interfaceFileObject- Returns:
trueif this file is writeable,falseif not.- Throws:
FileSystemException- On error determining if this file exists.
-
getParent
public FileObject getParent() throws FileSystemException
Returns the parent of the file.- Specified by:
getParentin interfaceFileObject- Returns:
- The folder that contains this file. Returns null if this file is the root of a file system.
- Throws:
FileSystemException- On error finding the file's parent.
-
getChildren
public FileObject[] getChildren() throws FileSystemException
Returns the children of the file.- Specified by:
getChildrenin interfaceFileObject- Returns:
- An array containing the children of this file. The array is unordered. If the file does not have any children, a zero-length array is returned. This method never returns null.
- Throws:
FileSystemException- If this file does not exist, or is not a folder, or on error listing this file's children.
-
getChild
public FileObject getChild(java.lang.String name) throws FileSystemException
Returns a child of this file.- Specified by:
getChildin interfaceFileObject- Parameters:
name- The name of the child.- Returns:
- The child, or null if there is no such child.
- Throws:
FileSystemException- If this file does not exist, or is not a folder, or on error determining this file's children.
-
resolveFile
public FileObject resolveFile(java.lang.String name, NameScope scope) throws FileSystemException
Returns a child by name.- Specified by:
resolveFilein interfaceFileObject- Parameters:
name- The name to resolve.- Returns:
- The file.
- Throws:
FileSystemException- On error parsing the path, or on error finding the file.
-
resolveFile
public FileObject resolveFile(java.lang.String path) throws FileSystemException
Finds a file, relative to this file.- Specified by:
resolveFilein interfaceFileObject- Parameters:
path- The path of the file to locate. Can either be a relative path, which is resolved relative to this file, or an absolute path, which is resolved relative to the file system that contains this file.- Returns:
- The file.
- Throws:
FileSystemException- On error parsing the path, or on error finding the file.
-
delete
public boolean delete() throws FileSystemExceptionDeletes this file.- Specified by:
deletein interfaceFileObject- Returns:
- true if this object has been deleted
- Throws:
FileSystemException- If this file is a non-empty folder, or if this file is read-only, or on error deleteing this file.
-
delete
public int delete(FileSelector selector) throws FileSystemException
Deletes this file, and all children.- Specified by:
deletein interfaceFileObject- Parameters:
selector- The selector to use to select which files to delete.- Returns:
- the number of deleted files
- Throws:
FileSystemException- If this file or one of its descendents is read-only, or on error deleting this file or one of its descendents.
-
createFile
public void createFile() throws FileSystemExceptionCreates this file, if it does not exist.- Specified by:
createFilein interfaceFileObject- Throws:
FileSystemException- If the file already exists with the wrong type, or the parent folder is read-only, or on error creating this file or one of its ancestors.
-
createFolder
public void createFolder() throws FileSystemExceptionCreates this folder, if it does not exist. Also creates any ancestor files which do not exist.- Specified by:
createFolderin interfaceFileObject- Throws:
FileSystemException- If the folder already exists with the wrong type, or the parent folder is read-only, or on error creating this folder or one of its ancestors.
-
copyFrom
public void copyFrom(FileObject file, FileSelector selector) throws FileSystemException
Copies another file to this file.- Specified by:
copyFromin interfaceFileObject- Parameters:
file- The source file to copy.selector- The selector to use to select which files to copy.- Throws:
FileSystemException- If this file is read-only, or if the source file does not exist, or on error copying the file.
-
moveTo
public void moveTo(FileObject destFile) throws FileSystemException
Moves (rename) the file to another one- Specified by:
moveToin interfaceFileObject- Parameters:
destFile- the New filename.- Throws:
FileSystemException- If this file is read-only, or if the source file does not exist, or on error copying the file.
-
isSameFile
protected boolean isSameFile(FileObject destFile) throws FileSystemException
Checks if this fileObject is the same file asdestFilejust with a different name.
E.g. for case insensitive filesystems like windows.- Throws:
FileSystemException
-
doIsSameFile
protected boolean doIsSameFile(FileObject destFile) throws FileSystemException
Checks if this fileObject is the same file asdestFilejust with a different name.
E.g. for case insensitive filesystems like windows.- Throws:
FileSystemException
-
canRenameTo
public boolean canRenameTo(FileObject newfile)
Queries the object if a simple rename to the filename ofnewfileis possible.- Specified by:
canRenameToin interfaceFileObject- Parameters:
newfile- the new filename- Returns:
- true if rename is possible
-
findFiles
public FileObject[] findFiles(FileSelector selector) throws FileSystemException
Finds the set of matching descendents of this file, in depthwise order.- Specified by:
findFilesin interfaceFileObject- Parameters:
selector- The selector to use to select matching files.- Returns:
- list of files or null if the base file (this object) do not exist
- Throws:
FileSystemException
-
getContent
public FileContent getContent() throws FileSystemException
Returns the file's content.- Specified by:
getContentin interfaceFileObject- Returns:
- This file's content.
- Throws:
FileSystemException- On error getting this file's content.
-
refresh
public void refresh() throws FileSystemExceptionThis will prepare the fileObject to get resynchronized with the underlaying filesystem if required- Specified by:
refreshin interfaceFileObject- Throws:
FileSystemException
-
close
public void close() throws FileSystemExceptionCloses this file, and its content.- Specified by:
closein interfaceFileObject- Throws:
FileSystemException- On error closing the file.- See Also:
FileContent.close()
-
getInputStream
public java.io.InputStream getInputStream() throws FileSystemExceptionReturns an input stream to use to read the content of the file.- Throws:
FileSystemException
-
getRandomAccessContent
public RandomAccessContent getRandomAccessContent(RandomAccessMode mode) throws FileSystemException
Returns an input/output stream to use to read and write the content of the file in and random manner.- Throws:
FileSystemException
-
getOutputStream
public java.io.OutputStream getOutputStream() throws FileSystemExceptionPrepares this file for writing. Makes sure it is either a file, or its parent folder exists. Returns an output stream to use to write the content of the file to.- Throws:
FileSystemException
-
getOutputStream
public java.io.OutputStream getOutputStream(boolean bAppend) throws FileSystemExceptionPrepares this file for writing. Makes sure it is either a file, or its parent folder exists. Returns an output stream to use to write the content of the file to.- Parameters:
bAppend- true when append to the file.
Note: If the underlaying filesystem do not support this, it wont work.- Throws:
FileSystemException
-
endOutput
protected void endOutput() throws java.lang.ExceptionCalled when the ouput stream for this file is closed.- Throws:
java.lang.Exception
-
handleCreate
protected void handleCreate(FileType newType) throws java.lang.Exception
Called when this file is created. Updates cached info and notifies the parent and file system.- Throws:
java.lang.Exception
-
handleDelete
protected void handleDelete() throws java.lang.ExceptionCalled when this file is deleted. Updates cached info and notifies subclasses, parent and file system.- Throws:
java.lang.Exception
-
handleChanged
protected void handleChanged() throws java.lang.ExceptionCalled when this file is changed.
This will only happen if you monitor the file usingFileMonitor.- Throws:
java.lang.Exception
-
childrenChanged
protected void childrenChanged() throws java.lang.ExceptionDeprecated.Notifies the file that its children have changed.- Throws:
java.lang.Exception
-
childrenChanged
protected void childrenChanged(FileName childName, FileType newType) throws java.lang.Exception
Notifies the file that its children have changed.- Throws:
java.lang.Exception
-
findFiles
public void findFiles(FileSelector selector, boolean depthwise, java.util.List selected) throws FileSystemException
Traverses the descendents of this file, and builds a list of selected files.- Specified by:
findFilesin interfaceFileObject- Parameters:
selector- the selector used to determine if the file should be selecteddepthwise- controls the ordering in the list. e.g. deepest firstselected- container for selected files. list needs not to be empty.- Throws:
FileSystemException
-
isContentOpen
public boolean isContentOpen()
Check if the content stream is open- Specified by:
isContentOpenin interfaceFileObject- Returns:
- true if this is the case
-
isAttached
public boolean isAttached()
Check if the internal state is "attached"- Specified by:
isAttachedin interfaceFileObject- Returns:
- true if this is the case
-
getFileContentInfoFactory
protected FileContentInfoFactory getFileContentInfoFactory()
create the filecontentinfo implementation
-
injectType
protected void injectType(FileType fileType)
-
holdObject
public void holdObject(java.lang.Object strongRef)
This method is meant to add a object where this object holds a strong reference then. E.g. a archive-filesystem creates a list of all childs and they shouldnt get garbage collected until the container is garbage collected- Parameters:
strongRef-
-
notifyAllStreamsClosed
protected void notifyAllStreamsClosed()
will be called after this file-object closed all its streams.
-
getFileOperations
public FileOperations getFileOperations() throws FileSystemException
- Specified by:
getFileOperationsin interfaceFileObject- Returns:
- FileOperations interface that provides access to the operations API.
- Throws:
FileSystemException
-
finalize
protected void finalize() throws java.lang.Throwable- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
-