Class FileIO

java.lang.Object
  |
  +--FileIO

class FileIO
extends java.lang.Object

This file I/O class includes methods to do file I/O to a local disk or URL. The disk (standalone file:// mode) or through a URL (standalone or applet http://) connects to a Web server. If you are doing a URL I/O is used, then it uses JavaCGIbridge to read files. If you are using password protection or writing a file to a URL, then it needs a cooperating CGI process running the the target Web server.

This work was produced by Peter Lemkin of the National Cancer Institute, an agency of the United States Government. As a work of the United States Government there is no associated copyright. It is offered as open source software under the Mozilla Public License (version 1.1) subject to the limitations noted in the accompanying LEGAL file. This notice must be included with the code. The MAExplorer Mozilla and Legal files are available on http://maexplorer.sourceforge.net/.

Version:
$Date: 2003/07/21 19:40:15 $ $Revision: 1.13 $
Author:
P. Lemkin (NCI), G. Thornwall (SAIC), NCI-Frederick, Frederick, MD
See Also:
MAExplorer Home

Field Summary
private  char[] logDataBuf
          standalone log file data buff
private  int logDataBufSize
          size of standalone log file data buff
private  java.io.File logFile
          standalone log file descriptor
private  java.lang.String logFileName
          standalone log file name
(package private)  java.io.FileWriter logFileWriter
          standalone log file writer
private  MAExplorer mae
          link to global MAExplorer instance
 
Constructor Summary
(package private) FileIO(MAExplorer mae)
          FileIO() - constructor to create fio instance and to close the log file.
 
Method Summary
private  java.lang.String cacheRead(java.lang.String fullFilePath)
          cacheRead() - read data file from Cache file it it exists.
private  boolean cacheWrite(java.lang.String fullFilePath, java.lang.String data)
          cacheWrite() - Write string into file in local cache.
(package private)  void closeLogFile()
          closeLogFile() - close log file if it is open, but always reset the log state
 boolean copyFile(java.lang.String srcName, java.lang.String dstName, java.lang.String optUpdateMsg, int optEstInputFileLth)
          copyFile() - binary copy of one file or URL to a local file
private  java.lang.String createCacheFileName(java.lang.String fullFilePath)
          createCacheFileName() - create cache file name.
 boolean deleteLocalFile(java.lang.String fileName)
          deleteLocalFile() - delete local file.
 boolean deleteRecursive(java.io.File dirNameToDelete)
           
 boolean extractZipFiles(java.lang.String zipFileName, java.lang.String rootPath)
          extractZipFiles() - extract files from zip file.
(package private)  void logMsg(java.lang.String msg)
          logMsg() - write msg to opened log file if stand-alone else print to Java console.
(package private)  void logMsg(java.lang.String msg, java.lang.String logFileName, boolean newFileFlag, boolean appendFlag)
          logMsg() - write msg to log file.
(package private)  void logMsgln(java.lang.String msg)
          logMsgln() - write msg+"\n" to opened log file if stand-alone else if applet, just do System.out.println() call to java console.
 byte[] readBytesFromURL(java.lang.String srcName, java.lang.String optUpdateMsg)
          readBytesFromURL() - read binary data from URL
(package private)  java.lang.String readData(java.lang.String fileName, java.lang.String msg)
          readData() - read data from URL or local file depending on prefix.
(package private)  java.lang.String readFileFromDisk(java.lang.String fileName)
          readFileFromDisk() - read file from disk and return entire file as String
(package private)  java.lang.String readFileFromUrl(java.lang.String URLaddress, boolean sendAuthReqFlag)
          readFileFromUrl() - read data from http:// URL, using JavaCGIBridge.
(package private)  java.lang.String readZipFileFromDisk(java.lang.String fileName, java.lang.String entryName)
          readZipFileFromDisk() - [TODO] read Zip file from disk and return it as a String.
(package private)  boolean writeData(java.lang.String fileName, java.lang.String msg, java.lang.String data)
          writeData() - write data to a URL or local file depending on file name prefix.
(package private)  boolean writeFileFromUrl(java.lang.String URLaddress, java.lang.String fileName, java.lang.String data)
          writeFileFromUrl() - write string data to http URL, using JavaCGIBridge.
(package private)  boolean writeFileToDisk(java.lang.String fileName, java.lang.String data)
          writeFileToDisk() - write string data to a local disk file.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

mae

private MAExplorer mae
link to global MAExplorer instance

logFileName

private java.lang.String logFileName
standalone log file name

logFile

private java.io.File logFile
standalone log file descriptor

logFileWriter

java.io.FileWriter logFileWriter
standalone log file writer

logDataBuf

private char[] logDataBuf
standalone log file data buff

logDataBufSize

private int logDataBufSize
size of standalone log file data buff
Constructor Detail

FileIO

FileIO(MAExplorer mae)
FileIO() - constructor to create fio instance and to close the log file.
Parameters:
mae - is instance of MAExplorer
See Also:
closeLogFile()
Method Detail

readData

java.lang.String readData(java.lang.String fileName,
                          java.lang.String msg)
readData() - read data from URL or local file depending on prefix.
Parameters:
fileName - is the full path filename to read the data. If it starts with "http://" then it should be a URL.
msg - to display while reading the file
Returns:
string data for entire file if succeed, else null if fail.
See Also:
Util.showMsg(java.lang.String), cacheRead(java.lang.String), cacheWrite(java.lang.String, java.lang.String), readFileFromDisk(java.lang.String), readFileFromUrl(java.lang.String, boolean)

writeData

boolean writeData(java.lang.String fileName,
                  java.lang.String msg,
                  java.lang.String data)
writeData() - write data to a URL or local file depending on file name prefix.
Parameters:
fileName - is the full path filename to write the data
msg - to display while writing the file
data - is the string to write to the file.
Returns:
string data for entire file if succeed, else null if fail.
See Also:
Util.showMsg(java.lang.String), writeFileToDisk(java.lang.String, java.lang.String), writeFileFromUrl(java.lang.String, java.lang.String, java.lang.String)

readFileFromDisk

java.lang.String readFileFromDisk(java.lang.String fileName)
readFileFromDisk() - read file from disk and return entire file as String
Parameters:
fileName - is the full path filename to read the data
Returns:
string data for entire file if succeed, else null if fail.
See Also:
logMsgln(java.lang.String), readZipFileFromDisk(java.lang.String, java.lang.String)

readZipFileFromDisk

java.lang.String readZipFileFromDisk(java.lang.String fileName,
                                     java.lang.String entryName)
readZipFileFromDisk() - [TODO] read Zip file from disk and return it as a String. [NOTE] Could use this method for unpacking a RDBMS data sets from an array Web server...
Parameters:
fileName - is the full path zip filename to read the data
entryName - is the particular entry to read from the zip file
Returns:
string data for entire file if succeed, else null if fail.
See Also:
logMsgln(java.lang.String)

extractZipFiles

public boolean extractZipFiles(java.lang.String zipFileName,
                               java.lang.String rootPath)
extractZipFiles() - extract files from zip file. Create the dirs if needed.
Parameters:
zipfileName - Name of zip file including full path
rootPath - Directory to extract the zip file

readFileFromUrl

java.lang.String readFileFromUrl(java.lang.String URLaddress,
                                 boolean sendAuthReqFlag)
readFileFromUrl() - read data from http:// URL, using JavaCGIBridge. It returns string if successful. If it fails with a 401 UNAUTHORIZED error, it trys again using the mae.(userName,userPasswd) if it exists. If it does not exist, it pops up the dialog box to get it. If it tries the password and it fails, then fails returning null.
[BUG] security does not always work correctly.
Parameters:
URLaddress - is the full URLaddress to read the data
sendAuthReqFlag - is the authorization required (optional)
Returns:
string data for entire file if succeed, else null if fail.
See Also:
JavaCGIBridge, JavaCGIBridge.getRawCGIData(java.net.URL), logMsgln(java.lang.String)

writeFileToDisk

boolean writeFileToDisk(java.lang.String fileName,
                        java.lang.String data)
writeFileToDisk() - write string data to a local disk file.
Parameters:
fileName - is the full path filename to write the data
data - is the string to write to the file.
Returns:
string data for entire file if succeed, else null if fail.
See Also:
logMsgln(java.lang.String)

writeFileFromUrl

boolean writeFileFromUrl(java.lang.String URLaddress,
                         java.lang.String fileName,
                         java.lang.String data)
writeFileFromUrl() - write string data to http URL, using JavaCGIBridge.
Parameters:
URLaddress - is the full CGI URL to write the data
fileName - is the filename on the server
data - is the string to write to the file.
Returns:
string data for entire file if succeed, else null if fail.
See Also:
JavaCGIBridge, JavaCGIBridge.setThreadJavaCGIBridgeTimeOut(int), JavaCGIBridge.addFormValue(java.util.Hashtable, java.lang.String, java.lang.String), JavaCGIBridge.getRawCGIData(java.net.URL), logMsgln(java.lang.String)

createCacheFileName

private java.lang.String createCacheFileName(java.lang.String fullFilePath)
createCacheFileName() - create cache file name.
Parameters:
fullFilePath - is the full path filename to read the data
Returns:
string data for entire file if succeed, else null if fail.

cacheRead

private java.lang.String cacheRead(java.lang.String fullFilePath)
cacheRead() - read data file from Cache file it it exists. Strip off file name from end of fullPath.
Parameters:
fullFilePath - is the full path filename to read the data
Returns:
string data for entire file if succeed, else null if fail.
See Also:
createCacheFileName(java.lang.String), readFileFromDisk(java.lang.String)

cacheWrite

private boolean cacheWrite(java.lang.String fullFilePath,
                           java.lang.String data)
cacheWrite() - Write string into file in local cache.
Parameters:
fullFilePath - is the full path filename to write the data
data - is the data to write to the file
Returns:
true if succeed
See Also:
createCacheFileName(java.lang.String), writeFileToDisk(java.lang.String, java.lang.String)

closeLogFile

void closeLogFile()
closeLogFile() - close log file if it is open, but always reset the log state

logMsg

void logMsg(java.lang.String msg)
logMsg() - write msg to opened log file if stand-alone else print to Java console. Do NOT append a "\n". If applet, just do System.out.print() call to console. If it is not opened, then a NO-OP.

logMsgln

void logMsgln(java.lang.String msg)
logMsgln() - write msg+"\n" to opened log file if stand-alone else if applet, just do System.out.println() call to java console. If it is not opened, then a NO-OP.
Parameters:
msg - to write to log file

logMsg

void logMsg(java.lang.String msg,
            java.lang.String logFileName,
            boolean newFileFlag,
            boolean appendFlag)
logMsg() - write msg to log file. The \n must be added by the caller. First time over write, then append.
Parameters:
msg - to write, add \n if want it in the file
logFileName - is name of log file
newFileFlag - to force new file
appendFlag - if want to append, else new file

copyFile

public boolean copyFile(java.lang.String srcName,
                        java.lang.String dstName,
                        java.lang.String optUpdateMsg,
                        int optEstInputFileLth)
copyFile() - binary copy of one file or URL to a local file
Parameters:
srcName - is either a full path local file name or a http:// prefixed URL string of the source file.
dstName - is the full path of the local destination file name
optUpdateMsg - (opt) will display message in showMsg() and increasing ... in showMsg2(). One '.' for every 10K bytes read. This only is used when reading a URL. Set to null if not used.
optEstInputFileLth - is the estimate size of the in;ut file if known else 0. Used in progress bar.
Returns:
true if succeed.

readBytesFromURL

public byte[] readBytesFromURL(java.lang.String srcName,
                               java.lang.String optUpdateMsg)
readBytesFromURL() - read binary data from URL
Parameters:
srcName - is either a full path local file name or a http:// prefixed URL string of the source file.
optUpdateMsg - (opt) will display message in showMsg() and increasing ... in showMsg2(). One '.' for every 10K bytes read. This only is used when reading a URL. Set to null if not used.
Returns:
a byte[] if succeed, else null.

deleteLocalFile

public boolean deleteLocalFile(java.lang.String fileName)
deleteLocalFile() - delete local file.
Parameters:
fileName - to be deleted on the local file system.
Returns:
false if failed.

deleteRecursive

public boolean deleteRecursive(java.io.File dirNameToDelete)