KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > io > StreamFlusher


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.util.io;
25
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.PrintStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.File JavaDoc;
32
33
34
35 public class StreamFlusher extends Thread JavaDoc {
36     
37     private InputStream JavaDoc _input=null;
38     private OutputStream JavaDoc _output=null;
39     private String JavaDoc _logFile=null;
40
41     
42     public StreamFlusher(InputStream JavaDoc input, OutputStream JavaDoc output) {
43         this(input, output, null);
44     }
45
46     
47     public StreamFlusher(InputStream JavaDoc input, OutputStream JavaDoc output, String JavaDoc logFile) {
48         this._input=input;
49         this._output=output;
50         this._logFile=logFile;
51     }
52     
53     public void run() {
54         
55         // check for null stream
56
if (_input == null) return;
57         
58         PrintStream JavaDoc printStream=null;
59         
60         // If applicable, write to a log file
61
if (_logFile != null) {
62             try {
63                 if(createFileStructure(_logFile)) {
64                     // reset streams to logfile
65
printStream = new PrintStream JavaDoc(new FileOutputStream JavaDoc(_logFile, true), true);
66                 } else {
67                     // could not write to log for some reason
68
_logFile=null;
69                 }
70             } catch (IOException JavaDoc ie) {
71                 ie.printStackTrace();
72                 _logFile=null;
73             }
74         }
75         
76         // transfer bytes from input to output stream
77
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                     // also send to log, if it exists
86
if (_logFile != null) {
87                         printStream.write(buffer, 0, byteCnt);
88                         printStream.flush();
89                     }
90                 }
91                 yield();
92             }
93         } catch (IOException JavaDoc e) {
94             // shouldn't matter
95
}
96     }
97
98     
99     /**
100      * createFileStructure - This method validates that that the file can be written to. It the
101      * if the parent directory structure does not exist, it will be created
102      *
103      * @param logFile - fully qualified path of the logfile
104      */

105     protected boolean createFileStructure(String JavaDoc logFile) {
106         boolean bRet=false;
107         File JavaDoc outputFile=new File JavaDoc(logFile);
108         
109         try {
110             // Verify that we can write to the output file
111
File JavaDoc parentFile = new File JavaDoc(outputFile.getParent());
112             // To take care of non-existent log directories
113
if ( !parentFile.exists() ) {
114                 // Trying to create non-existent parent directories
115
parentFile.mkdirs();
116             }
117             // create the file if it doesn't exist
118
if (!outputFile.exists()) {
119                 outputFile.createNewFile();
120             }
121             if (outputFile.canWrite()) {
122                 // everything is okay to logfile
123
bRet=true;
124             }
125         } catch (IOException JavaDoc e) {
126             // will only see on verbose more, so okay
127
e.printStackTrace();
128         }
129
130         return bRet;
131     }
132 }
Popular Tags