Skip to main content
summaryrefslogtreecommitdiffstats
blob: 160f3a4af9fb1bcaaface8344de08f80f3273bec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.util.io.streams;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Jeff C. Phillips
 */
public class StreamCatcher extends Thread {
   private final InputStream is;
   private final String type;
   private final Logger logger;

   public StreamCatcher(InputStream is, String type) {
      this(is, type, null);
   }

   public StreamCatcher(InputStream is, String type, Logger logger) {
      this.is = is;
      this.type = type;
      this.logger = logger;
   }

   @Override
   public void run() {
      try {
         InputStreamReader isr = new InputStreamReader(is);
         BufferedReader br = new BufferedReader(isr);
         String line = null;
         StringBuilder loggerError = new StringBuilder();

         while ((line = br.readLine()) != null) {

            if (logger == null) {
               System.out.println(type + ">" + line);
            } else {
               loggerError.append(line);
               loggerError.append("\n");
            }
         }

         if (logger != null && loggerError.length() > 0) {
            logger.log(Level.SEVERE, loggerError.toString());
         }

      } catch (IOException ioe) {
         ioe.printStackTrace();
      }
   }
}

Back to the top