StreamFlusherpublic class StreamFlusher extends Thread
Fields Summary |
---|
private InputStream | _input | private OutputStream | _output | private String | _logFile |
Methods Summary |
---|
protected boolean | createFileStructure(java.lang.String logFile)createFileStructure - This method validates that that the file can be written to. It the
if the parent directory structure does not exist, it will be created
boolean bRet=false;
File outputFile=new File(logFile);
try {
// Verify that we can write to the output file
File parentFile = new File(outputFile.getParent());
// To take care of non-existent log directories
if ( !parentFile.exists() ) {
// Trying to create non-existent parent directories
parentFile.mkdirs();
}
// create the file if it doesn't exist
if (!outputFile.exists()) {
outputFile.createNewFile();
}
if (outputFile.canWrite()) {
// everything is okay to logfile
bRet=true;
}
} catch (IOException e) {
// will only see on verbose more, so okay
e.printStackTrace();
}
return bRet;
| public void | run()
// check for null stream
if (_input == null) return;
PrintStream printStream=null;
// If applicable, write to a log file
if (_logFile != null) {
try {
if(createFileStructure(_logFile)) {
// reset streams to logfile
printStream = new PrintStream(new FileOutputStream(_logFile, true), true);
} else {
// could not write to log for some reason
_logFile=null;
}
} catch (IOException ie) {
ie.printStackTrace();
_logFile=null;
}
}
// transfer bytes from input to output stream
try {
int byteCnt=0;
byte[] buffer=new byte[4096];
while ((byteCnt=_input.read(buffer)) != -1) {
if (_output != null && byteCnt > 0) {
_output.write(buffer, 0, byteCnt);
_output.flush();
// also send to log, if it exists
if (_logFile != null) {
printStream.write(buffer, 0, byteCnt);
printStream.flush();
}
}
yield();
}
} catch (IOException e) {
// shouldn't matter
}
|
|