KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > harness > HandleResult


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.HandleResult
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.harness;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.BufferedReader JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31   Class: HandleResult
32   Purpose: To capture stdout & stderr to a file
33   (PrintWriter is used for writing the output)
34 */

35
36 public class HandleResult
37 {
38
39     public static void main(String JavaDoc[] args) throws Exception JavaDoc
40     {
41     }
42
43     public static String JavaDoc handleResult(int exitCode, InputStream JavaDoc stdout,
44             InputStream JavaDoc stderr, PrintWriter JavaDoc printWriter)
45             throws IOException JavaDoc
46     {
47         return handleResult(exitCode, stdout, stderr, printWriter, null);
48     }
49     
50     public static String JavaDoc handleResult(int exitCode, InputStream JavaDoc stdout,
51         InputStream JavaDoc stderr, PrintWriter JavaDoc printWriter, String JavaDoc encoding)
52         throws IOException JavaDoc
53     {
54         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
55
56         // only used for debugging
57
sb.append("exitcode=");
58         sb.append(exitCode);
59
60         if (stdout != null)
61         {
62             // reader for stdout
63
BufferedReader JavaDoc outReader;
64             if(encoding != null)
65                 outReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(stdout, encoding));
66             else
67                 outReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(stdout));
68
69             // Read each line and write to printWriter
70
String JavaDoc s = null;
71             int lines = 0;
72             while ((s = outReader.readLine()) != null)
73             {
74                 lines++;
75                 if (printWriter == null)
76                     System.out.println(s);
77                 else
78                     printWriter.println(s);
79             }
80             sb.append(",");
81             sb.append(lines);
82             outReader.close();
83             printWriter.flush();
84         }
85
86         if (stderr != null)
87         {
88             // reader for stderr
89
BufferedReader JavaDoc errReader;
90             if(encoding != null)
91                 errReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(stderr, encoding));
92             else
93                 errReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(stderr));
94
95             String JavaDoc s = null;
96             int lines = 0;
97             while ((s = errReader.readLine()) != null)
98             {
99                 if (printWriter == null)
100                     System.out.println(s);
101                 else
102                     printWriter.println(s);
103             }
104             errReader.close();
105             printWriter.flush();
106         }
107
108         return sb.toString();
109     }
110 }
111
112
113
Popular Tags