001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.harness.HandleResult
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.harness;
023:
024: import java.io.InputStream;
025: import java.io.InputStreamReader;
026: import java.io.BufferedReader;
027: import java.io.PrintWriter;
028: import java.io.IOException;
029:
030: /**
031: Class: HandleResult
032: Purpose: To capture stdout & stderr to a file
033: (PrintWriter is used for writing the output)
034: */
035:
036: public class HandleResult {
037:
038: public static void main(String[] args) throws Exception {
039: }
040:
041: public static String handleResult(int exitCode, InputStream stdout,
042: InputStream stderr, PrintWriter printWriter)
043: throws IOException {
044: return handleResult(exitCode, stdout, stderr, printWriter, null);
045: }
046:
047: public static String handleResult(int exitCode, InputStream stdout,
048: InputStream stderr, PrintWriter printWriter, String encoding)
049: throws IOException {
050: StringBuffer sb = new StringBuffer();
051:
052: // only used for debugging
053: sb.append("exitcode=");
054: sb.append(exitCode);
055:
056: if (stdout != null) {
057: // reader for stdout
058: BufferedReader outReader;
059: if (encoding != null)
060: outReader = new BufferedReader(new InputStreamReader(
061: stdout, encoding));
062: else
063: outReader = new BufferedReader(new InputStreamReader(
064: stdout));
065:
066: // Read each line and write to printWriter
067: String s = null;
068: int lines = 0;
069: while ((s = outReader.readLine()) != null) {
070: lines++;
071: if (printWriter == null)
072: System.out.println(s);
073: else
074: printWriter.println(s);
075: }
076: sb.append(",");
077: sb.append(lines);
078: outReader.close();
079: printWriter.flush();
080: }
081:
082: if (stderr != null) {
083: // reader for stderr
084: BufferedReader errReader;
085: if (encoding != null)
086: errReader = new BufferedReader(new InputStreamReader(
087: stderr, encoding));
088: else
089: errReader = new BufferedReader(new InputStreamReader(
090: stderr));
091:
092: String s = null;
093: int lines = 0;
094: while ((s = errReader.readLine()) != null) {
095: if (printWriter == null)
096: System.out.println(s);
097: else
098: printWriter.println(s);
099: }
100: errReader.close();
101: printWriter.flush();
102: }
103:
104: return sb.toString();
105: }
106: }
|