use OOMwarning to warn user when out of Memory occurs
[jalview.git] / src / jalview / io / IdentifyFile.java
index a8d93d5..4deb858 100755 (executable)
@@ -1,17 +1,17 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
- *
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
+ * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
- *
+ * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *
+ * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
@@ -40,6 +40,7 @@ public class IdentifyFile
    */
   public String Identify(String file, String protocol)
   {
+    String emessage = "UNIDENTIFIED FILE PARSING ERROR";
     FileParse parser = null;
     try {
       parser = new FileParse(file, protocol);
@@ -49,10 +50,11 @@ public class IdentifyFile
     } catch (Exception e) {
       System.err.println("Error whilst identifying");
       e.printStackTrace(System.err);
+      emessage = e.getMessage();
     }
     if (parser!=null)
       return parser.errormessage;
-    return "UNIDENTIFIED FILE PARSING ERROR";
+    return emessage;
   }
   public String Identify(FileParse source) {
     return Identify(source, true); // preserves original behaviour prior to version 2.3
@@ -66,10 +68,42 @@ public class IdentifyFile
   public String Identify(FileParse source, boolean closeSource) {
     String reply = "PFAM";
     String data;
+    int length=0;
     boolean lineswereskipped=false;
+    boolean isBinary = false; // true if length is non-zero and non-printable characters are encountered
     try {
+      if (!closeSource)
+      {
+        source.mark();
+      }
       while ( (data = source.nextLine()) != null)
       {
+        length+=data.length();
+        if (!lineswereskipped)
+        {
+          for (int i=0;!isBinary && i<data.length(); i++)
+          {
+            char c = data.charAt(i);
+            isBinary = (c<32 && c!='\t' && c!='\n' && c!='\r' && c!=5 && c!=27); // nominal binary character filter excluding CR, LF, tab,DEL and ^E for certain blast ids 
+          }
+        }
+        if (isBinary)
+        {
+          // jar files are special - since they contain all sorts of random characters.
+          if (source.inFile!=null) 
+          {
+              String fileStr=source.inFile.getName();
+              // possibly a Jalview archive. 
+              if (fileStr.lastIndexOf(".jar")>-1 || fileStr.lastIndexOf(".zip")>-1) 
+              {
+                reply = "Jalview";
+              }
+          } 
+          if (!lineswereskipped && data.startsWith("PK")) {
+            reply="Jalview"; // archive.
+            break;
+          }
+        }
         data = data.toUpperCase();
 
         if ( (data.indexOf("# STOCKHOLM") > -1))
@@ -136,10 +170,10 @@ public class IdentifyFile
             }
             else
             {
-              reply = "FASTA";
+                reply = "FASTA";
+                // TODO : AMSA File is indicated if there is annotation in the FASTA file - but FASTA will automatically generate this at the mo.
             }
           }
-
           break;
         }
         else if (data.indexOf("HEADER") == 0 ||
@@ -157,18 +191,6 @@ public class IdentifyFile
           reply = "JnetFile";
           break;
         }
-        else  if (source.inFile!=null) 
-        {
-            String fileStr=source.inFile.getName();
-            // possibly a Jalview archive. 
-            if (fileStr.lastIndexOf(".jar")>-1 || fileStr.lastIndexOf(".zip")>-1) 
-            {
-              reply = "Jalview";
-            }
-        } else if (data.startsWith("PK")) {
-          reply="Jalview"; // archive.
-          break;
-        }
         
         lineswereskipped=true; // this means there was some junk before any key file signature  
       }
@@ -183,7 +205,23 @@ public class IdentifyFile
       System.err.println("File Identification failed!\n" + ex);
       return source.errormessage;
     }
-
+    if (length==0)
+    {
+      System.err.println("File Identification failed! - Empty file was read.");
+      return "EMPTY DATA FILE";
+    }
     return reply;
   }
+  public static void main(String[] args) {
+    for (int i=0; args!=null && i<args.length; i++)
+    {
+      IdentifyFile ider = new IdentifyFile();
+      String type = ider.Identify(args[i], AppletFormatAdapter.FILE);
+      System.out.println("Type of "+args[i]+" is "+type);
+    }
+    if (args==null || args.length==0)
+    {
+      System.err.println("Usage: <Filename> [<Filename> ...]");
+    }
+  }
 }