A wee improvement
authorpvtroshin <pvtroshin@e3abac25-378b-4346-85de-24260fe3988d>
Wed, 27 Apr 2011 23:22:08 +0000 (23:22 +0000)
committerpvtroshin <pvtroshin@e3abac25-378b-4346-85de-24260fe3988d>
Wed, 27 Apr 2011 23:22:08 +0000 (23:22 +0000)
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

index 10eec8f..4b6fd66 100644 (file)
@@ -58,14 +58,28 @@ public class FastaReader implements Iterator<FastaSequence> {
         *            read from\r
         * @throws FileNotFoundException\r
         *             if the input file is not found\r
+        * @throws IllegalStateException\r
+        *             if the close method was called on this instance\r
+        * \r
         */\r
-       public FastaReader(final String input) throws FileNotFoundException {\r
-               this.input = new Scanner(new File(input), "UTF8");\r
-               this.input.useDelimiter("\\s*>");\r
-       }\r
+       public FastaReader(final String inputFile) throws FileNotFoundException {\r
+               input = new Scanner(new File(inputFile), "UTF8");\r
+               input.useDelimiter("\\s*>");\r
+               Runtime.getRuntime().addShutdownHook(new Thread() {\r
 \r
+                       @Override\r
+                       public void run() {\r
+                               if (input != null) {\r
+                                       input.close();\r
+                               }\r
+                       }\r
+               });\r
+       }\r
        /**\r
         * {@inheritDoc}\r
+        * \r
+        * @throws IllegalStateException\r
+        *             if the close method was called on this instance\r
         */\r
        @Override\r
        public boolean hasNext() {\r
@@ -77,6 +91,8 @@ public class FastaReader implements Iterator<FastaSequence> {
         * \r
         * @throws AssertionError\r
         *             if the header or the sequence is missing\r
+        * @throws IllegalStateException\r
+        *             if the close method was called on this instance\r
         */\r
        @Override\r
        public FastaSequence next() {\r
@@ -91,6 +107,16 @@ public class FastaReader implements Iterator<FastaSequence> {
                throw new UnsupportedOperationException();\r
        }\r
 \r
+       /**\r
+        * Call this method to close the connection to the input file if you want to\r
+        * free up the resources. The connection will be closed on the JVM shutdown\r
+        * if this method was not called explicitly. No further reading on this\r
+        * instance of the FastaReader will be possible after calling this method.\r
+        */\r
+       public void close() {\r
+               input.close();\r
+       }\r
+\r
        private static FastaSequence toFastaSequence(final String singleFastaEntry) {\r
                final Scanner sc = new Scanner(singleFastaEntry);\r
                // Use new line delimiter\r