001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.stream; 017 018import java.io.BufferedReader; 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.InputStreamReader; 022import java.io.PrintWriter; 023 024import org.codehaus.plexus.util.IOUtil; 025import org.codehaus.plexus.util.cli.AbstractStreamHandler; 026import org.codehaus.plexus.util.cli.StreamConsumer; 027 028/** 029 * Copied from Plexus in order to add constructors that allow you to provide a BufferedReader directly, or to provide an explicit character encoding. 030 * 031 * This class would be entirely unnecessary if the Plexus version had a constructor that allowed you to provide a BufferedReader 032 */ 033public class StreamPumper extends AbstractStreamHandler { 034 035 private final BufferedReader in; 036 private final StreamConsumer consumer; 037 private final PrintWriter out; 038 private volatile Exception exception = null; 039 private static final int SIZE = 1024; 040 041 public StreamPumper(InputStream in) { 042 this(in, (StreamConsumer) null); 043 } 044 045 public StreamPumper(InputStream in, StreamConsumer consumer) { 046 this(in, (PrintWriter) null, consumer); 047 } 048 049 public StreamPumper(InputStream in, PrintWriter writer) { 050 this(in, writer, null); 051 } 052 053 public StreamPumper(InputStream in, PrintWriter writer, StreamConsumer consumer) { 054 this(new BufferedReader(new InputStreamReader(in), SIZE), writer, consumer); 055 } 056 057 public StreamPumper(InputStream in, String encoding, StreamConsumer consumer) throws IOException { 058 this(new BufferedReader(new InputStreamReader(in, encoding), SIZE), null, consumer); 059 } 060 061 public StreamPumper(BufferedReader in, PrintWriter writer, StreamConsumer consumer) { 062 this.in = in; 063 this.out = writer; 064 this.consumer = consumer; 065 } 066 067 @Override 068 public void run() { 069 try { 070 for (String line = in.readLine(); line != null; line = in.readLine()) { 071 try { 072 if (exception == null) { 073 consumeLine(line); 074 } 075 } catch (Exception t) { 076 exception = t; 077 } 078 079 if (out != null) { 080 out.println(line); 081 out.flush(); 082 } 083 } 084 } catch (IOException e) { 085 exception = e; 086 } finally { 087 IOUtil.close(in); 088 synchronized (this) { 089 setDone(); 090 this.notifyAll(); 091 } 092 } 093 } 094 095 public void flush() { 096 if (out != null) { 097 out.flush(); 098 } 099 } 100 101 public void close() { 102 IOUtil.close(out); 103 } 104 105 public Exception getException() { 106 return exception; 107 } 108 109 private void consumeLine(String line) { 110 if (consumer != null && !isDisabled()) { 111 consumer.consumeLine(line); 112 } 113 } 114}