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