O2Plib.io
Class FileIO

java.lang.Object
  extended by O2Plib.io.FileIO

public class FileIO
extends java.lang.Object

Class FileIO is used to read file I/O from local disk or URLs for the Open2Dprot projects. [TODO] It could be expanded to access JDBC RDBMS and client-server I/O. If a method fails, the error flag errFlag is set true and the errMsg contains the error message. This makes the code GUI independent. This code was derived and refactored from MAExplorer, Flicker, early Seg2Dgel Java code, and GELLAB-II C code.

This work was derived from MAExplorer under the Mozilla 1.1 Open Source Public License by Peter Lemkin of the National Cancer Institute, an agency of the United States Government subject to the limitations noted in the accompanying LEGAL file. See licence info on http://maexplorer.sourceforge.org/

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 Open2Dprot Mozilla and Legal files are available on http://open2dprot.sourceforge.net/

Version:
$Date: 2005/10/21 16:26:14 $ $Revision: 1.17 $
Author:
P. Lemkin (LECB/NCI), G. Thornwall (SAIC), Frederick, MD
See Also:
Open2Dprot Home

Field Summary
 java.lang.String batchDir
          Batch temporary data files used in exec'ing pipeline modules directory {projDir}/batch/
 java.lang.String cacheDir
          Cache database directory {projDir}/cache/
static int errCode
          error code is 0 if succeed
static java.lang.String fioErrStr
          error code string if an error occurred, else null
static int IO_EXCEPTION
           
static int IO_FILE_NOT_FOUND
           
static int IO_FILE_NOT_READ_ENABLED
           
static int IO_FILE_WRITE_ERROR
           
static int IO_FORMAT_FULL
          File format for full tab-delimited format that includes data BEFORE the spot list and summary data AFTER the spot list
static int IO_FORMAT_GELLAB
          File format for old GELLAB-II formats
static int IO_FORMAT_TAB_DELIM
          File format for tab-delimited format for use with spreadsheets such as Excel.
static int IO_FORMAT_XML
          File format for XML for use with the Open2Dprot project
static int IO_OK
          I/O error codes
static int IO_SECURITY_VIOLATION
           
 java.lang.String ppxDir
          Original sample images directory {projDir}/ppx/
static boolean printErrMsgFlag
          To display error messages.
 java.lang.String projDir
          Project directory that is either userDir, specified by -projDir:{path}, or extracted from the input file path it the image is in a "/ppx/" subdirectory.
 java.lang.String rdbmsDir
          RDBMS database directory {projDir}/rdbms/
 boolean subDirsOKflag
          Flag indicating that subdirectories were ok or created ok.
 java.lang.String tmpDir
          Tmp images directory {projDir}/tmp/
static java.lang.String VERSION
          Version for this module
 java.lang.String xmlDir
          Directory {projDir}/xml/ use to accession, landmark, SSF, SPF, CSD and other database files.
 
Constructor Summary
FileIO()
          FileIO() - constructor.
FileIO(boolean printErrorMsgFlag)
          FileIO() - constructor
FileIO(java.lang.String projectDir)
          FileIO() - constructor.
FileIO(java.lang.String projectDir, boolean allowMkdirsFlag)
          FileIO() - constructor.
 
Method Summary
static boolean atomicFileCopy(java.lang.String fromFile, java.lang.String toFile)
          atomicFileCopy() - do an atomic file move where we delete the toFile PRIOR to copying the fromFile to the toFile.
static 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
static boolean copyFile(java.lang.String srcName, java.lang.String dstName, java.lang.String optUpdateMsg, int optEstInputFileLth, boolean useAppendMsgFlag)
          copyFile() - binary copy of one file or URL to a local file
static boolean deleteLocalFile(java.lang.String fileName)
          deleteLocalFile() - delete local file.
static java.lang.String discoverFileExtension(java.lang.String dir, java.lang.String fileName, java.lang.String[] legalExtens)
          discoverFileExtension() - discover file extension by trying to lookup the file discoverFileExtension(dir,fileName,legalExtens)
static java.lang.String getDirPathByDirBrowser(java.lang.String defaultDir, java.lang.String sPrompt)
          getDirPathByDirBrowser() - set the output path by directory browser
static java.lang.String[] getFilesInDir(java.lang.String dir, java.lang.String ext)
          getFilesInDir() - get list of files in directory with specified file extension
static java.lang.String getFullPathPixFileName(java.lang.String ppxDir, java.lang.String pixFileName, java.lang.String[] legalExtens)
          getFullPathPixFileName() - get the full path pix file name
 java.lang.String getProjectDirPaths()
          getProjectDirPaths() - get printable string of project directory (batch/ cache/ ppx/ rdbms/ tmp/ xml/).
 boolean makeProjectDirs(java.lang.String projectDir, boolean allowMkdirsFlag)
          makeProjectDirs() - make a project directory and sub directories and create directory and sub directories: batch/ cache/ ppx/ rdbms/ tmp/ xml/.
static java.io.File openReadFile(java.lang.String fileName, java.lang.String directory)
          openReadFile() - Try to open an input File for reading.
static boolean out_it(java.lang.String txt, java.io.FileWriter ochan)
          out_it() - Output a String to the output channel 'oChan'.
static byte[] readBytesFromURL(java.lang.String srcName, java.lang.String optUpdateMsg)
          readBytesFromURL() - read binary data from URL
static java.lang.String readTextFile(java.lang.String fileName)
          readTextFile() - Will read file from disk & returns as String
static int resizeLAXfileData(java.lang.String applicationName, java.lang.String userDir)
          resizeLAXfileData() - resize {applicationName}.lax file data for applicationName.
 void setProjectDirs(java.lang.String projectDir)
          setProjectDirs() - setup the project sub-directories from the project directory (batch/ cache/ ppx/ rdbms/ tmp/ xml/).
static boolean testLegalFileExtension(java.lang.String fileNamePath, java.lang.String[] legalExtens)
          testLegalFileExtension() - test if file has a legal file extension by checking the list legalExtens[]
static boolean updateFilesFromServer(java.lang.String serverURL, java.lang.String userDir, java.lang.String[] fileList, java.lang.String fileTypeMsg)
          updateFilesFromServer() - update a list of files from the current server into the program install area.
static boolean writeStrToFileBuffer(java.lang.String txt, java.io.FileWriter fd)
          writeStrToFileBuffer() - Output a String to the output channel 'fd'.
static boolean writeTextFile(java.lang.String fileName, java.lang.String data)
          writeTextFile() - write string data to a local disk file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

public static final java.lang.String VERSION
Version for this module

See Also:
Constant Field Values

IO_FORMAT_FULL

public static final int IO_FORMAT_FULL
File format for full tab-delimited format that includes data BEFORE the spot list and summary data AFTER the spot list

See Also:
Constant Field Values

IO_FORMAT_GELLAB

public static final int IO_FORMAT_GELLAB
File format for old GELLAB-II formats

See Also:
Constant Field Values

IO_FORMAT_TAB_DELIM

public static final int IO_FORMAT_TAB_DELIM
File format for tab-delimited format for use with spreadsheets such as Excel. This JUST includes the spot list and no preface or epilog data (see IO_FORMAT_FULL for the version with all data)

See Also:
Constant Field Values

IO_FORMAT_XML

public static final int IO_FORMAT_XML
File format for XML for use with the Open2Dprot project

See Also:
Constant Field Values

IO_OK

public static final int IO_OK
I/O error codes

See Also:
Constant Field Values

IO_FILE_NOT_READ_ENABLED

public static final int IO_FILE_NOT_READ_ENABLED
See Also:
Constant Field Values

IO_FILE_WRITE_ERROR

public static final int IO_FILE_WRITE_ERROR
See Also:
Constant Field Values

IO_FILE_NOT_FOUND

public static final int IO_FILE_NOT_FOUND
See Also:
Constant Field Values

IO_SECURITY_VIOLATION

public static final int IO_SECURITY_VIOLATION
See Also:
Constant Field Values

IO_EXCEPTION

public static final int IO_EXCEPTION
See Also:
Constant Field Values

errCode

public static int errCode
error code is 0 if succeed


fioErrStr

public static java.lang.String fioErrStr
error code string if an error occurred, else null


printErrMsgFlag

public static boolean printErrMsgFlag
To display error messages.


projDir

public java.lang.String projDir
Project directory that is either userDir, specified by -projDir:{path}, or extracted from the input file path it the image is in a "/ppx/" subdirectory.


batchDir

public java.lang.String batchDir
Batch temporary data files used in exec'ing pipeline modules directory {projDir}/batch/


cacheDir

public java.lang.String cacheDir
Cache database directory {projDir}/cache/


ppxDir

public java.lang.String ppxDir
Original sample images directory {projDir}/ppx/


rdbmsDir

public java.lang.String rdbmsDir
RDBMS database directory {projDir}/rdbms/


tmpDir

public java.lang.String tmpDir
Tmp images directory {projDir}/tmp/


xmlDir

public java.lang.String xmlDir
Directory {projDir}/xml/ use to accession, landmark, SSF, SPF, CSD and other database files.


subDirsOKflag

public boolean subDirsOKflag
Flag indicating that subdirectories were ok or created ok.

Constructor Detail

FileIO

public FileIO()
FileIO() - constructor. The printErrMsgFlag is set to display error messages, else just return from methods. In either case, the (errCode,fioErrStr) is set.


FileIO

public FileIO(java.lang.String projectDir)
FileIO() - constructor. The printErrMsgFlag is set to display error messages, else just return from methods. In either case, the (errCode,fioErrStr) is set. Also check and create the project subdirectories if needed. The subDirsOKflag is set true if they are ok or created ok.

Parameters:
projectDir - to check and possibly create subproject directories

FileIO

public FileIO(java.lang.String projectDir,
              boolean allowMkdirsFlag)
FileIO() - constructor. The printErrMsgFlag is set to display error messages, else just return from methods. In either case, the (errCode,fioErrStr) is set. Also check and create the project subdirectories. The subDirsOKflag is set true if they are ok or created ok.

Parameters:
projectDir - to check and possibly create subproject directories
allowMkdirsFlag - to allow mkdirs() if directories not found

FileIO

public FileIO(boolean printErrorMsgFlag)
FileIO() - constructor

Parameters:
printErrorMsgFlag - to display error messages, else just return. In either case, the (errCode,fioErrStr) is set.
Method Detail

out_it

public static boolean out_it(java.lang.String txt,
                             java.io.FileWriter ochan)
out_it() - Output a String to the output channel 'oChan'.

Parameters:
txt - is the text to output
ochan - is the FileWriter channel to use for OUTPUT
Returns:
true if succeed

writeStrToFileBuffer

public static boolean writeStrToFileBuffer(java.lang.String txt,
                                           java.io.FileWriter fd)
writeStrToFileBuffer() - Output a String to the output channel 'fd'.

Parameters:
txt - is the text to output
fd - is the FileWriter channel to use for OUTPUT
Returns:
true if succeed

readTextFile

public static java.lang.String readTextFile(java.lang.String fileName)
readTextFile() - Will read file from disk & returns as String

Returns:
null if failed.

writeTextFile

public static boolean writeTextFile(java.lang.String fileName,
                                    java.lang.String data)
writeTextFile() - 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.

getFilesInDir

public static java.lang.String[] getFilesInDir(java.lang.String dir,
                                               java.lang.String ext)
getFilesInDir() - get list of files in directory with specified file extension

Parameters:
dir - directory to read
ext - is the file exteneions (e.g. ".tif") to match if not null, else accept all files
Returns:
list of files

deleteLocalFile

public static boolean deleteLocalFile(java.lang.String fileName)
deleteLocalFile() - delete local file.

Returns:
false if failed..

copyFile

public static 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 showMsg(). 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 input file if known else 0. Used in progress bar.
Returns:
true if succeed.

copyFile

public static boolean copyFile(java.lang.String srcName,
                               java.lang.String dstName,
                               java.lang.String optUpdateMsg,
                               int optEstInputFileLth,
                               boolean useAppendMsgFlag)
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 showMsg(). 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 input file if known else 0. Used in progress bar.
useAppendMsgFlag - to add to append message window.
Returns:
true if succeed.

atomicFileCopy

public static final boolean atomicFileCopy(java.lang.String fromFile,
                                           java.lang.String toFile)
atomicFileCopy() - do an atomic file move where we delete the toFile PRIOR to copying the fromFile to the toFile.

Parameters:
fromFile - is the source file
toFile - is the destination file
Returns:
true if succeed, false if copy failed.

readBytesFromURL

public static 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 showMsg(). 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.

discoverFileExtension

public static java.lang.String discoverFileExtension(java.lang.String dir,
                                                     java.lang.String fileName,
                                                     java.lang.String[] legalExtens)
discoverFileExtension() - discover file extension by trying to lookup the file discoverFileExtension(dir,fileName,legalExtens)

Parameters:
dir - is diretory to test
fileName - is file name without "." file extension.
legalExtens - is list of possible legal file names
Returns:
file extension (e.g., ".gif") if found, else null

testLegalFileExtension

public static boolean testLegalFileExtension(java.lang.String fileNamePath,
                                             java.lang.String[] legalExtens)
testLegalFileExtension() - test if file has a legal file extension by checking the list legalExtens[]

Parameters:
fileNamePath - is file name with or without a file extension.
legalExtens - is list of possible legal file names
Returns:
true if the file extension is in the legalExtens list.

getFullPathPixFileName

public static java.lang.String getFullPathPixFileName(java.lang.String ppxDir,
                                                      java.lang.String pixFileName,
                                                      java.lang.String[] legalExtens)
getFullPathPixFileName() - get the full path pix file name

Parameters:
ppxDir - - directory to search
pixFileName - - this may be base name or full path or partial path
legalExtens - is list of possible legal file names
Returns:
full path pix file name

getDirPathByDirBrowser

public static java.lang.String getDirPathByDirBrowser(java.lang.String defaultDir,
                                                      java.lang.String sPrompt)
getDirPathByDirBrowser() - set the output path by directory browser

Parameters:
defaultDir - if not null
sPrompt - to use if not null
Returns:
directory if not null.

openReadFile

public static java.io.File openReadFile(java.lang.String fileName,
                                        java.lang.String directory)
openReadFile() - Try to open an input File for reading. It must exist and be readable.

Returns:
File descriptir if it succeeds, else return null.

updateFilesFromServer

public static boolean updateFilesFromServer(java.lang.String serverURL,
                                            java.lang.String userDir,
                                            java.lang.String[] fileList,
                                            java.lang.String fileTypeMsg)
updateFilesFromServer() - update a list of files from the current server into the program install area.
 [1] Setup a list of files to download
 [2] For each file in docFile[0:nFiles],
    [2.1] Backup the old file with a .bkup extension
    [2.2] Open the url: from serverURL and read the file from 
          the Web into local file docList[i]+".tmp"
    [2.3] Move the docList[i]+".tmp" file into docList[i] in the
          program directory

Parameters:
serverURL - is the URL of the server where the files reside
userDir - is the user directory where the fileList is found
fileList - - contains the files on the server that are in the save subdirectory order as in the install directory
fileTypeMsg - is the type of file being downloaded, e.g., "documentation", "demo", "jar", etc.
Returns:
true if succeed

resizeLAXfileData

public static int resizeLAXfileData(java.lang.String applicationName,
                                    java.lang.String userDir)
resizeLAXfileData() - resize {applicationName}.lax file data for applicationName. This file is read by the InstallAnywhere runtime when you start the application and is used to set the initial heap and stack process size. The LAX file (e.g., C:/Program Files/{applicationName}/{applicationName}.lax) specifies the startup sizes for both the heap and stack. It determines the current size, asks the user for a new size then creates a new file if it succeeds in the Q and A. The memory size specified must be > 30Mbytes and less than the swap space. However, it is not clear how to get access to that value across all systems.

Parameters:
applicationName - is the name of the application
userDir - is the user directory where the fileList is found
Returns:
-1 if fail, else the new memory size

setProjectDirs

public void setProjectDirs(java.lang.String projectDir)
setProjectDirs() - setup the project sub-directories from the project directory (batch/ cache/ ppx/ rdbms/ tmp/ xml/).

Parameters:
projectDir - target project directory

makeProjectDirs

public boolean makeProjectDirs(java.lang.String projectDir,
                               boolean allowMkdirsFlag)
makeProjectDirs() - make a project directory and sub directories and create directory and sub directories: batch/ cache/ ppx/ rdbms/ tmp/ xml/. If a directory is not found, then if allowMkdirsFlag is true, try to mkdirs() that directory.

Parameters:
projectDir - to check and possibly create
allowMkdirsFlag - to allow mkdirs() if directories not found
Returns:
true if succeed, false if i/o error or a directory was NOT found and allowMkdirsFlag was NOT set.

getProjectDirPaths

public java.lang.String getProjectDirPaths()
getProjectDirPaths() - get printable string of project directory (batch/ cache/ ppx/ rdbms/ tmp/ xml/).

Returns:
paths of project directories