* 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
* \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
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