001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022/*
023 *    StreamPipe.java
024 */
025package org.biojava.utils.process;
026
027import java.io.BufferedInputStream;
028import java.io.BufferedOutputStream;
029import java.io.InputStream;
030import java.io.OutputStream;
031import java.util.logging.Level;
032import java.util.logging.Logger;
033
034/**
035 * A {@linkplain java.lang.Runnable multi threaded} class 
036 * which pipes the contents of an input stream to an output stream. 
037 * @author <a href="mailto:Martin.Szugat@GMX.net">Martin Szugat</a>
038 * @version $Revision$
039 */
040public class StreamPipe implements Runnable {
041    
042    /* STATIC FIELDS */
043        
044    /**
045     * The class logger.
046     */
047    private static final Logger LOGGER = 
048        Logger.getLogger(StreamPipe.class.getName());
049    
050    /* PRIVATE FIELDS */
051
052    /**
053     * The input stream from which to read.
054     */
055    private InputStream input = null;
056    
057        /**
058         * The output stream to which to write.
059         */
060        private OutputStream output = null;
061    
062    /**
063     * A tag for logging.
064     */
065    private String tag = null;
066        
067    /* PUBLIC CONSTRUCTORS */
068    
069        /**
070     * Initializes the stream pipe.
071         * @param input the input stream from which to read. 
072     * May be <code>null</code>.
073         * @param output the output stream to which to write
074     * May be <code>null</code>.
075         * @param tag a tag which is used for logging the in- and output
076     * May be <code>null</code>.
077         */
078        public StreamPipe(InputStream input, OutputStream output, String tag) {
079        setInput(input);
080        setOutput(output);
081                this.tag = tag;
082        }
083    
084    /* PUBLIC PROPERTIES */
085
086    /**
087     * Gets the input stream
088     * @return the input from which to read. May be <code>null</code>.
089     */
090    public InputStream getInput() {
091        return input;
092    }
093
094    /**
095     * Sets the input stream
096     * @param input the input stream from which to read. May be 
097     * <code>null</code>.
098     */
099    public void setInput(InputStream input) {
100        this.input = input;
101    }
102    
103    /**
104     * Sets the output stream
105     * @param output the output stream to which to write. May be 
106     * <code>null</code>.
107     */
108    public void setOutput(OutputStream output) {
109        this.output = output;
110    }
111
112    /**
113     * Gets the output stream.
114     * @return the output stream to which to write. May be <code>null</code>.
115     */
116    public OutputStream getOutput() {
117        return output;
118    }
119    
120    /* INTERFACE Runnable */
121
122        /**
123         * {@inheritDoc}
124         */
125        public void run() {
126                
127        LOGGER.entering(getClass().getName(), "run");
128        
129        if (input != null) {
130                try {                   
131                        BufferedOutputStream bout = null;
132                        if (output != null) {
133                    bout = new BufferedOutputStream(output);
134                }  
135                        BufferedInputStream bin = new BufferedInputStream(input);
136                boolean log = LOGGER.isLoggable(Level.FINEST);
137                        byte[] buffer = new byte[1024];
138                        int len;
139                        while ((len = bin.read(buffer)) != -1) {
140                    if (log) {
141                        String data = new String(buffer, 0, len);
142                        if (tag == null) {
143                            LOGGER.finest(data);
144                        } else {
145                            LOGGER.finest("<" + tag + "> " + data);
146                        }
147                    }
148                    if (bout != null) {
149                                        bout.write(buffer, 0, len);
150                                        bout.flush();
151                    }
152                        }
153                } catch (Exception e) {
154                    LOGGER.severe(e.toString());
155                }
156        }
157        LOGGER.exiting(getClass().getName(), "run");
158        }
159
160}