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
023package org.biojava.bio.seq.io;
024
025import java.io.IOException;
026import java.io.OutputStream;
027import java.io.PrintStream;
028
029import org.biojava.bio.BioException;
030import org.biojava.bio.seq.SequenceIterator;
031
032/**
033 * Writes all of the sequences from a SequenceIterator to a stream with a
034 * particular format.
035 * <p>
036 * This can be wired from a StreamReader to make a simple file-format conversion
037 * utility, or can be used to write out the sequences in a database to disk.
038 *
039 * <p>More functionality is offered by {@link org.biojavax.bio.seq.io.RichStreamWriter RichStreamWriter},
040 * Use of this interface is prefered.</p>
041 *
042 * @author Matthew Pocock
043 * @see org.biojavax.bio.seq.io.RichStreamWriter
044 */
045public class StreamWriter {
046  /**
047   * The format to write in.
048   */
049  private SequenceFormat format;
050  
051  /**
052   * The stream to write to.
053   */
054  private PrintStream os;
055
056  /**
057   * Write each of the sequences in ss to the stream in the given format.
058   *
059   * @param ss  the SequenceIterator to loop over
060   * @throws IOException if the stream has any problems
061   */
062  public void writeStream(SequenceIterator ss)
063              throws IOException {
064    while(ss.hasNext()) {
065      try {
066        format.writeSequence(ss.nextSequence(), os);
067      } catch (BioException se) {
068        se.printStackTrace();
069      }
070    }
071  }
072
073  /**
074   * Generate a new StreamWriter to the stream os and using format.
075   *
076   * @param os  the OutputStream to write to
077   * @param format the SequenceFormat to write with
078   */
079  public StreamWriter(OutputStream os, SequenceFormat format) {
080    this.os = new PrintStream(os);
081    this.format = format;
082  }
083}