FileDocCategorySizeDatePackage
StreamFlusher.javaAPI DocGlassfish v2 API5272Fri May 04 22:32:14 BST 2007com.sun.enterprise.util.io

StreamFlusher

public class StreamFlusher extends Thread

Fields Summary
private InputStream
_input
private OutputStream
_output
private String
_logFile
Constructors Summary
public StreamFlusher(InputStream input, OutputStream output)


    
         
        this(input, output, null);
    
public StreamFlusher(InputStream input, OutputStream output, String logFile)

        this._input=input;
        this._output=output;
        this._logFile=logFile;
    
Methods Summary
protected booleancreateFileStructure(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

param
logFile - fully qualified path of the logfile

        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 voidrun()

        
        // 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
        }