fi.hu.cs.titokone
Class FileHandler

java.lang.Object
  extended byfi.hu.cs.titokone.FileHandler

public class FileHandler
extends java.lang.Object

This class transforms files into various sorts of buffer classes depending on who needs them, and saves these buffer classes to files when needed. The buffer classes will not be dependent on files and I/O operations anymore, and therefore will not throw eg. IOExceptions when read.


Field Summary
static int APPEND_ACCESS
          Append access to file
private  java.util.logging.Logger logger
          This logger will be used for logging the I/O activities.
static java.lang.String loggerName
          This class has its own logger.
static int READ_ACCESS
          Read only access to file
static int WRITE_ACCESS
          Write access to file
 
Constructor Summary
FileHandler()
          This class sets up a new FileHandler and sets up its Logger.
 
Method Summary
 void appendDataToStdOut(java.lang.String dataItem, java.io.File stdoutFile)
          This method appends data to a stdout file.
 java.io.File changeExtension(java.io.File f, java.lang.String newExtension)
          This method changes the extension of the given filename to newExtension and returns the new filename as a File object.
private  java.lang.String changeExtensionStr(java.lang.String filename, java.lang.String newExtension)
          This method returns the first string modified so that the part of it following the last period is removed, including the period, and the result is this modified followed by newExtension.
 Binary loadBinary(java.io.File binaryFile)
          This function loads a Binary from a binary .b91 file and returns the result.
private  java.lang.StringBuffer loadFileContentsToString(java.io.File loadFile)
          This function is a private assistant method for FileHandler and it loads the contents of the given file into a string and returns that string.
private  java.lang.StringBuffer loadReaderContentsToString(java.io.BufferedReader reader)
          This function is a private assistant method, which loads the contents of a given reader into a string and returns that string.
 java.util.ResourceBundle loadResourceBundle(java.io.File rbFile)
          This method attempts to load a resource bundle from a file (with an URLClassLoader).
 java.lang.StringBuffer loadSettings(java.io.File settingsFile)
          This function loads a settings file into a StringBuffer.
 java.lang.StringBuffer loadSettings(java.io.InputStream settingsStream)
          This function loads a settings input stream to a StringBuffer.
 Source loadSource(java.io.File srcFile)
          This function loads up a Source file from a given file.
 java.lang.StringBuffer loadStdIn(java.io.File stdinFile)
          This method loads a "stdin" file representing the disk into a string.
 void saveBinary(Binary bin, java.io.File binarySaveFile)
          This method saves a Binary to file in a .b91 binary format.
 void saveSettings(java.lang.String settingsData, java.io.File settingsFile)
          This method saves settings data from a StringBuffer to a file.
 void saveSource(Source src, java.io.File srcFile)
          This method is used to save a source that has been modified.
private  void saveStringToFile(java.lang.String str, java.io.File saveFile)
          This method is a private helper method which handles saving strings to files.
 void testAccess(java.io.File accessedFile, int accessType)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private java.util.logging.Logger logger
This logger will be used for logging the I/O activities.


loggerName

public static java.lang.String loggerName
This class has its own logger.


READ_ACCESS

public static final int READ_ACCESS
Read only access to file

See Also:
Constant Field Values

APPEND_ACCESS

public static final int APPEND_ACCESS
Append access to file

See Also:
Constant Field Values

WRITE_ACCESS

public static final int WRITE_ACCESS
Write access to file

See Also:
Constant Field Values
Constructor Detail

FileHandler

public FileHandler()
This class sets up a new FileHandler and sets up its Logger.

Method Detail

loadSource

public Source loadSource(java.io.File srcFile)
                  throws java.io.IOException
This function loads up a Source file from a given file.

Parameters:
srcFile - The identifier of the file to read from.
Returns:
A source instance which is no longer dependent on I/O.
Throws:
java.io.IOException - If an I/O error occurs. Eg. one of the possible IOExceptions is FileNotFoundException.

saveSource

public void saveSource(Source src,
                       java.io.File srcFile)
                throws java.io.IOException
This method is used to save a source that has been modified.

Parameters:
src - The source object to save to file.
srcFile - The file to save the source to.
Throws:
java.io.IOException - If an I/O error occurds.

loadSettings

public java.lang.StringBuffer loadSettings(java.io.File settingsFile)
                                    throws java.io.IOException
This function loads a settings file into a StringBuffer.

Parameters:
settingsFile - The identifier of the file to read from.
Returns:
A StringBuffer which no longer depends on I/O.
Throws:
java.io.IOException - If an I/O error occurs. Eg. one of the possible IOExceptions is FileNotFoundException.

loadSettings

public java.lang.StringBuffer loadSettings(java.io.InputStream settingsStream)
                                    throws java.io.IOException
This function loads a settings input stream to a StringBuffer.

Parameters:
settingsStream - An input stream to read the contents from.
Returns:
A StringBuffer containing the stream's contents, linebreaks unmodified, or null if the settingsStream was null.
Throws:
java.io.IOException - If an I/O error occurs while reading or closing the stream.

saveSettings

public void saveSettings(java.lang.String settingsData,
                         java.io.File settingsFile)
                  throws java.io.IOException
This method saves settings data from a StringBuffer to a file. The line separator will be System.getProperty("line.separator", "\n").

Parameters:
settingsData - The settings data in a StringBuffer in the form it is to be saved in. The linebreaks in the file will be \ns.
settingsFile - The identifier of the file to save to.
Throws:
java.io.IOException - If an I/O error occurs, eg. the directory the file should be in does not exist or we cannot write to it.

loadBinary

public Binary loadBinary(java.io.File binaryFile)
                  throws java.io.IOException,
                         java.text.ParseException
This function loads a Binary from a binary .b91 file and returns the result. The Binary class checks itself upon creation and throws a ParseException if it is not syntactically correct.

Parameters:
binaryFile - Identifier of the file to read from.
Returns:
An Binary instance containing the contents of the .b91 file.
Throws:
java.io.IOException - If an I/O error occurs. Eg. one of the possible IOExceptions is FileNotFoundException.
java.text.ParseException - If the file does not contain a valid binary.

saveBinary

public void saveBinary(Binary bin,
                       java.io.File binarySaveFile)
                throws java.io.IOException
This method saves a Binary to file in a .b91 binary format.

Parameters:
bin - The binary to save to file.
binarySaveFile - The identifier for the file to save to.
Throws:
java.io.IOException - If an I/O error occurs, eg. the given file cannot be written to.

loadStdIn

public java.lang.StringBuffer loadStdIn(java.io.File stdinFile)
                                 throws java.io.IOException
This method loads a "stdin" file representing the disk into a string. The contents should be integers delimited by \n, \r or \r\n, but the loader does not check that this is the case.

Parameters:
stdinFile - The identifier for the file to read from.
Returns:
A stringbuffer containing the contents of the file.
Throws:
java.io.IOException - If an I/O error occurs, eg. the given file is not found.

appendDataToStdOut

public void appendDataToStdOut(java.lang.String dataItem,
                               java.io.File stdoutFile)
                        throws java.io.IOException
This method appends data to a stdout file. If the file does not exist, it is created.

Parameters:
dataItem - The data to append to the file (a newline is added automagically).
stdoutFile - The file to append to.
Throws:
java.io.IOException - If an I/O error occurs.

loadResourceBundle

public java.util.ResourceBundle loadResourceBundle(java.io.File rbFile)
                                            throws ResourceLoadFailedException
This method attempts to load a resource bundle from a file (with an URLClassLoader). It bundles up the various exceptions possibly created by this into ResourceLoadFailedException.

Parameters:
rbFile - The filename to load and instantiate the ResourceBundle from.
Returns:
A ResourceBundle found from the file.
Throws:
ResourceLoadFailedException - If the file load would cast an IOException, or the class loading would cast a ClassNotFoundException or the instantiation would cast a InstantiationException or the cast a ClassCastException.

loadFileContentsToString

private java.lang.StringBuffer loadFileContentsToString(java.io.File loadFile)
                                                 throws java.io.IOException
This function is a private assistant method for FileHandler and it loads the contents of the given file into a string and returns that string. It may throw an IOException in case of a read error.

Throws:
java.io.IOException

loadReaderContentsToString

private java.lang.StringBuffer loadReaderContentsToString(java.io.BufferedReader reader)
                                                   throws java.io.IOException
This function is a private assistant method, which loads the contents of a given reader into a string and returns that string. The lines will be read using .readLine() and recombined with \ns.

Throws:
java.io.IOException - If an I/O error occurs while reading the file.

saveStringToFile

private void saveStringToFile(java.lang.String str,
                              java.io.File saveFile)
                       throws java.io.IOException
This method is a private helper method which handles saving strings to files.

Throws:
java.io.IOException

changeExtension

public java.io.File changeExtension(java.io.File f,
                                    java.lang.String newExtension)
This method changes the extension of the given filename to newExtension and returns the new filename as a File object. File extensions are considered to be the part after the last period in the File.getName(). If there are no periods in that part, the file is considered to be without an extension and newExtension is added.


changeExtensionStr

private java.lang.String changeExtensionStr(java.lang.String filename,
                                            java.lang.String newExtension)
This method returns the first string modified so that the part of it following the last period is removed, including the period, and the result is this modified followed by newExtension. If newExtension is not an empty string, the two are separated with a ".".


testAccess

public void testAccess(java.io.File accessedFile,
                       int accessType)
                throws java.io.IOException
Throws:
java.io.IOException