1 23 24 package com.sun.enterprise.util.io; 25 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.PrintStream ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.File ; 32 33 34 35 public class StreamFlusher extends Thread { 36 37 private InputStream _input=null; 38 private OutputStream _output=null; 39 private String _logFile=null; 40 41 42 public StreamFlusher(InputStream input, OutputStream output) { 43 this(input, output, null); 44 } 45 46 47 public StreamFlusher(InputStream input, OutputStream output, String logFile) { 48 this._input=input; 49 this._output=output; 50 this._logFile=logFile; 51 } 52 53 public void run() { 54 55 if (_input == null) return; 57 58 PrintStream printStream=null; 59 60 if (_logFile != null) { 62 try { 63 if(createFileStructure(_logFile)) { 64 printStream = new PrintStream (new FileOutputStream (_logFile, true), true); 66 } else { 67 _logFile=null; 69 } 70 } catch (IOException ie) { 71 ie.printStackTrace(); 72 _logFile=null; 73 } 74 } 75 76 try { 78 int byteCnt=0; 79 byte[] buffer=new byte[4096]; 80 while ((byteCnt=_input.read(buffer)) != -1) { 81 if (_output != null && byteCnt > 0) { 82 _output.write(buffer, 0, byteCnt); 83 _output.flush(); 84 85 if (_logFile != null) { 87 printStream.write(buffer, 0, byteCnt); 88 printStream.flush(); 89 } 90 } 91 yield(); 92 } 93 } catch (IOException e) { 94 } 96 } 97 98 99 105 protected boolean createFileStructure(String logFile) { 106 boolean bRet=false; 107 File outputFile=new File (logFile); 108 109 try { 110 File parentFile = new File (outputFile.getParent()); 112 if ( !parentFile.exists() ) { 114 parentFile.mkdirs(); 116 } 117 if (!outputFile.exists()) { 119 outputFile.createNewFile(); 120 } 121 if (outputFile.canWrite()) { 122 bRet=true; 124 } 125 } catch (IOException e) { 126 e.printStackTrace(); 128 } 129 130 return bRet; 131 } 132 } | Popular Tags |