From 48f16780e1a818055c883d259df7285a3226b073 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Fri, 12 Apr 2013 14:37:56 +1000 Subject: [PATCH] JAL-1286 URL stream reader tries to use a GZIPInputStream if URL ends in .gz --- src/jalview/io/FileParse.java | 29 +++++++++++++++++++++- test/jalview/io/FileIOTester.java | 49 +++++++++++++++++++++++++++++++++++++ test/jalview/io/test_gz_fasta.gz | Bin 0 -> 398 bytes 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 test/jalview/io/FileIOTester.java create mode 100644 test/jalview/io/test_gz_fasta.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 0000000000000000000000000000000000000000..f2850cf38c2fa33a586a221f6ec2821252d2d50d GIT binary patch literal 398 zcmV;90df8xiwFS1$e>UF169(^Zmcj21>imJ!lKK&sg%KviR|&@$2H3Pzer6BR~;4@ z3G(IWkk600+cMkiNhVBY?1)f6b|ENYu1rHb3FQIQF!hR9)^%C0iw;qmie`kJAXl>K zI14SGACJesC9}8Gc9@n73{aYIB5)Tk^)hnPpGU+7NXsEla>fCNww+fL{G3K=e^TRdNQ!S*3Ault@ z0TXLeh}8ATNU%Mvl>IU~!p|af=#FC)9ijd42^y_6yf0;#!k z6jH1R^M#Dx0i)?38jiy#=t$j>O0G$x>s37H^mr{Q`J^H40jtR<#)K^0g)U?2onPrK z^@gcMPps7F_$|b$YtVV>Vi!H*1Q-Iy$oom>Rz&a6