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 *    ReaderWriterPipe.java
024 */
025package org.biojava.utils.process;
026
027import java.io.BufferedReader;
028import java.io.BufferedWriter;
029import java.io.Reader;
030import java.io.Writer;
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 reader to an output 
037 * writer. 
038 * @author <a href="mailto:Martin.Szugat@GMX.net">Martin Szugat</a>
039 * @version $Revision$
040 */
041public class ReaderWriterPipe implements Runnable {
042    
043    /* STATIC FIELDS */
044    
045    /**
046     * The class logger.
047     */
048    private static final Logger LOGGER = 
049        Logger.getLogger(ReaderWriterPipe.class.getName());
050    
051    /* PRIVATE FIELDS */
052
053    /**
054     * The reader from which to read.
055     */
056    private Reader reader;
057
058    /**
059     * The writer to which to write.
060     */
061    private Writer writer;
062
063    /**
064     * A tag for logging.
065     */
066    private String tag;
067    
068    /* PUBLIC CONSTRUCTORS */
069
070    /**
071     * Initializes the reader writer pipe.
072     * @param reader the reader from which to read. May be <code>null</code>.
073     * @param writer the writer to which to write. May be <code>null</code>.
074     * @param tag a tag for loggging. May be <code>null</code>.
075     */
076    public ReaderWriterPipe(Reader reader, Writer writer, String tag) {
077        setReader(reader);
078        setWriter(writer);
079        this.tag = tag;
080    }
081    
082    /* PUBLIC PROPERTIES */
083
084    /**
085     * Gets the reader.
086     * @return the reader from which to read. May be <code>null</code>.
087     */
088    public Reader getReader() {
089        return reader;
090    }
091
092    /**
093     * Gets the writer.
094     * @return the writer to which to write. May be <code>null</code>.
095     */
096    public Writer getWriter() {
097        return writer;
098    }
099
100    /**
101     * Sets the reader.
102     * @param reader the reader from which to read. May be <code>null</code>.
103     */
104    public void setReader(Reader reader) {
105        this.reader = reader;
106    }
107
108    /**
109     * Sets the writer.
110     * @param writer the writer to which to write. May be <code>null</code>.
111     */
112    public void setWriter(Writer writer) {
113        this.writer = writer;
114    }
115
116    /* INTERFACE Runnable */
117
118    /**
119     * {@inheritDoc}
120     */
121    public void run() {
122
123        LOGGER.entering(getClass().getName(), "run");
124
125        if (reader != null) {
126            try {
127
128                BufferedWriter bout = null;
129                if (writer != null) {
130                    bout = new BufferedWriter(writer);
131                }
132                BufferedReader bin = new BufferedReader(reader);
133                boolean log = LOGGER.isLoggable(Level.FINEST);
134                String line = null;
135                while ((line = bin.readLine()) != null) {
136                    if (bout != null) {
137                        if (log) {
138                            if (tag == null) {
139                                LOGGER.finest(line);
140                            } else {
141                                LOGGER.finest("<" + tag + "> " + line);
142                            }
143                        }
144                        bout.write(line);
145                        bout.newLine();
146                        bout.flush();
147                    }
148                }
149
150            } catch (Exception e) {
151                LOGGER.severe(e.toString());
152            }
153        }
154
155        LOGGER.exiting(getClass().getName(), "run");
156    }
157}