JAL-1286 URL stream reader tries to use a GZIPInputStream if URL ends in .gz
authorJim Procter <jprocter@compbio.dundee.ac.uk>
Fri, 12 Apr 2013 04:37:56 +0000 (14:37 +1000)
committerJim Procter <jprocter@compbio.dundee.ac.uk>
Mon, 13 May 2013 13:57:24 +0000 (14:57 +0100)
src/jalview/io/FileParse.java
test/jalview/io/FileIOTester.java [new file with mode: 0644]
test/jalview/io/test_gz_fasta.gz [new file with mode: 0644]

index 62237d9..f9440fa 100755 (executable)
@@ -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 (file)
index 0000000..62ce958
--- /dev/null
@@ -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 (file)
index 0000000..f2850cf
Binary files /dev/null and b/test/jalview/io/test_gz_fasta.gz differ