org.entityfs.util
Class ClasspathNamedReadableFile

java.lang.Object
  extended by org.entityfs.util.ClasspathNamedReadableFile
All Implemented Interfaces:
Lockable, ReadLockable, Named, NamedReadableFile, ReadableFile

public class ClasspathNamedReadableFile
extends Object
implements NamedReadableFile

This object can be used to treat a resource on the current classpath as a NamedReadableFile.

Since:
1.1
Author:
Karl Gustafsson

Constructor Summary
ClasspathNamedReadableFile(AbsoluteLocation loc, ClassLoader cl)
          Create a named readable file using the resource at the specified location loaded by the supplied class loader.
ClasspathNamedReadableFile(EntityLocation<?> loc, Class<?> c)
          Create a named readable file using the resource with the specified location.
 
Method Summary
 long getDataSize()
          Get the size of the data in the file.
 String getName()
          Get the name.
 EntityLock getReadLock()
          This method returns a dummy lock.
 long getSize()
          Get the size of the file (in bytes).
 boolean isReadLockedByCurrentThread()
          This method always returns true.
 EntityLock lockForReading()
          This method returns a dummy lock.
 ReadableByteChannel openChannelForRead()
          Open a ReadableByteChannel for reading from the file.
 InputStream openForRead()
          Opens the file for reading.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClasspathNamedReadableFile

public ClasspathNamedReadableFile(AbsoluteLocation loc,
                                  ClassLoader cl)
                           throws EntityNotFoundException
Create a named readable file using the resource at the specified location loaded by the supplied class loader.

To create the location, just substitute the '.':s in the fully qualified name for '/':es in the AbsoluteLocation.

Parameters:
cl - The class loader to load the resource with.
loc - The fully qualified location of the resource.
Throws:
EntityNotFoundException - If there is no resource at the specified location.

ClasspathNamedReadableFile

public ClasspathNamedReadableFile(EntityLocation<?> loc,
                                  Class<?> c)
                           throws EntityNotFoundException
Create a named readable file using the resource with the specified location. The location is either absolute (i.e. fully qualified), or relative to the supplied class' package's location.

To create the location, just substitute the '.':s in the name for '/':es in the EntityLocation.

The supplied class' class loader is used to load the resource.

Parameters:
c - The class whose class loader will be used to load the resource.
loc - The location of the resource. This is either an AbsoluteLocation, meaning a fully qualified resource name, or a RelativeLocation taken relative to the supplied class' package's location.
Throws:
EntityNotFoundException - If there is no resource at the specified location.
Method Detail

openChannelForRead

public ReadableByteChannel openChannelForRead()
                                       throws WrappedIOException
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.
Throws:
WrappedIOException
See Also:
ReadableFile.openForRead()

openForRead

public InputStream openForRead()
                        throws WrappedIOException
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.
Throws:
WrappedIOException

getReadLock

public EntityLock getReadLock()
This method returns a dummy lock.

Specified by:
getReadLock in interface ReadLockable
Returns:
A dummy lock.

isReadLockedByCurrentThread

public boolean isReadLockedByCurrentThread()
This method always returns true.

Specified by:
isReadLockedByCurrentThread in interface ReadLockable
Returns:
true, always.

lockForReading

public EntityLock lockForReading()
This method returns a dummy lock.

Specified by:
lockForReading in interface ReadLockable
Returns:
A dummy lock,

getName

public String getName()
               throws IllegalStateException,
                      ReadLockRequiredException
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.
Throws:
IllegalStateException - If this is called on a disconnected view.
ReadLockRequiredException - If the client does not have a read lock for the viewed entity or its parent.

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()