public abstract class VirtualFile extends UserDataHolderBase implements ModificationTracker
Represents a file in VirtualFileSystem
. A particular file is represented by equal
VirtualFile
instances for the entire lifetime of the IDE process, unless the file
is deleted, in which case isValid()
will return false
.
VirtualFile instances are created on request, so there can be several instances corresponding to the same file.
All of them are equal, have the same hashCode
and use shared storage for all related data, including user data
(see UserDataHolder
).
If an in-memory implementation of VirtualFile is required, LightVirtualFile
can be used.
Please see Virtual File System for a high-level overview.
VirtualFileSystem
,
VirtualFileManager
,
VfsUtil
Modifier and Type | Class and Description |
---|---|
static interface |
VirtualFile.PropName
Acceptable values for "propertyName" argument of
VFilePropertyChangeEvent() . |
Modifier and Type | Field and Description |
---|---|
static VirtualFile[] |
EMPTY_ARRAY |
static java.lang.String |
PROP_ENCODING
Used as a property name in the
VirtualFilePropertyEvent fired when the encoding of a VirtualFile changes. |
static java.lang.String |
PROP_HIDDEN
Used as a property name in the
VirtualFilePropertyEvent fired when a visibility of a VirtualFile changes. |
static java.lang.String |
PROP_NAME
Used as a property name in the
VirtualFilePropertyEvent fired when the name of a VirtualFile changes. |
static java.lang.String |
PROP_SYMLINK_TARGET
Used as a property name in the
VirtualFilePropertyEvent fired when a symlink target of a VirtualFile changes. |
static java.lang.String |
PROP_WRITABLE
Used as a property name in the
VirtualFilePropertyEvent fired when write permission of a VirtualFile changes. |
EVER_CHANGED, NEVER_CHANGED
Modifier | Constructor and Description |
---|---|
protected |
VirtualFile() |
Modifier and Type | Method and Description |
---|---|
abstract byte [] |
contentsToByteArray()
Returns file content as an array of bytes.
|
byte [] |
contentsToByteArray(boolean cacheContent)
Returns file content as an array of bytes.
|
VirtualFile |
copy(java.lang.Object requestor,
VirtualFile newParent,
java.lang.String copyName) |
VirtualFile |
createChildData(java.lang.Object requestor,
java.lang.String name)
Creates a new file in this directory.
|
VirtualFile |
createChildDirectory(java.lang.Object requestor,
java.lang.String name)
Creates a subdirectory in this directory.
|
void |
delete(java.lang.Object requestor)
Deletes this file.
|
boolean |
exists() |
VirtualFile |
findChild(java.lang.String name)
Finds child of this file with the given name.
|
VirtualFile |
findFileByRelativePath(java.lang.String relPath)
Finds file by path relative to this file.
|
VirtualFile |
findOrCreateChildData(java.lang.Object requestor,
java.lang.String name) |
byte [] |
getBOM() |
VirtualFile |
getCanonicalFile()
Resolves all symbolic links containing in a path to this file and returns a link target.
|
java.lang.String |
getCanonicalPath()
Resolves all symbolic links containing in a path to this file and returns a path to a link target (in platform-independent format).
|
java.nio.charset.Charset |
getCharset() |
abstract VirtualFile[] |
getChildren()
Gets the child files.
|
java.lang.String |
getDetectedLineSeparator() |
java.lang.String |
getExtension()
Gets the extension of this file.
|
abstract VirtualFileSystem |
getFileSystem()
Gets the
VirtualFileSystem this file belongs to. |
FileType |
getFileType()
Returns the
FileType of this file, or FileTypes.UNKNOWN if a type cannot be determined
(i.e. |
abstract java.io.InputStream |
getInputStream()
Gets the
InputStream for this file. |
abstract long |
getLength()
File length in bytes.
|
long |
getModificationCount() |
long |
getModificationStamp()
Gets modification stamp value.
|
abstract java.lang.String |
getName()
Gets the name of this file.
|
java.lang.CharSequence |
getNameSequence() |
java.lang.String |
getNameWithoutExtension()
Gets the file name without the extension.
|
java.io.OutputStream |
getOutputStream(java.lang.Object requestor)
Creates the
OutputStream for this file. |
abstract java.io.OutputStream |
getOutputStream(java.lang.Object requestor,
long newModificationStamp,
long newTimeStamp)
Gets the
OutputStream for this file and sets modification stamp and time stamp to the specified values
after closing the stream. |
abstract VirtualFile |
getParent()
Gets the parent
VirtualFile . |
abstract java.lang.String |
getPath()
Gets the path of this file.
|
java.lang.String |
getPresentableName() |
java.lang.String |
getPresentableUrl()
Fetches "presentable URL" of this file.
|
abstract long |
getTimeStamp()
Gets the timestamp for this file.
|
java.lang.String |
getUrl()
Returns the URL of this file.
|
boolean |
is(VFileProperty property)
Checks whether this file has a specific property.
|
boolean |
isCharsetSet() |
abstract boolean |
isDirectory()
Checks whether this file is a directory.
|
boolean |
isInLocalFileSystem() |
boolean |
isRecursiveOrCircularSymLink()
Returns
true if this file is a symlink that is either recursive (i.e. |
abstract boolean |
isValid()
Checks whether this
VirtualFile is valid. |
static boolean |
isValidName(java.lang.String name)
Deprecated.
|
abstract boolean |
isWritable()
Checks whether this file could be modified.
|
void |
move(java.lang.Object requestor,
VirtualFile newParent)
Moves this file to another directory.
|
protected boolean |
nameEquals(java.lang.String name) |
void |
refresh(boolean asynchronous,
boolean recursive)
Refreshes the cached file information from the physical file system.
|
abstract void |
refresh(boolean asynchronous,
boolean recursive,
java.lang.Runnable postRunnable)
The same as
refresh(boolean, boolean) but also runs postRunnable
after the operation is completed. |
void |
rename(java.lang.Object requestor,
java.lang.String newName)
Renames this file to the
newName . |
void |
setBinaryContent(byte [] content) |
void |
setBinaryContent(byte [] content,
long newModificationStamp,
long newTimeStamp) |
void |
setBinaryContent(byte [] content,
long newModificationStamp,
long newTimeStamp,
java.lang.Object requestor) |
void |
setBOM(byte [] BOM) |
void |
setCharset(java.nio.charset.Charset charset) |
void |
setCharset(java.nio.charset.Charset charset,
java.lang.Runnable whenChanged) |
void |
setCharset(java.nio.charset.Charset charset,
java.lang.Runnable whenChanged,
boolean fireEventsWhenChanged) |
void |
setDetectedLineSeparator(java.lang.String separator) |
void |
setPreloadedContentHint(byte[] preloadedContentHint) |
void |
setWritable(boolean writable) |
protected void |
storeCharset(java.nio.charset.Charset charset) |
java.lang.String |
toString() |
changeUserMap, clearUserData, clone, copyCopyableDataTo, copyUserDataTo, getCopyableUserData, getUserData, getUserDataString, getUserMap, isUserDataEmpty, putCopyableUserData, putUserData, putUserDataIfAbsent, replace, setUserMap
public static final VirtualFile[] EMPTY_ARRAY
public static final java.lang.String PROP_NAME
VirtualFilePropertyEvent
fired when the name of a VirtualFile
changes.public static final java.lang.String PROP_ENCODING
VirtualFilePropertyEvent
fired when the encoding of a VirtualFile
changes.public static final java.lang.String PROP_WRITABLE
VirtualFilePropertyEvent
fired when write permission of a VirtualFile
changes.public static final java.lang.String PROP_HIDDEN
VirtualFilePropertyEvent
fired when a visibility of a VirtualFile
changes.public static final java.lang.String PROP_SYMLINK_TARGET
VirtualFilePropertyEvent
fired when a symlink target of a VirtualFile
changes.public abstract java.lang.String getName()
getNameSequence()
public java.lang.CharSequence getNameSequence()
public abstract VirtualFileSystem getFileSystem()
VirtualFileSystem
this file belongs to.VirtualFileSystem
public abstract java.lang.String getPath()
VirtualFileSystem
. Format of the path depends on the concrete file system.
For LocalFileSystem
it is an absolute file path with file separator characters
(File.separatorChar
) replaced to the forward slash ('/'
).public java.lang.String getUrl()
Returns the URL of this file. The URL is a string that uniquely identifies a file in all file systems.
It has the following format: <protocol>://<path>
.
File can be found by its URL using VirtualFileManager.findFileByUrl(java.lang.String)
method.
Please note these URLs are intended for use withing VFS - meaning they are not necessarily RFC-compliant.
VirtualFileManager.findFileByUrl(java.lang.String)
,
getPath()
,
VirtualFileSystem.getProtocol()
public final java.lang.String getPresentableUrl()
VirtualFileSystem.extractPresentableUrl(java.lang.String)
public java.lang.String getExtension()
public java.lang.String getNameWithoutExtension()
getName()
is returned.public void rename(java.lang.Object requestor, java.lang.String newName) throws java.io.IOException
Renames this file to the newName
.
This method should only be called within write action
.
requestor
- any object to control who called this method. Note that
it is considered to be an external change if requestor
is null
.
See VirtualFileEvent.getRequestor()
newName
- the new file namejava.io.IOException
- if file failed to be renamedpublic abstract boolean isWritable()
true
if this file is writable, false
otherwisepublic void setWritable(boolean writable) throws java.io.IOException
java.io.IOException
public abstract boolean isDirectory()
true
if this file is a directory, false
otherwisepublic boolean is(VFileProperty property)
true
if the file has a specific property, false
otherwisepublic java.lang.String getCanonicalPath()
Resolves all symbolic links containing in a path to this file and returns a path to a link target (in platform-independent format).
Note: please use this method judiciously. In most cases VFS clients don't need to resolve links in paths and should work with those provided by a user.
getPath()
if there are no symbolic links in a file's path;
getCanonicalFile().getPath()
if the link was successfully resolved;
null
otherwisepublic VirtualFile getCanonicalFile()
Resolves all symbolic links containing in a path to this file and returns a link target.
Note: please use this method judiciously. In most cases VFS clients don't need to resolve links in paths and should work with those provided by a user.
this
if there are no symbolic links in a file's path;
instance of VirtualFile
if the link was successfully resolved;
null
otherwisepublic abstract boolean isValid()
VirtualFile
is valid. File can be invalidated either by deleting it or one of its
parents with delete(java.lang.Object)
method or by an external change.
If file is not valid only Object.equals(java.lang.Object)
, Object.hashCode()
,
getName()
, getPath()
, getUrl()
, getPresentableUrl()
and methods from
UserDataHolder
can be called for it. Using any other methods for an invalid VirtualFile
instance
produce unpredictable results.true
if this is a valid file, false
otherwisepublic abstract VirtualFile getParent()
VirtualFile
.null
if this file is a root directorypublic abstract VirtualFile[] getChildren()
null
if this file is not a directorypublic VirtualFile findChild(java.lang.String name)
name
- the file name to search bynull
otherwisepublic VirtualFile findOrCreateChildData(java.lang.Object requestor, java.lang.String name) throws java.io.IOException
java.io.IOException
public FileType getFileType()
FileType
of this file, or FileTypes.UNKNOWN
if a type cannot be determined
(i.e. file type is not registered via FileTypeRegistry
).
Performance notice: consider using FileTypeRegistry.isFileOfType(com.intellij.openapi.vfs.VirtualFile, com.intellij.openapi.fileTypes.FileType)
or FileTypeRegistry.getFileTypeByFileName(java.lang.CharSequence)
if this method is to be called for massive file collections, e.g. in BulkFileListener
. See FileTypeRegistry
javadoc
for the details.
FileTypeRegistry
public VirtualFile findFileByRelativePath(java.lang.String relPath)
relPath
- the relative path with / used as separatorsnull
otherwisepublic VirtualFile createChildDirectory(java.lang.Object requestor, java.lang.String name) throws java.io.IOException
Application.runWriteAction(java.lang.Runnable)
.requestor
- any object to control who called this method. Note that
it is considered to be an external change if requestor
is null
.
See VirtualFileEvent.getRequestor()
name
- directory nameVirtualFile
representing the created directoryjava.io.IOException
- if directory failed to be createdpublic VirtualFile createChildData(java.lang.Object requestor, java.lang.String name) throws java.io.IOException
Application.runWriteAction(java.lang.Runnable)
.requestor
- any object to control who called this method. Note that
it is considered to be an external change if requestor
is null
.
See VirtualFileEvent.getRequestor()
VirtualFile
representing the created filejava.io.IOException
- if file failed to be createdpublic void delete(java.lang.Object requestor) throws java.io.IOException
Application.runWriteAction(java.lang.Runnable)
.requestor
- any object to control who called this method. Note that
it is considered to be an external change if requestor
is null
.
See VirtualFileEvent.getRequestor()
java.io.IOException
- if file failed to be deletedpublic void move(java.lang.Object requestor, VirtualFile newParent) throws java.io.IOException
Application.runWriteAction(java.lang.Runnable)
.requestor
- any object to control who called this method. Note that
it is considered to be an external change if requestor
is null
.
See VirtualFileEvent.getRequestor()
newParent
- the directory to move this file tojava.io.IOException
- if file failed to be movedpublic VirtualFile copy(java.lang.Object requestor, VirtualFile newParent, java.lang.String copyName) throws java.io.IOException
java.io.IOException
public java.nio.charset.Charset getCharset()
protected void storeCharset(java.nio.charset.Charset charset)
public void setCharset(java.nio.charset.Charset charset)
public void setCharset(java.nio.charset.Charset charset, java.lang.Runnable whenChanged)
public void setCharset(java.nio.charset.Charset charset, java.lang.Runnable whenChanged, boolean fireEventsWhenChanged)
public boolean isCharsetSet()
public final void setBinaryContent(byte [] content) throws java.io.IOException
java.io.IOException
public void setBinaryContent(byte [] content, long newModificationStamp, long newTimeStamp) throws java.io.IOException
java.io.IOException
public void setBinaryContent(byte [] content, long newModificationStamp, long newTimeStamp, java.lang.Object requestor) throws java.io.IOException
java.io.IOException
public final java.io.OutputStream getOutputStream(java.lang.Object requestor) throws java.io.IOException
OutputStream
for this file.
Writes BOM first, if there is any. See Unicode Byte Order Mark FAQ for an explanation.requestor
- any object to control who called this method. Note that
it is considered to be an external change if requestor
is null
.
See VirtualFileEvent.getRequestor()
and SafeWriteRequestor
.OutputStream
java.io.IOException
- if an I/O error occurspublic abstract java.io.OutputStream getOutputStream(java.lang.Object requestor, long newModificationStamp, long newTimeStamp) throws java.io.IOException
Gets the OutputStream
for this file and sets modification stamp and time stamp to the specified values
after closing the stream.
Normally, you should not use this method.
Writes BOM first, if there is any. See Unicode Byte Order Mark FAQ for an explanation.
requestor
- any object to control who called this method. Note that
it is considered to be an external change if requestor
is null
.
See VirtualFileEvent.getRequestor()
and SafeWriteRequestor
.newModificationStamp
- new modification stamp or -1 if no special value should be setnewTimeStamp
- new time stamp or -1 if no special value should be setOutputStream
java.io.IOException
- if an I/O error occursgetModificationStamp()
public abstract byte [] contentsToByteArray() throws java.io.IOException
java.io.IOException
- if an I/O error occurscontentsToByteArray(boolean)
,
getInputStream()
public byte [] contentsToByteArray(boolean cacheContent) throws java.io.IOException
cacheContent
- set true tojava.io.IOException
- if an I/O error occurscontentsToByteArray()
public long getModificationStamp()
getTimeStamp()
public abstract long getTimeStamp()
File.lastModified()
public abstract long getLength()
public void refresh(boolean asynchronous, boolean recursive)
Refreshes the cached file information from the physical file system. If this file is not a directory
the timestamp value is refreshed and contentsChanged
event is fired if it is changed.
If this file is a directory the set of its children is refreshed. If recursive value is true
all
children are refreshed recursively.
When invoking synchronous refresh from a thread other than the event dispatch thread, the current thread must NOT be in a read action, otherwise a deadlock may occur.
asynchronous
- if true
, the method will return immediately and the refresh will be processed
in the background. If false
, the method will return only after the refresh
is done and the VFS change events caused by the refresh have been fired and processed
in the event dispatch thread. Instead of synchronous refreshes, it's recommended to use
asynchronous refreshes with a postRunnable
whenever possible.recursive
- whether to refresh all the files in this directory recursivelypublic abstract void refresh(boolean asynchronous, boolean recursive, java.lang.Runnable postRunnable)
refresh(boolean, boolean)
but also runs postRunnable
after the operation is completed. The runnable is executed on event dispatch thread inside write action.public java.lang.String getPresentableName()
public long getModificationCount()
getModificationCount
in interface ModificationTracker
protected boolean nameEquals(java.lang.String name)
public abstract java.io.InputStream getInputStream() throws java.io.IOException
InputStream
for this file.
Skips BOM if there is any. See Unicode Byte Order Mark FAQ for an explanation.InputStream
java.io.IOException
- if an I/O error occurscontentsToByteArray()
public byte [] getBOM()
public void setBOM(byte [] BOM)
public java.lang.String toString()
toString
in class java.lang.Object
public boolean exists()
public boolean isInLocalFileSystem()
@Deprecated public static boolean isValidName(java.lang.String name)
VirtualFileSystem.isValidName(String)
public java.lang.String getDetectedLineSeparator()
LineSeparator
public void setDetectedLineSeparator(java.lang.String separator)
public void setPreloadedContentHint(byte[] preloadedContentHint)
public boolean isRecursiveOrCircularSymLink()
true
if this file is a symlink that is either recursive (i.e. points to this file' parent) or
circular (i.e. its path has a form of "/.../linkX/.../linkX").