org.entityfs.util.base
Class AbstractReadWritableFile

java.lang.Object
  extended by org.entityfs.util.base.AbstractReadWritableFile
All Implemented Interfaces:
Deletable, Lockable, ReadLockable, ReadWriteLockable, WriteLockable, RandomlyAccessibleFile, ReadableFile, ReadWritableFile, WritableFile
Direct Known Subclasses:
ReadWritableFileAdapter

public abstract class AbstractReadWritableFile
extends Object
implements ReadWritableFile

This class can be used as a starting point for ReadWritableFile implementations.

The locking methods of this class uses DummyLock:s.

Since:
1.0
Author:
Karl Gustafsson

Field Summary
static int DEFAULT_BUFFER_SIZE
          The default size, in bytes, of internal buffers if no buffer size is set when creating an instance.
 
Constructor Summary
protected AbstractReadWritableFile()
          Create a ReadWritableFile object.
protected AbstractReadWritableFile(int bufferSize)
          Create a ReadWritableFile object.
 
Method Summary
 long copy(OutputStream os)
          Dump the file contents to the stream.
 long copy(WritableByteChannel oc)
          Copy the file's contents to the WritableByteChannel.
 long getDataSize()
          This default implementation returns the result from the ReadableFile.getSize() method.
 EntityLock getReadLock()
          This returns a dummy lock.
 long getSizeNoLocking()
          This default implementation returns the result from the ReadableFile.getSize() method.
 EntityLock getWriteLock()
          This returns a dummy lock.
 boolean isReadLockedByCurrentThread()
          This method always returns true.
 boolean isWriteLockedByCurrentThread()
          This method always returns true.
 EntityLock lockForReading()
          This returns a dummy lock.
 EntityLock lockForWriting()
          This returns a dummy lock.
 void replaceContents(ReadWritableFile f)
          Replace the contents of this file with the contents of the supplied file and then delete the supplied file.
 
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
getSize, openChannelForRead, openForRead
 
Methods inherited from interface org.entityfs.WritableFile
openChannelForAppend, openChannelForWrite, openForAppend, openForWrite
 
Methods inherited from interface org.entityfs.RandomlyAccessibleFile
openForRandomAccess
 
Methods inherited from interface org.entityfs.Deletable
delete, isDeleted
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default size, in bytes, of internal buffers if no buffer size is set when creating an instance.

See Also:
Constant Field Values
Constructor Detail

AbstractReadWritableFile

protected AbstractReadWritableFile()
Create a ReadWritableFile object.

If this object has to create a temporary buffer for storing data in, it will use a buffer of the default size, DEFAULT_BUFFER_SIZE.

See Also:
AbstractReadWritableFile(int)

AbstractReadWritableFile

protected AbstractReadWritableFile(int bufferSize)
                            throws IllegalArgumentException
Create a ReadWritableFile object.

If this object has to create a temporary buffer for storing data in, it will use a buffer of the supplied size.

Parameters:
bufferSize - The size of internal buffers.
Throws:
IllegalArgumentException - If the buffer size is not greater than zero.
See Also:
AbstractReadWritableFile()
Method Detail

getReadLock

public EntityLock getReadLock()
This returns a dummy lock.

Specified by:
getReadLock in interface ReadLockable
Returns:
The DummyLock instance.

lockForReading

public EntityLock lockForReading()
This returns a dummy lock.

Specified by:
lockForReading in interface ReadLockable
Returns:
The DummyLock instance.

isReadLockedByCurrentThread

public boolean isReadLockedByCurrentThread()
This method always returns true.

Specified by:
isReadLockedByCurrentThread in interface ReadLockable
Returns:
true.

getWriteLock

public EntityLock getWriteLock()
This returns a dummy lock.

Specified by:
getWriteLock in interface WriteLockable
Returns:
The DummyLock instance.

lockForWriting

public EntityLock lockForWriting()
This returns a dummy lock.

Specified by:
lockForWriting in interface WriteLockable
Returns:
The DummyLock instance.

isWriteLockedByCurrentThread

public boolean isWriteLockedByCurrentThread()
This method always returns true.

Specified by:
isWriteLockedByCurrentThread in interface WriteLockable
Returns:
true.

replaceContents

public void replaceContents(ReadWritableFile f)
Description copied from interface: ReadWritableFile
Replace the contents of this file with the contents of the supplied file and then delete the supplied file. If both files are in the same file system, an optimized implementation simply moves the contents of f over to this file, which makes the entire operation quick and not requiring any extra disk space.

If both files support metadata, this file's metadata is replaced with that of f.

Specified by:
replaceContents in interface ReadWritableFile
Parameters:
f - The file whose contents are to be moved to this file. f is deleted by this method.

getDataSize

public long getDataSize()
This default implementation returns the result from the ReadableFile.getSize() method.

Specified by:
getDataSize in interface ReadableFile
Returns:
The size of data in the file.
See Also:
ReadableFile.getSize()

getSizeNoLocking

public long getSizeNoLocking()
This default implementation returns the result from the ReadableFile.getSize() method.

Specified by:
getSizeNoLocking in interface ReadWritableFile
Returns:
The approximate size of the file, in bytes. If the file object is invalid, 0 is returned.
See Also:
ReadableFile.getSize()

copy

public long copy(OutputStream os)
Description copied from interface: ReadWritableFile
Dump the file contents to the stream.

Specified by:
copy in interface ReadWritableFile
Parameters:
os - The stream the file contents are written to. The stream is not closed after writing, that must be done by the caller.
Returns:
The number of bytes written to the channel.

copy

public long copy(WritableByteChannel oc)
Description copied from interface: ReadWritableFile
Copy the file's contents to the WritableByteChannel.

Specified by:
copy in interface ReadWritableFile
Parameters:
oc - The WritableByteChannel. The channel is not closed by this method.
Returns:
The number of bytes written to the channel.