com.intellij.openapi.vfs
Class VirtualFile

java.lang.Object
  extended by com.intellij.openapi.vfs.VirtualFile
All Implemented Interfaces:
ModificationTracker, UserDataHolder
Direct Known Subclasses:
AbstractVcsVirtualFile, MockVirtualFile

public abstract class VirtualFile
extends java.lang.Object
implements UserDataHolder, ModificationTracker

Represents a file in VirtualFileSystem.

See Also:
VirtualFileSystem, VirtualFileManager

Field Summary
static VirtualFile[] EMPTY_ARRAY
           
protected  byte[] myBOM
           
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_WRITABLE
          Used as a property name in the VirtualFilePropertyEvent fired when the write permission of a VirtualFile changes.
 
Fields inherited from interface com.intellij.openapi.util.ModificationTracker
EVER_CHANGED
 
Constructor Summary
protected VirtualFile()
           
 
Method Summary
abstract  byte[] contentsToByteArray()
          Returns file content as an array of bytes.
abstract  char[] contentsToCharArray()
          Returns file content as an array of characters.
abstract  VirtualFile createChildData(java.lang.Object requestor, java.lang.String name)
          Creates a new file in this directory.
abstract  VirtualFile createChildDirectory(java.lang.Object requestor, java.lang.String name)
          Creates a subdirectory in this directory.
abstract  void delete(java.lang.Object requestor)
          Deletes this file.
 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.
abstract  long getActualTimeStamp()
          Gets the file timestamp.
 java.nio.charset.Charset getCharset()
           
abstract  VirtualFile[] getChildren()
          Gets the child files.
 java.lang.String getExtension()
          Gets the extension of this file.
abstract  VirtualFileSystem getFileSystem()
          Gets the VirtualFileSystem this file belongs to.
 FileType getFileType()
           
 javax.swing.Icon getIcon()
           
abstract  java.io.InputStream getInputStream()
          Gets the InputStream for this file.
abstract  long getLength()
          File length in bytes.
 long getModificationCount()
           
abstract  long getModificationStamp()
          Gets modification stamp value.
abstract  java.lang.String getName()
          Gets the name of this file.
 java.lang.String getNameWithoutExtension()
          Gets the file name without the extension.
 java.io.OutputStream getOutputStream(java.lang.Object requestor)
          Gets 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.
 java.io.Reader getReader()
          Gets the Reader for this file.
static java.io.Reader getReader(byte[] bytes)
           
abstract  long getTimeStamp()
          Gets the timestamp for this file.
 java.lang.String getUrl()
          Gets the URL of this file.
<T> T
getUserData(Key<T> key)
           
 java.io.Writer getWriter(java.lang.Object requestor)
          Gets the Writer for this file.
 java.io.Writer getWriter(java.lang.Object requestor, long newModificationStamp, long newTimeStamp)
          Gets the Writer for this file and sets modification stamp and time stamp to the specified values after closing the Writer.
abstract  boolean isDirectory()
          Checks whether this file is a directory.
abstract  boolean isValid()
          Checks whether this VirtualFile is valid.
abstract  boolean isWritable()
          Checks whether this file has write permission.
abstract  void move(java.lang.Object requestor, VirtualFile newParent)
          Moves this file to another directory.
 boolean nameEquals(java.lang.String name)
           
 byte[] physicalContentsToByteArray()
           
<T> void
putUserData(Key<T> key, T value)
           
 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 asynchronous, boolean recursive) but also runs postRunnable after the operation is completed.
abstract  void rename(java.lang.Object requestor, java.lang.String newName)
          Renames this file to the newName.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ARRAY

public static final VirtualFile[] EMPTY_ARRAY

myBOM

protected byte[] myBOM

PROP_NAME

public static final java.lang.String PROP_NAME
Used as a property name in the VirtualFilePropertyEvent fired when the name of a VirtualFile changes.

See Also:
VirtualFileListener.propertyChanged(com.intellij.openapi.vfs.VirtualFilePropertyEvent), VirtualFilePropertyEvent.getPropertyName(), Constant Field Values

PROP_WRITABLE

public static final java.lang.String PROP_WRITABLE
Used as a property name in the VirtualFilePropertyEvent fired when the write permission of a VirtualFile changes.

See Also:
VirtualFileListener.propertyChanged(com.intellij.openapi.vfs.VirtualFilePropertyEvent), VirtualFilePropertyEvent.getPropertyName(), Constant Field Values
Constructor Detail

VirtualFile

protected VirtualFile()
Method Detail

getFileSystem

@NotNull
public abstract VirtualFileSystem getFileSystem()
Gets the VirtualFileSystem this file belongs to.

Returns:
the VirtualFileSystem

getPath

public abstract java.lang.String getPath()
Gets the path of this file. Path is a string which uniquely identifies file within given VirtualFileSystem. Format of the path depends on the concrete file system. For LocalFileSystem it is an absoulute file path with file separator characters (File.separatorChar) replaced to the forward slash ('/').

Returns:
the path

getUrl

@NotNull
public java.lang.String getUrl()
Gets the URL of this file. The URL is a string which uniquely identifies 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.

Returns:
the URL consisting of protocol and path
See Also:
VirtualFileManager.findFileByUrl(java.lang.String), getPath(), VirtualFileSystem.getProtocol()

getPresentableUrl

public final java.lang.String getPresentableUrl()
Fetches "presentable URL" of this file. "Presentable URL" is a string to be used for displaying this file in the UI.

Returns:
the presentable URL.
See Also:
VirtualFileSystem.extractPresentableUrl(java.lang.String)

getName

@NotNull
public abstract java.lang.String getName()
Gets the name of this file.

Returns:
file name

getExtension

@Nullable
public java.lang.String getExtension()
Gets the extension of this file. If file name contains '.' extension is the substring from the last '.' to the end of the name, otherwise extension is null.

Returns:
the extension or null if file name doesn't contain '.'

getNameWithoutExtension

@NotNull
public java.lang.String getNameWithoutExtension()
Gets the file name without the extension. If file name contains '.' the substring till the last '.' is returned. Otherwise the same value as getName() method returns is returned.

Returns:
the name without extension if there is no '.' in it

rename

public abstract void rename(java.lang.Object requestor,
                            java.lang.String newName)
                     throws java.io.IOException
Renames this file to the newName.

This method should be only called within write-action. See Application.runWriteAction(java.lang.Runnable).

Parameters:
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 name
Throws:
java.io.IOException - if file failed to be renamed

isWritable

public abstract boolean isWritable()
Checks whether this file has write permission. Note that this value may be cached and may differ from the write permission of the physical file.

Returns:
true if this file is writable, false otherwise

isDirectory

public abstract boolean isDirectory()
Checks whether this file is a directory.

Returns:
true if this file is a directory, fasle otherwise

isValid

public abstract boolean isValid()
Checks whether this 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() and methods from UserDataHolder can be called for it. Using any other methods for an invalid VirtualFile instance produce inpredictable results.

Returns:
true if this is a valid file, fasle otherwise

getParent

@Nullable
public abstract VirtualFile getParent()
Gets the parent VirtualFile.

Returns:
the parent file or null if this file is a root directory

getChildren

public abstract VirtualFile[] getChildren()
Gets the child files.

Returns:
array of the child files or null if this file is not a directory

findChild

public VirtualFile findChild(java.lang.String name)
Finds child of this file with the given name.

Parameters:
name - the file name to search by
Returns:
the file if found any, null otherwise

getIcon

public javax.swing.Icon getIcon()

getFileType

public FileType getFileType()

findFileByRelativePath

@Nullable
public VirtualFile findFileByRelativePath(java.lang.String relPath)
Finds file by path relative to this file.

Parameters:
relPath - the relative path to search by
Returns:
the file if found any, null otherwise

createChildDirectory

public abstract VirtualFile createChildDirectory(java.lang.Object requestor,
                                                 java.lang.String name)
                                          throws java.io.IOException
Creates a subdirectory in this directory. This method should be only called within write-action. See Application.runWriteAction(java.lang.Runnable).

Parameters:
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 name
Returns:
VirtualFile representing the created directory
Throws:
java.io.IOException - if directory failed to be created

createChildData

public abstract VirtualFile createChildData(java.lang.Object requestor,
                                            java.lang.String name)
                                     throws java.io.IOException
Creates a new file in this directory. This method should be only called within write-action. See Application.runWriteAction(java.lang.Runnable).

Parameters:
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()
Returns:
VirtualFile representing the created file
Throws:
java.io.IOException - if file failed to be created

delete

public abstract void delete(java.lang.Object requestor)
                     throws java.io.IOException
Deletes this file. This method should be only called within write-action. See Application.runWriteAction(java.lang.Runnable).

Parameters:
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()
Throws:
java.io.IOException - if file failed to be deleted

move

public abstract void move(java.lang.Object requestor,
                          VirtualFile newParent)
                   throws java.io.IOException
Moves this file to another directory. This method should be only called within write-action. See Application.runWriteAction(java.lang.Runnable).

Parameters:
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 to
Throws:
java.io.IOException - if file failed to be moved

getInputStream

public abstract java.io.InputStream getInputStream()
                                            throws java.io.IOException
Gets the InputStream for this file.

Returns:
InputStream
Throws:
java.io.IOException - if an I/O error occurs
See Also:
contentsToByteArray()

getOutputStream

public final java.io.OutputStream getOutputStream(java.lang.Object requestor)
                                           throws java.io.IOException
Gets the OutputStream for this file.

Parameters:
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()
Returns:
OutputStream
Throws:
java.io.IOException - if an I/O error occurs

getOutputStream

public 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.

Parameters:
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()
newModificationStamp - new modification stamp or -1 if no special value should be set
newTimeStamp - new time stamp or -1 if no special value should be set
Returns:
OutputStream
Throws:
java.io.IOException - if an I/O error occurs
See Also:
getOutputStream(Object), getModificationStamp()

getCharset

public java.nio.charset.Charset getCharset()
Returns:
Retreive the charset file have been loaded with (if loaded) and would be saved with (if would).

getReader

public java.io.Reader getReader()
                         throws java.io.IOException
Gets the Reader for this file.

Returns:
Reader
Throws:
java.io.IOException - if an I/O error occurs
See Also:
contentsToCharArray()

getReader

public static java.io.Reader getReader(byte[] bytes)
                                throws java.io.IOException
Throws:
java.io.IOException

getWriter

public final java.io.Writer getWriter(java.lang.Object requestor)
                               throws java.io.IOException
Gets the Writer for this file.

Parameters:
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()
Returns:
Writer
Throws:
java.io.IOException - if an I/O error occurs

getWriter

public java.io.Writer getWriter(java.lang.Object requestor,
                                long newModificationStamp,
                                long newTimeStamp)
                         throws java.io.IOException
Gets the Writer for this file and sets modification stamp and time stamp to the specified values after closing the Writer.

Normally you should not use this method.

Parameters:
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()
newModificationStamp - new modification stamp or -1 if no special value should be set
newTimeStamp - new time stamp or -1 if no special value should be set
Returns:
Writer
Throws:
java.io.IOException - if an I/O error occurs
See Also:
getWriter(Object), getModificationStamp()

contentsToByteArray

public abstract byte[] contentsToByteArray()
                                    throws java.io.IOException
Returns file content as an array of bytes.

Returns:
file content
Throws:
java.io.IOException - if an I/O error occurs
See Also:
getInputStream()

contentsToCharArray

public abstract char[] contentsToCharArray()
                                    throws java.io.IOException
Returns file content as an array of characters.

Returns:
file content
Throws:
java.io.IOException - if an I/O error occurs
See Also:
getReader()

getModificationStamp

public abstract long getModificationStamp()
Gets modification stamp value. Modification stamp is a value changed by any modification of the content of the file. Note that it is not related to the file modification time.

Returns:
modification stamp
See Also:
getTimeStamp()

getTimeStamp

public abstract long getTimeStamp()
Gets the timestamp for this file. Note that this value may be cached and may differ from the timestamp of the physical file.

Returns:
timestamp
See Also:
getActualTimeStamp(), File.lastModified()

getActualTimeStamp

public abstract long getActualTimeStamp()
Gets the file timestamp. Unlike value returned by getTimeStamp() returns the actual timestamp for this file.

Returns:
the actual timestamp
See Also:
getTimeStamp()

getLength

public abstract long getLength()
File length in bytes.

Returns:
the length of this file.

refresh

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.

This method should be only called within write-action. See Application.runWriteAction(java.lang.Runnable).

Parameters:
asynchronous - if true then the operation will be performed in a separate thread, otherwise will be performed immediately
recursive - whether to refresh all the files in this directory recursively

refresh

public abstract void refresh(boolean asynchronous,
                             boolean recursive,
                             java.lang.Runnable postRunnable)
The same as refresh(boolean asynchronous, boolean recursive) but also runs postRunnable after the operation is completed.


getUserData

public <T> T getUserData(Key<T> key)
Specified by:
getUserData in interface UserDataHolder

putUserData

public <T> void putUserData(Key<T> key,
                            T value)
Specified by:
putUserData in interface UserDataHolder

physicalContentsToByteArray

public byte[] physicalContentsToByteArray()
                                   throws java.io.IOException
Throws:
java.io.IOException

getPresentableName

public java.lang.String getPresentableName()

getModificationCount

public long getModificationCount()
Specified by:
getModificationCount in interface ModificationTracker

nameEquals

public boolean nameEquals(java.lang.String name)
Parameters:
name -
Returns:
whether file name equals to this name result depends on the filesystem specifics