Class FileIO

java.lang.Object
  extended by FileIO
All Implemented Interfaces:
java.lang.Runnable

public class FileIO
extends java.lang.Object
implements java.lang.Runnable

The FileIO class is used to handle files.
Three methods, namely read, write, and append, work in an asynchronous manner. Such methods return immediately and this class calls the corresponding callback method once the operation is achieved. This class uses a single Thread to fulfil all the asynchronous operations. It is therefore not possible to launch a new request before the previous one is finished.

Author:
Michel Deriaz

Field Summary
static int STATE_CONTENT_NULL
          The given content is null.
static int STATE_FILENAME_NULL
          The given filename is null.
static int STATE_NOT_READY
          The class is not ready.
static int STATE_OK
          The operation finished successfully.
static int STATE_PROBLEM
          A problem occured during the operation.
 
Constructor Summary
FileIO(FileIOListener listener)
          Constructs a new FileIO object.
 
Method Summary
 void append(java.lang.String filename, byte[] content)
          Appends the specified bytes to the specified file.
 int create(java.lang.String filename)
          Creates a new file.
 int exists(java.lang.String filename)
          Checks if the specified file or directory exists.
 int mkdir(java.lang.String filename)
          Creates a new directory.
 void read(java.lang.String filename)
          Reads the specified file.
 void run()
           
 void write(java.lang.String filename, byte[] content)
          Writes in the specified file.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATE_OK

public static final int STATE_OK
The operation finished successfully.

See Also:
Constant Field Values

STATE_PROBLEM

public static final int STATE_PROBLEM
A problem occured during the operation. One reason could be that the file does not exist.

See Also:
Constant Field Values

STATE_NOT_READY

public static final int STATE_NOT_READY
The class is not ready.

See Also:
Constant Field Values

STATE_FILENAME_NULL

public static final int STATE_FILENAME_NULL
The given filename is null.

See Also:
Constant Field Values

STATE_CONTENT_NULL

public static final int STATE_CONTENT_NULL
The given content is null.

See Also:
Constant Field Values
Constructor Detail

FileIO

public FileIO(FileIOListener listener)
Constructs a new FileIO object.

Parameters:
listener - the listener that contains the callback methods
Method Detail

mkdir

public int mkdir(java.lang.String filename)
Creates a new directory. Directories in the specified path are not recursively created and must be explicitly created before subdirectories can be created. Returns STATE_FILENAME_NULL if the specified filename is null, STATE_PROBLEM if a problem occurs (like creating a directory that does already exist or specifying a path to the new subdirectory that doesn't exist), and STATE_OK if the new directory is created successfully.

Parameters:
filename - the full filename
Returns:
a state code that describes how the operation ended up

create

public int create(java.lang.String filename)
Creates a new file. This method does not create any directories specified in the file's path. Returns STATE_FILENAME_NULL if the specified filename is null, STATE_PROBLEM if a problem occurs (like creating a file that does already exist or specifying a path that doesn't exist), and STATE_OK if the new file is created successfully.

Parameters:
filename - the full filename
Returns:
a state code that describes how the operation ended up

exists

public int exists(java.lang.String filename)
Checks if the specified file or directory exists. Returns STATE_FILENAME_NULL if the specified filename is null, STATE_PROBLEM if a problem occurs or if the specified file or directory does not exist, and STATE_OK if the file or directory exists.

Parameters:
filename - the full filename
Returns:
a state code that describes how the operation ended up

read

public void read(java.lang.String filename)
Reads the specified file. This method returns immediately. The fileRead callback method is called when the operation ends up.

Parameters:
filename - the full filename
See Also:
FileIOListener.fileRead(int state, byte[] content);

write

public void write(java.lang.String filename,
                  byte[] content)
Writes in the specified file. All former content is erased. This method returns immediately. The fileWritten callback method is called when the operation ends up.

Parameters:
filename - the full filename
content - the bytes to write
See Also:
FileIOListener.fileWritten(int state);

append

public void append(java.lang.String filename,
                   byte[] content)
Appends the specified bytes to the specified file. This method returns immediately. The fileAppended callback method is called when the operation ends up.

Parameters:
filename - the full filename
content - the bytes to append
See Also:
FileIOListener.fileAppended(int state);

run

public void run()
Specified by:
run in interface java.lang.Runnable