From 1b10059cdf8b6a85fddd86a2a3c6588be3257c22 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Tue, 11 Jan 2022 20:13:57 +0000 Subject: [PATCH] JAL-3703 fix Gff3 shared InputStream with embedded FASTA data --- src/jalview/io/AlignFile.java | 21 +++++++++++++++++++-- src/jalview/io/FastaFile.java | 11 ++++++++--- src/jalview/io/FeaturesFile.java | 4 +++- src/jalview/io/FileParse.java | 4 ++-- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/jalview/io/AlignFile.java b/src/jalview/io/AlignFile.java index 9e426ab..3202ac9 100755 --- a/src/jalview/io/AlignFile.java +++ b/src/jalview/io/AlignFile.java @@ -79,6 +79,8 @@ public abstract class AlignFile extends FileParse private boolean parseImmediately = true; + private boolean dataClosed = false; + /** * @return if doParse() was called at construction time */ @@ -165,6 +167,12 @@ public abstract class AlignFile extends FileParse public AlignFile(boolean parseImmediately, FileParse source) throws IOException { + this(parseImmediately, source, true); + } + + public AlignFile(boolean parseImmediately, FileParse source, + boolean closeData) throws IOException + { super(source); initData(); @@ -174,7 +182,7 @@ public abstract class AlignFile extends FileParse if (parseImmediately) { - doParse(); + doParse(closeData); } } @@ -185,6 +193,11 @@ public abstract class AlignFile extends FileParse */ public void doParse() throws IOException { + doParse(true); + } + + public void doParse(boolean closeData) throws IOException + { if (parseCalled) { throw new IOException( @@ -193,7 +206,11 @@ public abstract class AlignFile extends FileParse } parseCalled = true; parse(); - dataIn.close(); + if (closeData && !dataClosed) + { + dataIn.close(); + dataClosed = true; + } } /** diff --git a/src/jalview/io/FastaFile.java b/src/jalview/io/FastaFile.java index 9acd7da..c698a31 100755 --- a/src/jalview/io/FastaFile.java +++ b/src/jalview/io/FastaFile.java @@ -20,14 +20,14 @@ */ package jalview.io; +import java.io.IOException; + import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.Annotation; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceI; -import java.io.IOException; - /** * DOCUMENT ME! * @@ -69,7 +69,12 @@ public class FastaFile extends AlignFile public FastaFile(FileParse source) throws IOException { - super(source); + this(source, true); + } + + public FastaFile(FileParse source, boolean closeData) throws IOException + { + super(true, source, closeData); } public FastaFile(SequenceI[] seqs) diff --git a/src/jalview/io/FeaturesFile.java b/src/jalview/io/FeaturesFile.java index dda59a7..71fc659 100755 --- a/src/jalview/io/FeaturesFile.java +++ b/src/jalview/io/FeaturesFile.java @@ -1393,7 +1393,9 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI } catch (IOException q) { } - FastaFile parser = new FastaFile(this); + // Opening a FastaFile object with the remainder of this object's dataIn. + // Tell the constructor to NOT close the dataIn when finished. + FastaFile parser = new FastaFile(this, false); List includedseqs = parser.getSeqs(); SequenceIdMatcher smatcher = new SequenceIdMatcher(newseqs); diff --git a/src/jalview/io/FileParse.java b/src/jalview/io/FileParse.java index 2ff0d27..5fd33be 100755 --- a/src/jalview/io/FileParse.java +++ b/src/jalview/io/FileParse.java @@ -230,7 +230,7 @@ public class FileParse return false; } input.mark(4); - + // get first 2 bytes or return false byte[] bytes = new byte[2]; int read = input.read(bytes); @@ -239,7 +239,7 @@ public class FileParse { return false; } - + int header = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); return (GZIPInputStream.GZIP_MAGIC == header); } -- 1.7.10.2