Class IFile
- java.lang.Object
-
- lumis.portal.filesystem.IFile
-
- All Implemented Interfaces:
java.io.Serializable
,IInputStreamProvider
- Direct Known Subclasses:
ClusterMirroredLocalDataFile
,LocalDataFile
@StableMinor(version="16.1", sinceVersion="8.0") public abstract class IFile extends java.lang.Object implements java.io.Serializable, IInputStreamProvider
Represents a LumisPortal file or directory that may or may not exist.It uses a
PortalFilePath
to normalize the file or directory among the cluster servers and exposes the supported file and directory operations. Once created, itsPortalFilePath
is immutable. ThePortalFilePath
also is used to check whether thisIFile
is equal to another (seeequals(Object)
).In some implementations the changes made in one cluster server might take a while to be reflected in the other servers.
Implementations must raise the appropriate filesystem events as file systems changes are performed using this API.
Implementations must guarantee the read/write file integrity. The following must be guaranteed (even in highly concurrent scenarios):
- The
read operation
must be able to read the entire file content without garbling with simultaneous writes - The
write operation
must be able to write the entire file content without garbling with simultaneous writes
Any changes must take effect immediately, so that if the value is read immediately after the change operation, the returned value is the one that has been set, once the commands are called in the same cluster server.
However, the same functionality is not guarantee if the change operation and the read operation are called in different cluster servers.
But if a file operation is performed through acluster transmission
with atotal order
flag set, this file operation (in any server) must happen after any file operation performed before the transmission was sent in the cluster server that sent the transmission.- Since:
- 8.0.0
- Version:
- $Revision: 26156 $ $Date: 2024-01-13 10:26:46 -0300 (Sat, 13 Jan 2024) $
- See Also:
lumis.portal.filesystem
,lumis.portal.filesystem.event
,IFileSystem
,IFileSystemManager.getFile(PortalFilePath)
, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description IFile(PortalFilePath portalFilePath)
Creates a new instance with the given PortalFilePath.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
copyRegularFileTo(IFile dest)
Copies this file, assuming it is a regular file, to the specified destination.void
copyTo(IFile dest)
Copies the file denoted by this logical filesystem object.abstract void
delete()
Deletes the file or directory.abstract void
deleteRecursively()
Tries to delete the file or directory recursively.boolean
equals(java.lang.Object obj)
Makes use ofPortalFilePath
to check if this object represents the same file system logical object represented by the given object.abstract boolean
exists()
Tests whether the file or directory exists.java.lang.String
getExtension()
Returns this file's extension ornull
if this file doesn't have an extension.abstract java.io.InputStream
getInputStream()
Returns anInputStream
to be used to read this file.abstract long
getLastModified()
Returns the time that the file or directory was last modified.abstract long
getLength()
Returns the length of the file.java.lang.String
getName()
Returns the name of the file or directory.abstract java.io.OutputStream
getOutputStream()
Returns anOutputStream
that writes to this file.IFile
getParent()
Returns the parentdirectory
.PortalFilePath
getPath()
Returns thePortalFilePath
that represents this file or directory.IFile
getRoot()
Returns the root folder of this file.int
hashCode()
Makes use ofPortalFilePath
to calculate this hashcode.boolean
isDescendentOf(IFile file)
Returns whether this file is descendent of the given file.abstract boolean
isDirectory()
Tests whether the file is a directory.boolean
isEmpty()
Returns whether this directory is empty.abstract boolean
isFile()
Tests whether the file denoted by thisIFile
is an actual file.boolean
isRoot()
Returns whether this file is the root of its base folder.abstract IFile[]
listFiles()
Lists all immediate sub files and directories of this directory (the files and directories whosegetParent()
is equal to this).IFile[]
listFiles(IFileFilter fileFilter)
Lists all immediate sub files and directories of this directory (the files and directories whosegetParent()
is equal to this), respecting a givenfile filter
.abstract void
mkdirs()
Creates the directory named by this logical filesystem object, including any necessary but nonexistent parent directories.byte[]
readBytes()
Reads this file content in bytes.java.lang.String
readString()
Reads this file using theUTF-8
encoding.java.lang.String
readString(java.lang.String charset)
Reads this file using the given charset name.java.lang.String
relativize(IFile other)
Relativizes the given file against this file.void
renameTo(IFile dest)
Renames the file denoted by this logical filesystem object.IFile
resolve(java.lang.String relativePath)
Resolves the given path against this file.abstract void
setLastModified(long time)
Sets the last-modified time of the file or directory.java.lang.String
toString()
abstract java.net.URL
toURL()
Returns a URL that represents this file.void
write(byte[] buf)
Writes the given bytes in this file.void
write(java.io.File file)
Writes the contents of the given file into this file.void
write(java.io.InputStream inputStream)
Copies the given input stream's content into this file.void
write(java.lang.String str)
Writes the given string in this file usingUTF-8
encoding.void
write(java.lang.String str, java.lang.String charset)
Writes the given string in this file using the given charset name.
-
-
-
Constructor Detail
-
IFile
public IFile(PortalFilePath portalFilePath)
Creates a new instance with the given PortalFilePath.- Parameters:
portalFilePath
- the portal file path.- Since:
- 8.0.0
-
-
Method Detail
-
getName
public final java.lang.String getName()
Returns the name of the file or directory. This is just the last name in the name sequence. If the relative path is empty (seePortalFilePath.getRelativePath()
), then the empty string is returned.- Returns:
- The name of the file or directory, or the empty string if this name sequence is empty
- Since:
- 8.0.0
- See Also:
PortalFilePath.getName()
-
getParent
public IFile getParent()
Returns the parentdirectory
. If this is already the root of thebase folder
defined inportal file path
,null
is returned. In other words, ifgetPath()
.getRelativePath()
is an empty string,null
is returned.- Returns:
- the parent
directory
- Since:
- 8.0.0
- See Also:
PortalFilePath.getParent()
-
resolve
public IFile resolve(java.lang.String relativePath)
Resolves the given path against this file. Thepath
must not be an absolute path. In other words, it cannot begin with a/
.The path of a file is composed by elements separated by
/
. The resolve process consists in create a new file using this file's relative path concatenated with a/
and the given relative path. If the given relative path contains an element..
it means this..
and the previous element will be eliminated (for examplemy/folder/../file.txt
will becomemy/file.txt
).- Parameters:
relativePath
- the relative path to be resolved.- Returns:
- a file relative to this one.
- Throws:
java.lang.IllegalArgumentException
- ifpath
isnull
or it starts with a/
.- Since:
- 8.0.0
- See Also:
PortalFilePath.resolve(String)
-
isRoot
public final boolean isRoot()
Returns whether this file is the root of its base folder.- Returns:
- whether this file is the root of its base folder.
- Since:
- 8.0.0
-
isDescendentOf
public final boolean isDescendentOf(IFile file)
Returns whether this file is descendent of the given file.- Parameters:
file
- the file to be tested.- Returns:
- whether this file is descendent of the given file.
- Since:
- 8.0.0
-
relativize
public final java.lang.String relativize(IFile other)
Relativizes the given file against this file. Returns the relativized path ornull
if the given file couldn't be relativized. The given file can only be relativized against this one if itis descendent of this file
.- Parameters:
other
- the file to be relativized.- Returns:
- the relative path or
null
if it couldn't be relativized. - Since:
- 8.0.0
- See Also:
PortalFilePath.relativize(PortalFilePath)
-
getRoot
public IFile getRoot()
Returns the root folder of this file. If this fileis already the root
, then an equivalent object will be returned.- Returns:
- the root folder of this file.
- Since:
- 8.0.0
-
isEmpty
public final boolean isEmpty() throws java.io.FileNotFoundException, FileIsNotDirectoryException, java.io.IOException
Returns whether this directory is empty.- Returns:
- whether this directory is empty.
- Throws:
java.io.FileNotFoundException
- if this file does not exist.FileIsNotDirectoryException
- if this file does not denote a directory.java.io.IOException
- if other error occur.- Since:
- 8.0.0
-
getPath
public final PortalFilePath getPath()
Returns thePortalFilePath
that represents this file or directory. This method never returnsnull
.- Returns:
- the
PortalFilePath
that represents this file or directory - Since:
- 8.0.0
-
getExtension
public final java.lang.String getExtension() throws java.io.IOException
Returns this file's extension ornull
if this file doesn't have an extension. The extension is the remaining part offile name
after the last.
character.- Returns:
- this file's extension.
- Throws:
java.io.IOException
- if some error occur.- Since:
- 8.0.0
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
hashCode
public final int hashCode()
Makes use ofPortalFilePath
to calculate this hashcode. Indeed, they are exactly the same.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hashcode.
- Since:
- 8.0.0
-
equals
public final boolean equals(java.lang.Object obj)
Makes use ofPortalFilePath
to check if this object represents the same file system logical object represented by the given object.Indeed, the following is always true for two (not
null
) givenIFile
— file1 and file2:
file1.equals(file2)
if and only iffile1.getPath().equals(file2.getPath())
if
file1.getPath().equals(file2.getPath())
istrue
thenfile1.equals(file2)
is alsotrue
- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- the other object to be tested against- Returns:
- true if this object is equal to the given object, false otherwise
- Since:
- 8.0.0
-
exists
public abstract boolean exists() throws java.io.IOException
Tests whether the file or directory exists.- Returns:
true
if and only if the file or directory exists;false
otherwise- Throws:
java.io.IOException
- if some error occur during this operation- Since:
- 8.0.0
-
isDirectory
public abstract boolean isDirectory() throws java.io.IOException
Tests whether the file is a directory.- Returns:
true
if and only if the file exists and is a directory;false
otherwise- Throws:
java.io.IOException
- if some error occur during this operation- Since:
- 8.0.0
-
isFile
public abstract boolean isFile() throws java.io.IOException
Tests whether the file denoted by thisIFile
is an actual file. This test might be system-dependent.- Returns:
true
if and only if the file denoted by thisIFile
exists and is an actual file;false
otherwise- Throws:
java.io.IOException
- if some error occur during this operation- Since:
- 8.0.0
-
setLastModified
public abstract void setLastModified(long time) throws java.io.FileNotFoundException, java.io.IOException
Sets the last-modified time of the file or directory.- Parameters:
time
- The new last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)- Throws:
java.lang.IllegalArgumentException
- If the argument is negativejava.io.FileNotFoundException
- If this file does not existjava.io.IOException
- if other error occur- Since:
- 8.0.0
-
getLastModified
public abstract long getLastModified() throws java.io.FileNotFoundException, java.io.IOException
Returns the time that the file or directory was last modified.- Returns:
- A
long
value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970) - Throws:
java.io.FileNotFoundException
- If this file does not existjava.io.IOException
- if other error occur- Since:
- 8.0.0
-
getLength
public abstract long getLength() throws java.io.FileNotFoundException, NotFileException, java.io.IOException
Returns the length of the file.- Returns:
- The length, in bytes, of the file.
- Throws:
NotFileException
- if this is not a filejava.io.FileNotFoundException
- If this file does not existjava.io.IOException
- if other error occur- Since:
- 8.0.0
-
delete
public abstract void delete() throws FileCouldNotBeDeletedException, java.io.IOException
Deletes the file or directory. If this denotes a directory, then the directory must be empty in order to be deleted. If this file doesn't exist already, no exception will be raised.- Throws:
FileCouldNotBeDeletedException
- if this file could not be deletedjava.io.IOException
- if other error occur- Since:
- 8.0.0
-
deleteRecursively
public abstract void deleteRecursively() throws FileCouldNotBeDeletedException, java.io.IOException
Tries to delete the file or directory recursively. If the deletion of some file or directory fails the deletion will not be complete, but some files and/or directories might already have been deleted.In other words, it is not guaranteed that either all files and directories will be deleted or none of the files and directories will be deleted.
If this file doesn't exist already, no exception will be raised.
- Throws:
FileCouldNotBeDeletedException
- if some file or directory couldn't be deletedjava.io.IOException
- if other error occur- Since:
- 8.0.0
-
listFiles
public abstract IFile[] listFiles() throws java.io.FileNotFoundException, FileIsNotDirectoryException, java.io.IOException
Lists all immediate sub files and directories of this directory (the files and directories whosegetParent()
is equal to this).- Returns:
- all immediate sub files and directories of this directory; an empty list if this directory has no files and no directories.
- Throws:
java.io.FileNotFoundException
- if this file doesn't existFileIsNotDirectoryException
- if this file does not denote a directoryjava.io.IOException
- if other error occur- Since:
- 8.0.0
-
listFiles
public IFile[] listFiles(IFileFilter fileFilter) throws java.io.FileNotFoundException, FileIsNotDirectoryException, java.io.IOException
Lists all immediate sub files and directories of this directory (the files and directories whosegetParent()
is equal to this), respecting a givenfile filter
.- Parameters:
fileFilter
- the file filter to be applied when searching sub files- Returns:
- all immediate sub files and directories that matches the specified file filter of this directory; an empty list if this directory has no files and no directories.
- Throws:
java.io.FileNotFoundException
- if this file doesn't existFileIsNotDirectoryException
- if this file does not denote a directoryjava.io.IOException
- if other error occur- Since:
- 8.0.0
-
mkdirs
public abstract void mkdirs() throws DirectoryCouldNotBeCreatedException, java.io.IOException
Creates the directory named by this logical filesystem object, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.- Throws:
DirectoryCouldNotBeCreatedException
- if some of the needed directories could not be createdjava.io.IOException
- if other error occur- Since:
- 8.0.0
-
renameTo
public void renameTo(IFile dest) throws FileCouldNotBeRenamedException, java.io.FileNotFoundException, java.io.IOException
Renames the file denoted by this logical filesystem object. This operation is equivalent to:copyTo(dest);
deleteRecursively();copyTo(IFile)
anddeleteRecursively()
.- Parameters:
dest
- The new logical filesystem object for the named file- Throws:
java.lang.IllegalArgumentException
- ifdest
isnull
java.io.FileNotFoundException
- if this file doesn't existFileCouldNotBeRenamedException
- if the file could not be renamed for some reasonjava.io.IOException
- if other error occur- Since:
- 8.0.0
-
copyTo
public void copyTo(IFile dest) throws java.io.FileNotFoundException, FileCouldNotBeCopiedException, java.io.IOException
Copies the file denoted by this logical filesystem object.This operation will copy this file or directory to the target file or directory using the following criteria:
- If this file does not exist, a
FileNotFoundException
will be raised. - If this file is a regular file:
- If the target file does not exist, the target file will be created using this file's content.
- If the target file is an already existent regular file, the target's content will be replaced by this file's content.
- If the target file is not a regular file, a
FileNotFoundException
will be raised.
- If this file is a directory:
- If the target file does not exist, the target file will be created as a directory and this file's children will be copied recursively.
- If the target file is a directory, this file's children will be copied recursively. Those files that already existed in the target but does not exist in this file will not be removed.
- If the target file exists and is not a directory, a
FileNotFoundException
will be raised.
- If this file exists but it isn't neither a regular file nor a directory, a
FileNotFoundException
will be raised.
- Parameters:
dest
- The new logical filesystem object for the named file- Throws:
java.io.FileNotFoundException
- if this file doesn't existFileCouldNotBeCopiedException
- if some error occur during copyjava.io.IOException
- if other error occur- Since:
- 8.0.0
- If this file does not exist, a
-
copyRegularFileTo
protected void copyRegularFileTo(IFile dest) throws java.io.IOException
Copies this file, assuming it is a regular file, to the specified destination.- Parameters:
dest
- the copy destination.- Throws:
java.io.IOException
- if I/O error occurred.- Since:
- 12.0.0
-
getInputStream
public abstract java.io.InputStream getInputStream() throws java.io.IOException
Returns anInputStream
to be used to read this file.If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a
FileNotFoundException
is thrown.The stream must be closed by the caller of this method. A try-finally statement is recommended to guarantee it is closed.
- Specified by:
getInputStream
in interfaceIInputStreamProvider
- Returns:
- an InputStream to be used to read this file.
- Throws:
java.io.FileNotFoundException
- If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for readingjava.io.IOException
- If an I/O error occurred- Since:
- 8.0.0
-
getOutputStream
public abstract java.io.OutputStream getOutputStream() throws java.io.IOException
Returns anOutputStream
that writes to this file.Any existent content in the file will be discarded and replaced with the content written to this output stream.
The new file content will only be available after
OutputStream.close()
is called.
If the file exists but is a directory rather than a regular file, does not exist but cannot be created, the parent directory does not exist, or cannot be opened for any other reason then a
FileNotFoundException
is thrown. This exception can be thrown in at any time between this call and the call ofOutputStream.close()
.The stream must be closed by the caller of this method. A try-finally statement is recommended to guarantee it is closed.
- Returns:
- an OutputStream to be used to write this file.
- Throws:
java.io.FileNotFoundException
- If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reasonjava.io.IOException
- If an I/O error occurred- Since:
- 8.0.0
-
readString
public final java.lang.String readString() throws java.io.IOException
Reads this file using theUTF-8
encoding.- Returns:
- the file content.
- Throws:
java.io.IOException
- Since:
- 8.0.0
-
readString
public final java.lang.String readString(java.lang.String charset) throws java.io.IOException
Reads this file using the given charset name.- Parameters:
charset
- the charset name- Returns:
- the file content.
- Throws:
java.io.IOException
- Since:
- 8.0.0
-
readBytes
public byte[] readBytes() throws java.io.IOException
Reads this file content in bytes.- Returns:
- this file content in bytes.
- Throws:
java.io.IOException
- Since:
- 8.0.0
-
write
public final void write(byte[] buf) throws java.io.IOException
Writes the given bytes in this file.- Parameters:
buf
- the bytes to be written.- Throws:
java.io.IOException
- Since:
- 8.0.0
-
write
public final void write(java.lang.String str, java.lang.String charset) throws java.io.IOException
Writes the given string in this file using the given charset name.- Parameters:
str
- the string to be written.charset
- the charset name- Throws:
java.io.IOException
- Since:
- 8.0.0
-
write
public final void write(java.lang.String str) throws java.io.IOException
Writes the given string in this file usingUTF-8
encoding.- Parameters:
str
- the string to be written.- Throws:
java.io.IOException
- Since:
- 8.0.0
-
write
public void write(java.io.InputStream inputStream) throws java.io.IOException
Copies the given input stream's content into this file.- Parameters:
inputStream
- the input stream.- Throws:
java.io.IOException
- Since:
- 8.0.0
-
write
public final void write(java.io.File file) throws java.io.IOException
Writes the contents of the given file into this file.- Parameters:
file
- the file with the desired contents.- Throws:
java.io.IOException
- if some error occur.- Since:
- 8.0.0
-
toURL
public abstract java.net.URL toURL() throws java.io.IOException
Returns a URL that represents this file. The URL must, at least, provide means of accessing this file'sinput stream
by implementingURLConnection.getInputStream()
.- Returns:
- a URL that represents this file.
- Throws:
java.io.IOException
- if some error occur.- Since:
- 8.0.0
-
-