uk.ac.rdg.resc.jstyx.server
Class FileOnDisk

java.lang.Object
  extended by uk.ac.rdg.resc.jstyx.server.StyxFile
      extended by uk.ac.rdg.resc.jstyx.server.FileOnDisk

public class FileOnDisk
extends StyxFile

Class representing a file on a hard drive. Note that creating a new instance of this class does not create an actual new file on the hard disk; it simply creates a wrapper for an existing file. This class opens a new Input/OutputStream every time a read/write message arrives, so it is probably not very efficient (but this was the simplest way to implement reliably).

Author:
Jon Blower $Revision: 476 $ $Date: 2005-11-09 17:42:24 +0000 (Wed, 09 Nov 2005) $ $Log$ Revision 1.19 2005/11/09 17:42:24 jonblower Modified EOF recognition (now requires zero-byte message to be written to the end of the file) Revision 1.18 2005/11/04 19:34:35 jonblower Added code to recognise EOF (i.e. zero-byte) write messages Revision 1.15 2005/09/08 07:08:59 jonblower Removed "String user" from list of parameters to StyxFile.write() Revision 1.13 2005/05/19 14:47:38 jonblower Realised that new RandomAccessFile("..", "rw") doesn't throw FileNotFoundException Revision 1.12 2005/05/11 10:33:50 jonblower Implemented MonitoredFileOnDisk.java Revision 1.11 2005/05/10 19:19:05 jonblower Reinstated chan.truncate() Revision 1.10 2005/05/10 12:43:57 jonblower Rewrote and simplified: now closes file after each read or write Revision 1.9 2005/05/10 08:02:18 jonblower Changes related to implementing MonitoredFileOnDisk Revision 1.8 2005/05/09 07:13:52 jonblower Changed getFileOnDisk() to getFileOrDirectoryOnDisk() Revision 1.7 2005/04/28 08:11:15 jonblower Modified permissions handling in documentation directory of SGS Revision 1.6 2005/04/27 16:11:43 jonblower Added capability to add documentation files to SGS namespace Revision 1.5 2005/03/24 09:48:31 jonblower Changed 'count' from long to int throughout for reading and writing Revision 1.4 2005/03/19 21:47:02 jonblower Further fixes relating to releasing ByteBuffers Revision 1.3 2005/03/16 17:56:23 jonblower Replaced use of java.nio.ByteBuffer with MINA's ByteBuffer to minimise copying of buffers Revision 1.2 2005/03/11 14:02:16 jonblower Merged MINA-Test_20059309 into main line of development Revision 1.1.1.1.2.1 2005/03/10 20:55:37 jonblower Removed references to Netty Revision 1.1.1.1 2005/02/16 18:58:31 jonblower Initial import

Field Summary
protected  boolean eofWritten
           
protected  java.io.File file
           
protected  boolean mustExist
           
 
Fields inherited from class uk.ac.rdg.resc.jstyx.server.StyxFile
auth, directory, lastModifiedTime, name, parent
 
Constructor Summary
FileOnDisk(java.io.File file)
          Creates a new FileOnDisk whose name is the same as that of (the last part of) the underlying file (i.e.
FileOnDisk(java.io.File file, boolean mustExist)
          Creates a FileOnDisk with default permissions (0666).
FileOnDisk(java.lang.String filepath)
          Creates a new FileOnDisk that wraps the file at the given path
FileOnDisk(java.lang.String name, java.io.File file)
          Creates a FileOnDisk with the default permissions (0666, rw-rw-rw-)
FileOnDisk(java.lang.String name, java.io.File file, int permissions, boolean mustExist)
           
 
Method Summary
protected  void delete()
          Closes the open FileChannel and removes the underlying file from the disk
static StyxFile getFileOrDirectoryOnDisk(java.io.File file)
          Gets a StyxFile that wraps the given java.io.File.
 ULong getLength()
           
 void read(StyxFileClient client, long offset, int count, int tag)
          Reads from the underlying java.io.File.
 boolean receivedEOF()
           
 void refresh()
          Reads all metadata from underlying disk file
 void write(StyxFileClient client, long offset, int count, org.apache.mina.common.ByteBuffer data, boolean truncate, int tag)
          Writes data to the underlying java.io.File.
 
Methods inherited from class uk.ac.rdg.resc.jstyx.server.StyxFile
addChangeListener, addClient, checkSetLastModifiedTime, checkSetLength, checkSetMode, checkSetName, clientConnected, clientDisconnected, contentsChanged, fireContentsChanged, getClient, getDirEntry, getFullPath, getGroup, getName, getNumClients, getOwner, getParent, getPermissions, getQid, getVersion, incrementVersion, isAppendOnly, isAuth, isDirectory, isExclusive, processAndReplyRead, processAndReplyRead, processAndReplyRead, remove, removeChangeListener, removeClient, rename, replyRead, replyRead, replyRead, replyRead, replyRead, replyWrite, setLastAccessTime, setLastModified, setLength, setMode, setName, setPermissions, setReadOnly
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

file

protected java.io.File file

mustExist

protected boolean mustExist

eofWritten

protected boolean eofWritten
Constructor Detail

FileOnDisk

public FileOnDisk(java.lang.String filepath)
           throws StyxException
Creates a new FileOnDisk that wraps the file at the given path

Throws:
StyxException

FileOnDisk

public FileOnDisk(java.io.File file)
           throws StyxException
Creates a new FileOnDisk whose name is the same as that of (the last part of) the underlying file (i.e. file.getName())

Parameters:
file - The java.io.File which this represents
Throws:
StyxException - if the java.io.File does not exist

FileOnDisk

public FileOnDisk(java.lang.String name,
                  java.io.File file)
           throws StyxException
Creates a FileOnDisk with the default permissions (0666, rw-rw-rw-)

Parameters:
name - The name of this file as it will appear in the namespace
file - The java.io.File which this represents
Throws:
StyxException - if the java.io.File does not exist

FileOnDisk

public FileOnDisk(java.io.File file,
                  boolean mustExist)
           throws StyxException
Creates a FileOnDisk with default permissions (0666). The name of the file in the namespace will be file.getName().

Parameters:
file - The java.io.File which this represents
mustExist - If this is true, then an exception will be thrown by this constructor if the underlying java.io.File does not exist. Furthermore, if the File is deleted, the FileOnDisk will be removed from the namespace. If this is set false, a non-existent File will be represented as a zero-length read-only FileOnDisk in the namespace.
Throws:
StyxException - if the java.io.File does not exist and mustExist=true

FileOnDisk

public FileOnDisk(java.lang.String name,
                  java.io.File file,
                  int permissions,
                  boolean mustExist)
           throws StyxException
Parameters:
name - The name of this file as it will appear in the namespace
file - The java.io.File which this represents
permissions - the permissions of the file
mustExist - If this is true, then an exception will be thrown by this constructor if the underlying java.io.File does not exist. Furthermore, if the File is deleted, the FileOnDisk will be removed from the namespace. If this is set false, a non-existent File will be represented as a zero-length read-only FileOnDisk in the namespace.
Throws:
StyxException - if the java.io.File does not exist and mustExist=true
Method Detail

getFileOrDirectoryOnDisk

public static StyxFile getFileOrDirectoryOnDisk(java.io.File file)
                                         throws StyxException
Gets a StyxFile that wraps the given java.io.File. If the File is a directory, this will return an instance of DirectoryOnDisk, otherwise it will return an instance of FileOnDisk.

Throws:
StyxException - if the File does not exist

read

public void read(StyxFileClient client,
                 long offset,
                 int count,
                 int tag)
          throws StyxException
Reads from the underlying java.io.File. This method opens a new file channel, reads the data, then closes the channel again. If the java.io.File does not exist and mustExist==true, this method will throw a StyxException. If the File does not exist and mustExist==false, this method will simply return zero bytes to the client.

Overrides:
read in class StyxFile
Parameters:
client - The client that is performing the read
offset - The point in the file at which to start reading
count - The maximum number of bytes to read
tag - The tag of the incoming Tread message (this is needed when calling readReply())
Throws:
StyxException

write

public void write(StyxFileClient client,
                  long offset,
                  int count,
                  org.apache.mina.common.ByteBuffer data,
                  boolean truncate,
                  int tag)
           throws StyxException
Writes data to the underlying java.io.File. If the File does not exist and mustExist is true, this throws a StyxException. If the File does not exist and mustExist is false.

Overrides:
write in class StyxFile
Parameters:
client - The client that is performing the write operation
offset - The place in the file where the new data will be added
count - The number of bytes to write
data - The data to write. The position and limit of this ByteBuffer will be set correctly, but subclasses should note that the position might not be zero.
truncate - If this is true the file will be truncated at the end of the new data
tag - The tag of the incoming Twrite message (this is needed when calling writeReply())
Throws:
StyxException

refresh

public void refresh()
Reads all metadata from underlying disk file

Overrides:
refresh in class StyxFile

getLength

public ULong getLength()
Overrides:
getLength in class StyxFile
Returns:
the length of the file, or zero if the file does not exist

receivedEOF

public boolean receivedEOF()
Returns:
true if we have received an EOF message from the client when the client uploaded data to this file and no data have been received since this EOF message was received.

delete

protected void delete()
Closes the open FileChannel and removes the underlying file from the disk

Overrides:
delete in class StyxFile


Copyright © 2004-2006 Reading e-Science Centre. All Rights Reserved.