org.apache.commons.vfs.provider
Class DefaultFileContent

java.lang.Object
  extended by org.apache.commons.vfs.provider.DefaultFileContent
All Implemented Interfaces:
FileContent

public final class DefaultFileContent
extends java.lang.Object
implements FileContent

The content of a file.

Version:
$Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
Author:
Adam Murdoch

Nested Class Summary
private  class DefaultFileContent.FileContentInputStream
          An input stream for reading content.
(package private)  class DefaultFileContent.FileContentOutputStream
          An output stream for writing content.
private  class DefaultFileContent.FileRandomAccessContent
          An input/output stream for reading/writing content on random positions
 
Field Summary
private  java.util.Map attrs
           
private  AbstractFileObject file
           
private  FileContentInfo fileContentInfo
           
private  FileContentInfoFactory fileContentInfoFactory
           
private  int openStreams
          open streams counter for this file
private  java.util.Map roAttrs
           
(package private) static int STATE_CLOSED
           
(package private) static int STATE_OPENED
           
private  java.lang.ThreadLocal threadData
           
 
Constructor Summary
DefaultFileContent(AbstractFileObject file, FileContentInfoFactory fileContentInfoFactory)
           
 
Method Summary
 void close()
          Closes all resources used by the content, including all streams, readers and writers.
private  void endInput(DefaultFileContent.FileContentInputStream instr)
          Handles the end of input stream.
private  void endOutput()
          Handles the end of output stream.
private  void endRandomAccess(RandomAccessContent rac)
          Handles the end of random access.
 java.lang.Object getAttribute(java.lang.String attrName)
          Gets the value of an attribute.
 java.lang.String[] getAttributeNames()
          Lists the attributes of this file.
 java.util.Map getAttributes()
          Returns a read-only map of this file's attributes.
 java.security.cert.Certificate[] getCertificates()
          Returns the certificates used to sign this file.
 FileContentInfo getContentInfo()
          get the content info.
 FileObject getFile()
          Returns the file that this is the content of.
 java.io.InputStream getInputStream()
          Returns an input stream for reading the content.
 long getLastModifiedTime()
          Returns the last-modified timestamp.
 java.io.OutputStream getOutputStream()
          Returns an output stream for writing the content.
 java.io.OutputStream getOutputStream(boolean bAppend)
          Returns an output stream for writing the content in append mode.
 RandomAccessContent getRandomAccessContent(RandomAccessMode mode)
          Returns an input/output stream to use to read and write the content of the file in an random manner.
 long getSize()
          Returns the size of the content (in bytes).
private  FileContentThreadData getThreadData()
           
 boolean isOpen()
          check if a input and/or output stream is open.
This checks only the scope of the current thread.
 boolean isOpenGlobal()
          check if a input and/or output stream is open.
This checks all threads.
 void setAttribute(java.lang.String attrName, java.lang.Object value)
          Sets the value of an attribute.
 void setLastModifiedTime(long modTime)
          Sets the last-modified timestamp.
(package private)  void streamClosed()
           
(package private)  void streamOpened()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATE_CLOSED

static final int STATE_CLOSED
See Also:
Constant Field Values

STATE_OPENED

static final int STATE_OPENED
See Also:
Constant Field Values

file

private final AbstractFileObject file

attrs

private java.util.Map attrs

roAttrs

private java.util.Map roAttrs

fileContentInfo

private FileContentInfo fileContentInfo

fileContentInfoFactory

private final FileContentInfoFactory fileContentInfoFactory

threadData

private final java.lang.ThreadLocal threadData

openStreams

private int openStreams
open streams counter for this file

Constructor Detail

DefaultFileContent

public DefaultFileContent(AbstractFileObject file,
                          FileContentInfoFactory fileContentInfoFactory)
Method Detail

getThreadData

private FileContentThreadData getThreadData()

streamOpened

void streamOpened()

streamClosed

void streamClosed()

getFile

public FileObject getFile()
Returns the file that this is the content of.

Specified by:
getFile in interface FileContent

getSize

public long getSize()
             throws FileSystemException
Returns the size of the content (in bytes).

Specified by:
getSize in interface FileContent
Returns:
The size of the file, in bytes.
Throws:
FileSystemException - If the file does not exist, or is being written to, or on error determining the size.

getLastModifiedTime

public long getLastModifiedTime()
                         throws FileSystemException
Returns the last-modified timestamp.

Specified by:
getLastModifiedTime in interface FileContent
Returns:
The last-modified timestamp.
Throws:
FileSystemException - If the file does not exist, or is being written to, or on error determining the last-modified timestamp.

setLastModifiedTime

public void setLastModifiedTime(long modTime)
                         throws FileSystemException
Sets the last-modified timestamp.

Specified by:
setLastModifiedTime in interface FileContent
Parameters:
modTime - The time to set the last-modified timestamp to.
Throws:
FileSystemException - If the file is read-only, or is being written to, or on error setting the last-modified timestamp.

getAttributes

public java.util.Map getAttributes()
                            throws FileSystemException
Returns a read-only map of this file's attributes.

Specified by:
getAttributes in interface FileContent
Throws:
FileSystemException - If the file does not exist, or does not support attributes.

getAttributeNames

public java.lang.String[] getAttributeNames()
                                     throws FileSystemException
Lists the attributes of this file.

Specified by:
getAttributeNames in interface FileContent
Returns:
The names of the attributes. Never returns null;
Throws:
FileSystemException - If the file does not exist, or does not support attributes.

getAttribute

public java.lang.Object getAttribute(java.lang.String attrName)
                              throws FileSystemException
Gets the value of an attribute.

Specified by:
getAttribute in interface FileContent
Parameters:
attrName - The name of the attribute. Attribute names are case insensitive.
Returns:
The value of the attribute, or null if the attribute value is unknown.
Throws:
FileSystemException - If the file does not exist, or does not support attributes.

setAttribute

public void setAttribute(java.lang.String attrName,
                         java.lang.Object value)
                  throws FileSystemException
Sets the value of an attribute.

Specified by:
setAttribute in interface FileContent
Parameters:
attrName - The name of the attribute.
value - The value of the attribute.
Throws:
FileSystemException - If the file does not exist, or is read-only, or does not support attributes, or on error setting the attribute.

getCertificates

public java.security.cert.Certificate[] getCertificates()
                                                 throws FileSystemException
Returns the certificates used to sign this file.

Specified by:
getCertificates in interface FileContent
Returns:
The certificates, or an empty array if there are no certificates or the file does not support signing.
Throws:
FileSystemException - If the file does not exist, or is being written.

getInputStream

public java.io.InputStream getInputStream()
                                   throws FileSystemException
Returns an input stream for reading the content.

Specified by:
getInputStream in interface FileContent
Returns:
An input stream to read the file's content from. The input stream is buffered, so there is no need to wrap it in a BufferedInputStream.
Throws:
FileSystemException - If the file does not exist, or is being read, or is being written, or on error opening the stream.

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 an random manner.

Specified by:
getRandomAccessContent in interface FileContent
Throws:
FileSystemException - If the file is read-only, or is being read, or is being written, or on error opening the stream.

getOutputStream

public java.io.OutputStream getOutputStream()
                                     throws FileSystemException
Returns an output stream for writing the content.

Specified by:
getOutputStream in interface FileContent
Returns:
An output stream to write the file's content to. The stream is buffered, so there is no need to wrap it in a BufferedOutputStream.
Throws:
FileSystemException - If the file is read-only, or is being read, or is being written, or on error opening the stream.

getOutputStream

public java.io.OutputStream getOutputStream(boolean bAppend)
                                     throws FileSystemException
Returns an output stream for writing the content in append mode.

Specified by:
getOutputStream in interface FileContent
Parameters:
bAppend - true if you would like to append to the file
Returns:
An output stream to write the file's content to. The stream is buffered, so there is no need to wrap it in a BufferedOutputStream.
Throws:
FileSystemException - If the file is read-only, or is being read, or is being written, or on error opening the stream.

close

public void close()
           throws FileSystemException
Closes all resources used by the content, including all streams, readers and writers.

Specified by:
close in interface FileContent
Throws:
FileSystemException

endInput

private void endInput(DefaultFileContent.FileContentInputStream instr)
Handles the end of input stream.


endRandomAccess

private void endRandomAccess(RandomAccessContent rac)
Handles the end of random access.


endOutput

private void endOutput()
                throws java.lang.Exception
Handles the end of output stream.

Throws:
java.lang.Exception

isOpen

public boolean isOpen()
check if a input and/or output stream is open.
This checks only the scope of the current thread.

Specified by:
isOpen in interface FileContent
Returns:
true if this is the case

isOpenGlobal

public boolean isOpenGlobal()
check if a input and/or output stream is open.
This checks all threads.

Returns:
true if this is the case

getContentInfo

public FileContentInfo getContentInfo()
                               throws FileSystemException
get the content info. e.g. content-type, content-encoding

Specified by:
getContentInfo in interface FileContent
Throws:
FileSystemException