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 *    ReaderInputHandler.java
024 */
025package org.biojava.utils.process;
026
027import java.io.IOException;
028import java.io.OutputStream;
029import java.io.OutputStreamWriter;
030import java.io.Reader;
031import java.util.logging.Logger;
032
033
034/**
035 * Reader {@linkplain org.biojava.utils.process.InputHandler input handler} 
036 * that reads the input for an external process from a 
037 * {@linkplain org.biojava.utils.process.ReaderWriterPipe#getReader() reader}. The
038 * {@linkplain org.biojava.utils.process.StreamPipe#getOutput() output stream} for
039 * the input of the external process is closed after the reader is read
040 * to its end.
041 * @author <a href="mailto:Martin.Szugat@GMX.net">Martin Szugat</a>
042 * @version $Revision$
043 */
044public class ReaderInputHandler extends ReaderWriterPipe implements
045        InputHandler {
046    
047    /* STATIC FIELDS */
048
049    /**
050     * The class logger.
051     */
052    private static final Logger LOGGER = Logger
053            .getLogger(ReaderInputHandler.class.getName());
054    
055    /* PRIVATE FIELDS */
056
057    /**
058     * The output stream for the external process.
059     */
060    private OutputStream output = null;
061    
062    /* PUBLIC CONSTRUCTORS */
063
064    /**
065     * Initializes the reader input handler.
066     * @param reader the reader from which to read the input for the external
067     * process. May be <code>null</code>.
068     * @param tag a tag for logging. May be <code>null</code>. 
069     */
070    public ReaderInputHandler(Reader reader, String tag) {
071        super(reader, null, tag);
072    }
073    
074    /* INTERFACE OutputHandler */
075
076    /**
077     * {@inheritDoc}
078     */
079    public void setOutput(OutputStream output) {
080        this.output = output;
081        if (output != null) {
082            setWriter(new OutputStreamWriter(output));
083        }
084    }
085    
086    /* INTERFACE OutputHandler */
087
088    /**
089     * {@inheritDoc}
090     */
091    public OutputStream getOutput() {
092        return output;
093    }
094    
095    /* INTERFACE Runnable */
096
097    /**
098     * {@inheritDoc}
099     */
100    public void run() {
101        super.run();
102        try {
103            getWriter().close();
104        } catch (IOException e) {
105            LOGGER.severe(e.toString());
106        }
107    }
108}