From f55e379f8941bc5589a5fa24f77dc6b4e69635d6 Mon Sep 17 00:00:00 2001 From: pvtroshin Date: Wed, 27 Apr 2011 23:22:08 +0000 Subject: [PATCH] A wee improvement git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@4042 e3abac25-378b-4346-85de-24260fe3988d --- datamodel/compbio/data/sequence/FastaReader.java | 34 +++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/datamodel/compbio/data/sequence/FastaReader.java b/datamodel/compbio/data/sequence/FastaReader.java index 10eec8f..4b6fd66 100644 --- a/datamodel/compbio/data/sequence/FastaReader.java +++ b/datamodel/compbio/data/sequence/FastaReader.java @@ -58,14 +58,28 @@ public class FastaReader implements Iterator { * read from * @throws FileNotFoundException * if the input file is not found + * @throws IllegalStateException + * if the close method was called on this instance + * */ - public FastaReader(final String input) throws FileNotFoundException { - this.input = new Scanner(new File(input), "UTF8"); - this.input.useDelimiter("\\s*>"); - } + public FastaReader(final String inputFile) throws FileNotFoundException { + input = new Scanner(new File(inputFile), "UTF8"); + input.useDelimiter("\\s*>"); + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + if (input != null) { + input.close(); + } + } + }); + } /** * {@inheritDoc} + * + * @throws IllegalStateException + * if the close method was called on this instance */ @Override public boolean hasNext() { @@ -77,6 +91,8 @@ public class FastaReader implements Iterator { * * @throws AssertionError * if the header or the sequence is missing + * @throws IllegalStateException + * if the close method was called on this instance */ @Override public FastaSequence next() { @@ -91,6 +107,16 @@ public class FastaReader implements Iterator { throw new UnsupportedOperationException(); } + /** + * Call this method to close the connection to the input file if you want to + * free up the resources. The connection will be closed on the JVM shutdown + * if this method was not called explicitly. No further reading on this + * instance of the FastaReader will be possible after calling this method. + */ + public void close() { + input.close(); + } + private static FastaSequence toFastaSequence(final String singleFastaEntry) { final Scanner sc = new Scanner(singleFastaEntry); // Use new line delimiter -- 1.7.10.2