org.entityfs.util
Class CharSequenceReadableFile

java.lang.Object
  extended by org.entityfs.util.base.AbstractReadableFile
      extended by org.entityfs.util.CharSequenceReadableFile
All Implemented Interfaces:
Lockable, ReadLockable, ReadableFile

public class CharSequenceReadableFile
extends AbstractReadableFile

This is a read only ReadableFile that reads data from a CharSequence.

Locking instances of this class returns a NoObjectDummyLock.

Since:
1.0
Author:
Karl Gustafsson
See Also:
ManualNamedReadableFile, ByteArrayReadableFile

Constructor Summary
CharSequenceReadableFile(CharSequence contents)
          Create a readable file on the supplied CharSequence.
CharSequenceReadableFile(CharSequence contents, Charset cs)
          Create a readable file on the supplied CharSequence.
 
Method Summary
 long getSize()
          Get the size of the file (in bytes).
 ReadableByteChannel openChannelForRead()
          This opens a byte channel on the InputStream returned from openForRead.
 InputStream openForRead()
          Opens the file for reading.
 String toString()
           
 
Methods inherited from class org.entityfs.util.base.AbstractReadableFile
getDataSize, getReadLock, isReadLockedByCurrentThread, lockForReading
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CharSequenceReadableFile

public CharSequenceReadableFile(CharSequence contents)
Create a readable file on the supplied CharSequence. Characters will be decoded using the default Charset.

Parameters:
contents - The contents of the file.

CharSequenceReadableFile

public CharSequenceReadableFile(CharSequence contents,
                                Charset cs)
Create a readable file on the supplied CharSequence. Characters will be decoded using the provided Charset.

Parameters:
contents - The contents of the file.
cs - The charset to decode the character sequence with.
Method Detail

openChannelForRead

public ReadableByteChannel openChannelForRead()
Description copied from class: AbstractReadableFile
This opens a byte channel on the InputStream returned from openForRead.

Specified by:
openChannelForRead in interface ReadableFile
Overrides:
openChannelForRead in class AbstractReadableFile
Returns:
An open byte channel.
See Also:
ReadableFile.openForRead()

openForRead

public InputStream openForRead()
Description copied from interface: ReadableFile
Opens the file for reading. The caller is responsible for closing the returned stream.

Although a read lock on the file is required for the execution thread opening the stream, the stream itself is not protected from access by other threads. It can be protected by wrapping it in a LockAwareInputStream.

Returns:
An InputStream on the file. The stream must be closed by the caller.

getSize

public long getSize()
             throws ReadLockRequiredException,
                    AccessDeniedException
Description copied from interface: ReadableFile
Get the size of the file (in bytes). This is the number of bytes that the file occupies where it is stored.

Note: Most, but not all, implementations know their sizes. If an implementation does not know its size beforehand, this method will be slow since it has to calculate the file size when it is called.

Implementation note: If the size of the backing file is not known, the StreamUtil.getSizeOfDataInStream(InputStream, int) method can be used to calculate it.

Returns:
The size of the file, in bytes.
Throws:
ReadLockRequiredException - If the client does not have a read lock for the file.
AccessDeniedException - If the client does not have read access to the file.
See Also:
ReadableFile.getDataSize()

toString

public String toString()
Overrides:
toString in class Object