001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public License
010: * as published by the Free Software Foundation; either version 2 of
011: * the License, or (at your option) any later version.
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: */
021:
022: package gruntspud;
023:
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.io.IOException;
027: import java.io.InputStream;
028:
029: /**
030: * Description of the Class
031: *
032: *@author magicthize
033: *@created 26 May 2002
034: */
035: public class StreamSink extends Thread {
036: private InputStream input;
037: private boolean err;
038: private ActionListener listener;
039:
040: /**
041: * Constructor for the StreamSink object
042: *
043: *@param input Description of the Parameter
044: *@param err Description of the Parameter
045: */
046: public StreamSink(InputStream input, boolean err) {
047: this (input, err, null);
048: }
049:
050: /**
051: * Constructor for the StreamSink object
052: *
053: * @param input Description of the Parameter
054: * @param err Description of the Parameter
055: * @param listener Invoked when stream closes
056: */
057: public StreamSink(InputStream input, boolean err, ActionListener l) {
058: this .input = input;
059: this .err = err;
060: this .listener = l;
061: }
062:
063: /**
064: * DOCUMENT ME!
065: */
066: public void run() {
067: try {
068: while (true) {
069: int a = input.available();
070:
071: if (a == -1) {
072: break;
073: }
074:
075: if (a == 0) {
076: a = 1;
077:
078: }
079: byte[] buf = new byte[a];
080: int z = input.read(buf);
081:
082: if (z == -1) {
083: break;
084: }
085:
086: if (err) {
087: Constants.SYSTEM_LOG.error(new String(buf, 0, z));
088: } else {
089: Constants.SYSTEM_LOG.info(new String(buf, 0, z));
090: }
091: }
092: } catch (IOException ioe) {
093: } finally {
094: try {
095: input.close();
096: } catch (IOException ioe) {
097: ioe.printStackTrace();
098: }
099:
100: if (listener != null) {
101: listener.actionPerformed(new ActionEvent(this,
102: ActionEvent.ACTION_PERFORMED, null));
103: }
104: }
105: }
106: }
|