From: Jim Procter Date: Fri, 12 Apr 2013 04:37:56 +0000 (+1000) Subject: JAL-1286 URL stream reader tries to use a GZIPInputStream if URL ends in .gz X-Git-Tag: Jalview_2_9~251 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=48f16780e1a818055c883d259df7285a3226b073;p=jalview.git JAL-1286 URL stream reader tries to use a GZIPInputStream if URL ends in .gz --- diff --git a/src/jalview/io/FileParse.java b/src/jalview/io/FileParse.java index 62237d9..f9440fa 100755 --- a/src/jalview/io/FileParse.java +++ b/src/jalview/io/FileParse.java @@ -19,6 +19,7 @@ package jalview.io; import java.io.*; import java.net.*; +import java.util.zip.GZIPInputStream; /** * implements a random access wrapper around a particular datasource, for @@ -152,7 +153,33 @@ public class FileParse { errormessage = "URL NOT FOUND"; URL url = new URL(fileStr); - dataIn = new BufferedReader(new InputStreamReader(url.openStream())); + // + // GZIPInputStream code borrowed from Aquaria (soon to be open sourced) via Kenny Sabir + Exception e=null; + if (fileStr.endsWith(".gz")) { + try { + InputStream inputStream = url.openStream(); + dataIn = new BufferedReader(new InputStreamReader(new GZIPInputStream(inputStream))); + dataIn.mark(2048); + dataIn.read(); + dataIn.reset(); + + dataName = fileStr; + return false; + } catch (Exception ex) { + e=ex; + } + } + + try { + dataIn = new BufferedReader(new InputStreamReader(url.openStream())); + } catch (IOException q) { + if (e!=null) + { + throw new IOException("Failed to resolve GZIP stream", e); + } + throw q; + } // record URL as name of datasource. dataName = fileStr; return false; diff --git a/test/jalview/io/FileIOTester.java b/test/jalview/io/FileIOTester.java new file mode 100644 index 0000000..62ce958 --- /dev/null +++ b/test/jalview/io/FileIOTester.java @@ -0,0 +1,49 @@ +/** + * + */ +package jalview.io; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author jimp + * + */ +public class FileIOTester +{ + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception + { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception + { + } +final static File ALIGN_FILE = new File("test/jalview/io/test_gz_fasta.gz"); + + @Test + public void testGzipIo() throws IOException + { + String uri; + FileParse fp = new FileParse(uri=ALIGN_FILE.getAbsoluteFile().toURI().toString(),AppletFormatAdapter.URL); + assertTrue("Couldn't resolve "+uri+" as a valid file",fp.isValid()); + String type = new IdentifyFile().Identify(fp); + assertTrue("Gzipped data from '"+uri+"' identified as '"+type+"'",type.equalsIgnoreCase("FASTA")); + } + +} diff --git a/test/jalview/io/test_gz_fasta.gz b/test/jalview/io/test_gz_fasta.gz new file mode 100644 index 0000000..f2850cf Binary files /dev/null and b/test/jalview/io/test_gz_fasta.gz differ