org.entityfs.util.io
Class ReadWritableFileAdapter

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

public class ReadWritableFileAdapter
extends AbstractReadWritableFile
implements NamedReadableFile

This class adapts java.io.File to the ReadWritableFile interface. It also implements NamedReadableFile, making it possible to get the file's name.

All locking methods return DummyLock:s.

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

Field Summary
 
Fields inherited from class org.entityfs.util.base.AbstractReadWritableFile
DEFAULT_BUFFER_SIZE
 
Constructor Summary
ReadWritableFileAdapter(File f)
          Create a ReadWritableFile adapter.
ReadWritableFileAdapter(File f, int bufferSize)
          Create a ReadWritableFile adapter.
 
Method Summary
 void delete()
          Delete the file.
 boolean equals(Object o)
           
 File getFile()
          Get the adapted file.
 String getName()
          Get the name.
 long getSize()
          Get the size of the file (in bytes).
 int hashCode()
           
 boolean isDeleted()
          This method returns true if the result from calling File.exists() on the adapted file is false, and vice versa.
 FileChannel openChannelForAppend()
          Open a channel for appending to the adapted file.
 FileChannel openChannelForRead()
          Open a channel for reading from the adapted file.
 FileChannel openChannelForWrite()
          Open a channel for writing on the adapted file.
 OutputStream openForAppend()
          Open a stream for appending to the adapted file.
 RandomAccess openForRandomAccess(RandomAccessMode mode)
          Open the file for random access in the specified mode.
 InputStream openForRead()
          Open a stream for reading from the adapted file.
 OutputStream openForWrite()
          Open a stream for writing on the adapted file.
 void replaceContents(ReadWritableFile f)
          Replace the contents of this file with the contents of the supplied file and then delete the supplied file.
 String toString()
           
 
Methods inherited from class org.entityfs.util.base.AbstractReadWritableFile
copy, copy, getDataSize, getReadLock, getSizeNoLocking, getWriteLock, isReadLockedByCurrentThread, isWriteLockedByCurrentThread, lockForReading, lockForWriting
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.entityfs.ReadableFile
getDataSize
 
Methods inherited from interface org.entityfs.lock.ReadLockable
getReadLock, isReadLockedByCurrentThread, lockForReading
 

Constructor Detail

ReadWritableFileAdapter

public ReadWritableFileAdapter(File f)
Create a ReadWritableFile adapter.

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

Parameters:
f - The file to adapt. It is not checked that the file referred by the object exists and that it is a file.
See Also:
ReadWritableFileAdapter(File, int)

ReadWritableFileAdapter

public ReadWritableFileAdapter(File f,
                               int bufferSize)
                        throws IllegalArgumentException
Create a ReadWritableFile adapter.

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

Parameters:
f - The file to adapt. It is not checked that the file referred by the object exists and that it is a file.
bufferSize - The size of temporary buffers used by this object.
Throws:
IllegalArgumentException - If the buffer size is not greater than zero.
See Also:
ReadWritableFileAdapter(File)
Method Detail

getFile

public File getFile()
Get the adapted file.

Returns:
The adapted file.

openChannelForRead

public FileChannel openChannelForRead()
                               throws WrappedIOException
Open a channel for reading from the adapted file.

Specified by:
openChannelForRead in interface ReadableFile
Returns:
A channel for reading from the file.
Throws:
WrappedIOException - On I/O errors.
See Also:
ReadableFile.openForRead()

openForRead

public InputStream openForRead()
                        throws WrappedIOException
Open a stream for reading from the adapted file.

Specified by:
openForRead in interface ReadableFile
Returns:
A stream for reading from the file.
Throws:
WrappedIOException - On I/O errors.

openChannelForWrite

public FileChannel openChannelForWrite()
                                throws WrappedIOException
Open a channel for writing on the adapted file. The previous contents of the file will be discarded.

Specified by:
openChannelForWrite in interface WritableFile
Returns:
A channel for writing to the file.
Throws:
WrappedIOException - On I/O errors.
See Also:
WritableFile.openForWrite(), WritableFile.openChannelForAppend()

openForWrite

public OutputStream openForWrite()
                          throws WrappedIOException
Open a stream for writing on the adapted file. The previous contents of the file will be discarded.

Specified by:
openForWrite in interface WritableFile
Returns:
A stream for writing to the file.
Throws:
WrappedIOException - On I/O errors.
See Also:
WritableFile.openForAppend(), WritableFile.openChannelForWrite()

openChannelForAppend

public FileChannel openChannelForAppend()
                                 throws WrappedIOException
Open a channel for appending to the adapted file.

Specified by:
openChannelForAppend in interface WritableFile
Returns:
A channel for appending to the file.
Throws:
WrappedIOException - On I/O errors.

openForAppend

public OutputStream openForAppend()
                           throws WrappedIOException
Open a stream for appending to the adapted file.

Specified by:
openForAppend in interface WritableFile
Returns:
A stream for appending to the file.
Throws:
WrappedIOException - On I/O errors.
See Also:
WritableFile.openForWrite(), WritableFile.openChannelForAppend()

openForRandomAccess

public RandomAccess openForRandomAccess(RandomAccessMode mode)
                                 throws WrappedIOException
Open the file for random access in the specified mode.

Specified by:
openForRandomAccess in interface RandomlyAccessibleFile
Parameters:
mode - The mode to open the file in.
Returns:
a RandomAccess object.
Throws:
WrappedIOException - On I/O errors.

delete

public void delete()
            throws BackendErrorException
Delete the file.

Specified by:
delete in interface Deletable
Throws:
BackendErrorException - If the file cannot be deleted.

isDeleted

public boolean isDeleted()
This method returns true if the result from calling File.exists() on the adapted file is false, and vice versa.

Specified by:
isDeleted in interface Deletable
Returns:
true if the entity object has been deleted.

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
Overrides:
replaceContents in class AbstractReadWritableFile
Parameters:
f - The file whose contents are to be moved to this file. f is deleted by this method.

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 file length.
See Also:
ReadableFile.getDataSize()

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.

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object