org.entityfs.util
Class ManualNamedReadableFile

java.lang.Object
  extended by org.entityfs.lock.AbstractLockable
      extended by org.entityfs.util.ManualNamedReadableFile
All Implemented Interfaces:
Lockable, ReadLockable, ReadWriteLockable, WriteLockable, Named, NamedReadableFile, ReadableFile

public class ManualNamedReadableFile
extends AbstractLockable
implements NamedReadableFile

This is a read only non-entity implementation of NamedReadableFile. It is created on a byte array or a CharSequence (a String, for instance.

The CharSequenceReadableFile is more suited for creating manual character files. (It can be converted into a NamedReadableFile using the NamedReadableFileAdapter.

Since:
1.0
Author:
Karl Gustafsson
See Also:
CharSequenceReadableFile

Constructor Summary
ManualNamedReadableFile(String name, byte[] data)
          Create a manual named readable file.
ManualNamedReadableFile(String name, CharSequence data)
          Create a manual named readable file.
 
Method Summary
 long getDataSize()
          Get the size of the data in the file.
 String getName()
          Get the name.
 long getSize()
          Get the size of the file (in bytes).
 ReadableByteChannel openChannelForRead()
          Open a ReadableByteChannel for reading from the file.
 InputStream openForRead()
          Opens the file for reading.
 
Methods inherited from class org.entityfs.lock.AbstractLockable
assertIsReadLocked, assertIsWriteLocked, getLockAcquiringStrategy, getLockAdapter, getLocked, getLogAdapter, getReadLock, getWriteLock, isReadLockedByCurrentThread, isWriteLockedByCurrentThread, lock, lockForReading, lockForWriting, logLockStack
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.entityfs.lock.ReadLockable
getReadLock, isReadLockedByCurrentThread, lockForReading
 

Constructor Detail

ManualNamedReadableFile

public ManualNamedReadableFile(String name,
                               byte[] data)
Create a manual named readable file.

Parameters:
name - The file name.
data - The file data.

ManualNamedReadableFile

public ManualNamedReadableFile(String name,
                               CharSequence data)
Create a manual named readable file.

Parameters:
name - The file name.
data - The file data (a String, for instance). This is converted internally to a byte array using the platform's default character encoding.
Method Detail

getName

public String getName()
Description copied from interface: Named
Get the name. The name of the root directory is /.

Specified by:
getName in interface Named
Returns:
The name of the named object.

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.

Specified by:
openForRead in interface ReadableFile
Returns:
An InputStream on the file. The stream must be closed by the caller.

openChannelForRead

public ReadableByteChannel openChannelForRead()
Description copied from interface: ReadableFile
Open a ReadableByteChannel for reading from the file. The semantics of this method is the same as for ReadableFile.openForRead().

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

Specified by:
openChannelForRead in interface ReadableFile
Returns:
An open ReadableByteChannel. The channel must be closed by the caller. The channel may be a ScatteringByteChannel, but that is not required.
See Also:
ReadableFile.openForRead()

getDataSize

public long getDataSize()
                 throws ReadLockRequiredException,
                        AccessDeniedException
Description copied from interface: ReadableFile
Get the size of the data in the file. For compressed files, this is larger than the value returned by ReadableFile.getSize().

Warning: Calculating the data size of a file has the potential of being really slow, depending on the implementation used. The entire file may have to be read through in order to calculate the size of its data.

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

Specified by:
getDataSize in interface ReadableFile
Returns:
The size of data in the file.
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.getSize()

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.

Specified by:
getSize in interface ReadableFile
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()