org.optimizationservices.oscommon.util
Class IOUtil

java.lang.Object
  extended by org.optimizationservices.oscommon.util.IOUtil
All Implemented Interfaces:
java.io.Serializable

public class IOUtil
extends java.lang.Object
implements java.io.Serializable

The IOUtil class contains methods for performing common basic input-output (I/O) operations, such as file reading/writing, used by various components in the Optimization Services (OS) framework.

Since:
OS1.0
Version:
1.0, 03/14/2004
Author:
Robert Fourer, Jun Ma, Kipp Martin
See Also:
Serialized Form

Field Summary
 java.lang.String m_sName
          m_sName holds the name of this class.
 
Constructor Summary
IOUtil()
           
 
Method Summary
static boolean appendStringToFile(java.lang.String string, java.lang.String fileName)
          Appends a string to a file.
static java.lang.String bytesToEncodedString(byte[] byteArray)
          Converts an array of bytes into a base64 encoded string.
static boolean bytesToFile(byte[] byteArray, java.lang.String fileName)
          Save an array of bytes into a file.
static java.lang.Object bytesToObject(byte[] byteArray)
          Converts an array of bytes into an object.
static java.lang.String bytesToString(byte[] byteArray)
          Converts an array of bytes into a string (unencoded).
static boolean cleanUpDisk()
          clean up disks, mainly including deleting temp files.
static boolean copyFile(java.lang.String from, java.lang.String to)
          copy a file.
static boolean createFile(java.lang.String file)
          Create a file.
static java.lang.String decodeString(java.lang.String encodedString)
          Deconde a base64 encoded string.
static boolean deleteDir(java.lang.String dir)
          Delete a directory.
static boolean deleteFile(java.lang.String file)
          delete a file.
static byte[] encodedStringToBytes(java.lang.String string)
          Decodes a base64 encoded string and return them in an array of bytes.
static boolean encodedStringToFile(java.lang.String string, java.lang.String fileName)
          Decodes and saves a base64 encoded string into a file.
static java.lang.Object encodedStringToObject(java.lang.String string)
          Decodes a base64 encoded string and return them in an object.
static java.lang.String encodeString(java.lang.String string)
          Encode a plain string with base64 encoding.
static java.lang.String exceptionStackToString(java.lang.Exception e)
          Convert printStackTrace() to a String.
static boolean existsFileOrDir(java.lang.String path)
          check if a file or directory exists.
static byte[] fileToBytes(java.lang.String fileName)
          Retrieve a file into an array of bytes.
static java.lang.String fileToEncodedString(java.lang.String fileName)
          Converts any file into a base64 encoded string.
static java.io.InputStream fileToInputStream(java.lang.String fileName)
           
static java.lang.Object fileToObject(java.lang.String fileName)
          Converts file that was previously serialized from an object into an object in memory.
static java.io.OutputStream fileToOnputStream(java.lang.String fileName)
           
static java.lang.String fileToString(java.lang.String fileName)
          Read a file into a string (unencoded).
static java.lang.String getCurrentDir()
          Get the current directory.
static double getFreeDiskSpace(java.lang.String path)
          get free disk space.
static long getLastWriteTime(java.lang.String file)
          get the last write time of the file.
static java.lang.String getParentDir()
          Get the parent directory.
static boolean isDir(java.lang.String path)
          test whether a path is a directory or not.
static boolean log(java.lang.String logMessage, java.lang.String logFileName)
           
static void main(java.lang.String[] args)
          main for test purposes.
static boolean makeDir(java.lang.String dir)
          Make a directory.
static boolean moveFile(java.lang.String from, java.lang.String to)
          move a file.
static byte[] objectToBytes(java.lang.Object object)
          Return an object in an array of bytes.
static java.lang.String objectToEncodedString(java.lang.Object object)
          Return an object in a base64 encoded string.
static boolean objectToFile(java.lang.Object object, java.lang.String fileName)
          Save a object into a file.
static boolean print(java.lang.String s)
          Print out a string message.
static boolean print(java.lang.String s, java.io.OutputStream outputStream)
          Print out a string message.
static boolean println()
          Print out a line feed "\n".
static boolean println(java.lang.String s)
          Print out a string message with an extra line feed "\n".
static byte[] readBytesFromURL(java.lang.String url)
          Reads from a url to a string in memory.
static java.lang.String readStringFromFile(java.lang.String fileName)
          Reads from a file to a string in memory.
static java.lang.String readStringFromURL(java.lang.String url)
          Reads from a url to a string in memory.
static boolean stringFileToBinaryFile(java.lang.String inFileName, java.lang.String outFileName)
          Converts a string file to a binary file.
static byte[] stringToBytes(java.lang.String string)
          Return a string (unencoded) in an array of bytes.
static boolean stringToFile(java.lang.String string, java.lang.String fileName)
          Saves a string into a file.
static java.io.InputStream stringToInputStream(java.lang.String string)
           
static boolean writeStringToFile(java.lang.String string, java.lang.String fileName)
          Writes a string to a file.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_sName

public java.lang.String m_sName
m_sName holds the name of this class.

Constructor Detail

IOUtil

public IOUtil()
Method Detail

encodeString

public static java.lang.String encodeString(java.lang.String string)
Encode a plain string with base64 encoding.

Parameters:
string - holds the plain string to be encoded.
Returns:
the base64 encoded string.

decodeString

public static java.lang.String decodeString(java.lang.String encodedString)
Deconde a base64 encoded string.

Parameters:
string - holds the base64 encoded string to be decoded into a plain string.
Returns:
the plain string that is decoded.

fileToString

public static java.lang.String fileToString(java.lang.String fileName)
Read a file into a string (unencoded).

Parameters:
fileName - holds the name of the file to read.
Returns:
the file in a string.

stringToFile

public static boolean stringToFile(java.lang.String string,
                                   java.lang.String fileName)
Saves a string into a file.

Parameters:
string - holds the string to be saved.
fileName - holds the name of the file to save the string.
Returns:
whether the string is saved to a file successfully.

fileToEncodedString

public static java.lang.String fileToEncodedString(java.lang.String fileName)
Converts any file into a base64 encoded string.

Parameters:
fileName - holds the name of the file to be converted to a base64 encoded string.
Returns:
the base64 encoded string from the file. The method returns null if an error occurs.

encodedStringToFile

public static boolean encodedStringToFile(java.lang.String string,
                                          java.lang.String fileName)
Decodes and saves a base64 encoded string into a file.

Parameters:
string - holds base64 encoded string to be decoded and saved.
fileName - holds the name of the file to save the string.
Returns:
whether the encoded string is decoded and saved to a file successfully.

bytesToString

public static java.lang.String bytesToString(byte[] byteArray)
Converts an array of bytes into a string (unencoded).

Parameters:
byteArray - holds the array of bytes to be converted to a string.
Returns:
the string from the bytes.

stringToBytes

public static byte[] stringToBytes(java.lang.String string)
Return a string (unencoded) in an array of bytes.

Parameters:
string - holds string to be put in an array of bytes.
Returns:
the string in an array of bytes.

bytesToEncodedString

public static java.lang.String bytesToEncodedString(byte[] byteArray)
Converts an array of bytes into a base64 encoded string.

Parameters:
byteArray - holds the array of bytes to be converted to a base64 encoded string.
Returns:
the base64 encoded string from the bytes.

encodedStringToBytes

public static byte[] encodedStringToBytes(java.lang.String string)
Decodes a base64 encoded string and return them in an array of bytes.

Parameters:
string - holds base64 encoded string to be decoded and put in an array of bytes.
Returns:
the decoded string in an array of bytes. The method returns null if an error occurs.

objectToBytes

public static byte[] objectToBytes(java.lang.Object object)
Return an object in an array of bytes.

Parameters:
object - holds the serializable object to be put into an array of bytes.
Returns:
the object in an array of bytes. The object has to implement the interface of java.io.Serializable. The method returns null if an error occurs.

bytesToObject

public static java.lang.Object bytesToObject(byte[] byteArray)
Converts an array of bytes into an object.

Parameters:
byteArray - holds the array of bytes to be converted to an object.
Returns:
the object. The object can then be type cast into a more concrete object that implements the java.io.Serializable. The method returns null if an error occurs.

objectToEncodedString

public static java.lang.String objectToEncodedString(java.lang.Object object)
Return an object in a base64 encoded string.

Parameters:
object - holds the serializable object to be encoded to a string.
Returns:
the object in a base64 encoded string. The object has to implement the interface of java.io.Serializable. The method returns null if an error occurs.

encodedStringToObject

public static java.lang.Object encodedStringToObject(java.lang.String string)
Decodes a base64 encoded string and return them in an object.

Parameters:
string - holds base64 encoded string to be decoded and converted to an object.
Returns:
the object. The object can then be type cast into a more concrete object that implements the java.io.Serializable. The method returns null if an error occurs.

objectToFile

public static boolean objectToFile(java.lang.Object object,
                                   java.lang.String fileName)
Save a object into a file.

Parameters:
object - holds the serializable object to be saved into a file.
fileName - holds the name of the file to save the object.
Returns:
whether the object is saved to a file successfully. The object has to implement the interface of java.io.Serializable.

fileToObject

public static java.lang.Object fileToObject(java.lang.String fileName)
Converts file that was previously serialized from an object into an object in memory.

Parameters:
fileName - holds the name of the file to was previsouly serialized from an object.
Returns:
the object. The object can then be type cast into a more concrete object that implements the java.io.Serializable. The method returns null if an error occurs.

bytesToFile

public static boolean bytesToFile(byte[] byteArray,
                                  java.lang.String fileName)
Save an array of bytes into a file.

Parameters:
byteArray - holds an array of bytes to be saved into a file.
fileName - holds the name of the file to save the array of bytes.
Returns:
whether the array of bytes is saved to a file successfully.

fileToBytes

public static byte[] fileToBytes(java.lang.String fileName)
Retrieve a file into an array of bytes.

Parameters:
fileName - holds the name of the file to be retrieved into the array of bytes.
Returns:
the array of bytes that is retrieved from the file.

stringFileToBinaryFile

public static boolean stringFileToBinaryFile(java.lang.String inFileName,
                                             java.lang.String outFileName)
Converts a string file to a binary file.

Parameters:
inFileName - holds the name of the string file.
outFileName - holds the name of the binary file to be converted.
Returns:
whether the conversion from string file to binary is successful or not.

readStringFromFile

public static java.lang.String readStringFromFile(java.lang.String fileName)
Reads from a file to a string in memory.

Parameters:
fileName - holds the file name of the file to be converted into a string
Returns:
the string to that is constructed from the file. A null value will be returned if there is any error in the reading.

writeStringToFile

public static boolean writeStringToFile(java.lang.String string,
                                        java.lang.String fileName)
Writes a string to a file.

Parameters:
string - holds the string to be written to a file.
fileName - holds the file name of the file to save the string.
Returns:
whether the string is saved to the file successfully or not.

appendStringToFile

public static boolean appendStringToFile(java.lang.String string,
                                         java.lang.String fileName)
Appends a string to a file.

Parameters:
string - holds the string to be appended to a file.
fileName - holds the file name of the file to append the string.
Returns:
whether the string is appended to the file successfully or not.

readStringFromURL

public static java.lang.String readStringFromURL(java.lang.String url)
Reads from a url to a string in memory.

Parameters:
url - holds the link address of the content to be converted into a string.
Returns:
the string constructed from reading the content of the link. A null value will be returned if there is any error in the reading.

readBytesFromURL

public static byte[] readBytesFromURL(java.lang.String url)
Reads from a url to a string in memory.

Parameters:
url - holds the link address of the content to be converted into a string.
Returns:
the bytes constructed from reading the content of the link. A null value will be returned if there is any error in the reading.

stringToInputStream

public static java.io.InputStream stringToInputStream(java.lang.String string)
Parameters:
string - holds the string to be converted to an inputstream.
Returns:
an inputStream that is constructed from the string.

fileToInputStream

public static java.io.InputStream fileToInputStream(java.lang.String fileName)
Parameters:
file - holds the file to converted to an inputstream.
Returns:
an inputStream that is constructed from the file.

fileToOnputStream

public static java.io.OutputStream fileToOnputStream(java.lang.String fileName)
Parameters:
file - holds the file to converted to an Outputstream.
Returns:
an OutputStream that is constructed from the file.

log

public static boolean log(java.lang.String logMessage,
                          java.lang.String logFileName)

print

public static boolean print(java.lang.String s,
                            java.io.OutputStream outputStream)
Print out a string message.

Parameters:
s - holds the string message to print.
outputStream - holds the output stream to print the message to
Returns:
whether the print is successful.

println

public static boolean println()
Print out a line feed "\n".

Returns:
whether the print is successful.

println

public static boolean println(java.lang.String s)
Print out a string message with an extra line feed "\n".

Parameters:
s - holds the string message to print.
Returns:
whether the print is successful.

print

public static boolean print(java.lang.String s)
Print out a string message.

Parameters:
s - holds the string message to print.
Returns:
whether the print is successful.

exceptionStackToString

public static java.lang.String exceptionStackToString(java.lang.Exception e)
Convert printStackTrace() to a String.

Parameters:
e - holds the exception to be redirected.
Returns:
the converted string.

cleanUpDisk

public static boolean cleanUpDisk()
clean up disks, mainly including deleting temp files.

Returns:
whether the clean up is successful or not.

existsFileOrDir

public static boolean existsFileOrDir(java.lang.String path)
check if a file or directory exists.

Parameters:
path - holds the the file or dir path to check.
Returns:
whether a file or directory exists.

getLastWriteTime

public static long getLastWriteTime(java.lang.String file)
get the last write time of the file.

Parameters:
file - holds the name of the file to check.
Returns:
the last write time of the file in millisecond.

isDir

public static boolean isDir(java.lang.String path)
test whether a path is a directory or not.

Parameters:
path - holds the file or dir path.
Returns:
true if the path is not a directory, false otherwise.

createFile

public static boolean createFile(java.lang.String file)
Create a file.

Parameters:
file - holds the name of the file to create.
Returns:
whether the file is created successfully.

deleteFile

public static boolean deleteFile(java.lang.String file)
delete a file.

Parameters:
file - holds the name of the file to delete.
Returns:
whether the file is deleted successfully.

copyFile

public static boolean copyFile(java.lang.String from,
                               java.lang.String to)
copy a file.

Parameters:
from - holds the file to copy from.
to - holds the file to copy to.
Returns:
whether the file is copied successfully.

moveFile

public static boolean moveFile(java.lang.String from,
                               java.lang.String to)
move a file.

Parameters:
from - holds the file to move from.
to - holds the file to move to.
Returns:
whether the file is moved successfully.

makeDir

public static boolean makeDir(java.lang.String dir)
Make a directory.

Parameters:
dir - holds the directory path
Returns:
whether the directory is made successfully.

deleteDir

public static boolean deleteDir(java.lang.String dir)
Delete a directory.

Parameters:
dir - holds the directory path
Returns:
whether the directory is deleted successfully.

getCurrentDir

public static java.lang.String getCurrentDir()
Get the current directory. Note the path is denoted by "/". If there any error, null will be returned.

Returns:
the current directory in a path ending with "/"

getParentDir

public static java.lang.String getParentDir()
Get the parent directory. Note the path is denoted by "/". If there any error, null will be returned.

Returns:
the parent directory in a path ending with "/"

getFreeDiskSpace

public static double getFreeDiskSpace(java.lang.String path)
                               throws java.io.IOException
get free disk space.

Parameters:
path - holds the path to get free space for, (including the colon on the windows system.)
Returns:
the amount of free drive space on the drive
Throws:
java.io.IOException - if an error occurs

main

public static void main(java.lang.String[] args)
main for test purposes.

Parameters:
argv - command line arguments.