org.entityfs.util
Class ByteArrayReadableFile

java.lang.Object
  extended by org.entityfs.util.base.AbstractReadableFile
      extended by org.entityfs.util.ByteArrayReadableFile
All Implemented Interfaces:
Lockable, ReadLockable, ReadWriteLockable, WriteLockable, RandomlyAccessibleFile, ReadableFile

public class ByteArrayReadableFile
extends AbstractReadableFile
implements ReadableFile, RandomlyAccessibleFile

This is a read only ReadableFile implementation that is backed by a byte array.

Locking instances of this class returns a NoObjectDummyLock.

Since:
1.0
Author:
Karl Gustafsson
In_jar:
entityfs-util

Constructor Summary
ByteArrayReadableFile(byte[] buf)
          Create a read only, byte array-backed ReadableFile.
 
Method Summary
 long getSize()
          Get the size of the file (in bytes).
 EntityLock getWriteLock()
          Returns a dummy lock.
 boolean isWriteLockedByCurrentThread()
          Always returns true.
 EntityLock lockForWriting()
          Returns a dummy lock.
 ReadableByteChannel openChannelForRead()
          This opens a byte channel on the InputStream returned from openForRead.
 RandomAccess openForRandomAccess(RandomAccessMode ram)
          Open the file for random access in the specified mode.
 InputStream openForRead()
          Opens the file for reading.
 
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, toString, wait, wait, wait
 
Methods inherited from interface org.entityfs.ReadableFile
getDataSize
 
Methods inherited from interface org.entityfs.lock.ReadLockable
getReadLock, isReadLockedByCurrentThread, lockForReading
 

Constructor Detail

ByteArrayReadableFile

public ByteArrayReadableFile(byte[] buf)
Create a read only, byte array-backed ReadableFile.

Parameters:
buf - The byte array.
Method Detail

getWriteLock

public EntityLock getWriteLock()
Returns a dummy lock.

Specified by:
getWriteLock in interface WriteLockable
Returns:
The lockable's write lock. This might be unlocked or locked by someone else.

lockForWriting

public EntityLock lockForWriting()
Returns a dummy lock.

Specified by:
lockForWriting in interface WriteLockable
Returns:
A locked write lock.

isWriteLockedByCurrentThread

public boolean isWriteLockedByCurrentThread()
Always returns true.

Specified by:
isWriteLockedByCurrentThread in interface WriteLockable
Returns:
true if the lockable object is write locked by the current thread.

openForRandomAccess

public RandomAccess openForRandomAccess(RandomAccessMode ram)
Description copied from interface: RandomlyAccessibleFile
Open the file for random access in the specified mode.

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

Specified by:
openForRandomAccess in interface RandomlyAccessibleFile
Parameters:
ram - The mode to open the file in.
Returns:
An open RandomAccess object. It must be closed by the caller.

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

getSize

public long getSize()
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.
See Also:
ReadableFile.getDataSize()