JAL-3615 used gzip endpoints for Pfam and Rfam
[jalview.git] / src / jalview / io / FileParse.java
index a5a4e36..dac8fe6 100755 (executable)
@@ -40,6 +40,8 @@ import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureSettingsModelI;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
+import jalview.ws.dbsources.Pfam;
+import jalview.ws.dbsources.Rfam;
 
 /**
  * implements a random access wrapper around a particular datasource, for
@@ -51,6 +53,8 @@ public class FileParse
 
   protected static final String TAB = "\t";
 
+  private static final String GZ_EXT = ".gz";
+
   /**
    * text specifying source of data. usually filename or url.
    */
@@ -64,6 +68,7 @@ public class FileParse
   {
     return bytes;
   }
+
   /**
    * a viewport associated with the current file operation. May be null. May
    * move to different object.
@@ -190,7 +195,7 @@ public class FileParse
     }
     if (!error)
     {
-      if (fileStr.toLowerCase().endsWith(".gz"))
+      if (fileStr.toLowerCase().endsWith(GZ_EXT))
       {
         try
         {
@@ -232,7 +237,7 @@ public class FileParse
     // GZIPInputStream code borrowed from Aquaria (soon to be open sourced) via
     // Kenny Sabir
     Exception e = null;
-    if (fileStr.toLowerCase().endsWith(".gz"))
+    if (isGzipped(fileStr))
     {
       try
       {
@@ -264,6 +269,27 @@ public class FileParse
   }
 
   /**
+   * Answers true if the filename (or URL) has a format which Jalview recognises
+   * as denoting gzipped content.
+   * <p>
+   * Currently this means having a ".gz" extension, or ending in "/gzipped" or
+   * "?gz=1" (used to retrieve gzipped from Pfam and Rfam respectively).
+   * 
+   * @param filename
+   * @return
+   */
+  protected static boolean isGzipped(String filename)
+  {
+    if (filename == null)
+    {
+      return false;
+    }
+    String lower = filename.toLowerCase();
+    return lower.endsWith(GZ_EXT) || lower.endsWith(Pfam.GZIPPED)
+            || lower.endsWith(Rfam.GZIPPED);
+  }
+
+  /**
    * sets the suffix string (if any) and returns remainder (if suffix was
    * detected)
    * 
@@ -345,7 +371,8 @@ public class FileParse
       {
         // this will be from JavaScript
         inFile = file;
-        dataIn = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes)));
+        dataIn = new BufferedReader(
+                new InputStreamReader(new ByteArrayInputStream(bytes)));
         dataName = fileStr;
       }
       else if (checkFileSource(fileStr))
@@ -453,8 +480,7 @@ public class FileParse
     {
       // pass up the reason why we have no source to read from
       throw new IOException(MessageManager.formatMessage(
-              "exception.failed_to_read_data_from_source",
-              new String[]
+              "exception.failed_to_read_data_from_source", new String[]
               { errormessage }));
     }
     error = false;