update of javajs package; adds org.apache.harmony, org.xml.sax.helpers
[jalview.git] / src / javajs / util / Rdr.java
index c357581..995a629 100644 (file)
@@ -43,7 +43,6 @@ import java.util.Map;
 import javajs.api.Interface;
 
 
-import javajs.J2SIgnoreImport;
 import javajs.api.GenericCifDataParser;
 import javajs.api.GenericLineReader;
 import javajs.api.GenericZipTools;
@@ -62,9 +61,28 @@ import javajs.api.GenericZipTools;
  * 
  * 
  */
-@J2SIgnoreImport(BufferedWriter.class)
 public class Rdr implements GenericLineReader {
 
+  public static class StreamReader extends BufferedReader {
+
+    private BufferedInputStream stream;
+
+    public StreamReader(BufferedInputStream bis, String charSet) throws UnsupportedEncodingException {
+      super(new InputStreamReader(bis, (charSet == null ? "UTF-8" : charSet)));
+      stream = bis;
+    }
+    
+    public BufferedInputStream getStream() {
+      try {
+        stream.reset();
+      } catch (IOException e) {
+        // ignore
+      }
+      return stream;
+    }
+
+  }
+
   BufferedReader reader;
 
   public Rdr(BufferedReader reader) {
@@ -89,7 +107,7 @@ public class Rdr implements GenericLineReader {
    * Read a UTF-8 byte array fully, converting it to a String.
    * Called by Jmol's XMLReaders
    * 
-   * @param bis
+   * @param bytes
    * @return a UTF-8 string
    */
   public static String bytesToUTF8String(byte[] bytes) {
@@ -126,7 +144,7 @@ public class Rdr implements GenericLineReader {
       throws IOException {
     // could also just make sure we have a buffered input stream here.
     if (getUTFEncodingForStream(bis) == Encoding.NONE)
-      return new BufferedReader(new InputStreamReader(bis, (charSet == null ? "UTF-8" : charSet)));
+      return new StreamReader(bis, (charSet == null ? "UTF-8" : charSet));
     byte[] bytes = getLimitedStreamBytes(bis, -1);
     bis.close();
     return getBR(charSet == null ? fixUTF(bytes) : new String(bytes, charSet));
@@ -136,7 +154,7 @@ public class Rdr implements GenericLineReader {
    * This method is specifically for strings that are marked for UTF 8 or 16.
    * 
    * @param bytes
-   * @return
+   * @return UTF-decoded bytes
    */
        public static String fixUTF(byte[] bytes) {
                Encoding encoding = getUTFEncoding(bytes);
@@ -181,14 +199,14 @@ public class Rdr implements GenericLineReader {
   
 
   private static Encoding getUTFEncodingForStream(BufferedInputStream is) throws IOException {
-    /**
-     * @j2sNative
-     * 
-     *  is.resetStream();
-     * 
-     */
-    {
-    }
+//    /**
+//     * @j2sNative
+//     * 
+//     *  is.resetStream();
+//     * 
+//     */
+//    {
+//    }
     byte[] abMagic = new byte[4];
     abMagic[3] = 1;
     try{
@@ -217,10 +235,19 @@ public class Rdr implements GenericLineReader {
         && (bytes[7] & 0xFF) == 0xE1);
   }
 
+  public static boolean isBZip2S(InputStream is) {
+    return isBZip2B(getMagic(is, 3));
+  }
+
   public static boolean isGzipS(InputStream is) {
     return isGzipB(getMagic(is, 2));
   }
 
+  public static boolean isBZip2B(byte[] bytes) {    
+    return (bytes != null && bytes.length >= 3  // BZh
+        && (bytes[0] & 0xFF) == 0x42 && (bytes[1] & 0xFF) == 0x5A  && (bytes[2] & 0xFF) == 0x68);
+}
+
   public static boolean isGzipB(byte[] bytes) {    
       return (bytes != null && bytes.length >= 2 
           && (bytes[0] & 0xFF) == 0x1F && (bytes[1] & 0xFF) == 0x8B);
@@ -236,12 +263,16 @@ public class Rdr implements GenericLineReader {
   }
 
   public static boolean isMessagePackS(InputStream is) {
-    return isMessagePackB(getMagic(is, 1));
+    return isMessagePackB(getMagic(is, 2));
   }
 
   public static boolean isMessagePackB(byte[] bytes) {
-    // look for 'map' start
-    return (bytes != null && bytes.length >= 1 && (bytes[0] & 0xFF) == 0xDE);
+    // look for 'map' start, but PNG files start with 0x89, which is
+    // the MessagePack start for a 9-member map, so in that case we have
+    // to check that the next byte is not "P" as in <89>PNG
+    int b;
+    
+    return (bytes != null && bytes.length >= 1 && (((b = bytes[0] & 0xFF)) == 0xDE || (b & 0xE0) == 0x80 && bytes[1] != 0x50));
   }
 
   public static boolean isPngZipStream(InputStream is) {
@@ -253,6 +284,11 @@ public class Rdr implements GenericLineReader {
     return (bytes[50] == 0 && bytes[51] == 0x50 && bytes[52] == 0x4E && bytes[53] == 0x47 && bytes[54] == 0x4A);
   }
 
+  /**
+   * Check for a ZIP input stream - starting with "PK<03><04>"
+   * @param is
+   * @return true if a ZIP stream
+   */
   public static boolean isZipS(InputStream is) {
     return isZipB(getMagic(is, 4));
   }
@@ -266,15 +302,15 @@ public class Rdr implements GenericLineReader {
   }
 
   public static byte[] getMagic(InputStream is, int n) {
-    byte[] abMagic = new byte[n];    
-    /**
-     * @j2sNative
-     * 
-     * is.resetStream();
-     * 
-     */
-    {
-    }
+    byte[] abMagic = new byte[n];
+//    /**
+//     * @j2sNative
+//     * 
+//     * is.resetStream();
+//     * 
+//     */
+//    {
+//    }
     try {
       is.mark(n + 1);
       is.read(abMagic, 0, n);
@@ -314,6 +350,14 @@ public class Rdr implements GenericLineReader {
     return new BufferedReader(new StringReader(string));
   }
 
+  
+       public static BufferedInputStream toBIS(Object o) {
+               return (AU.isAB(o) ? getBIS((byte[]) o)
+                               : o instanceof SB ? getBIS(Rdr.getBytesFromSB((SB) o))
+                                               : o instanceof String ? getBIS(((String) o).getBytes()) : null);
+       }
+
+
   /**
    * Drill down into a GZIP stack until no more layers.
    * @param jzt 
@@ -329,6 +373,14 @@ public class Rdr implements GenericLineReader {
     return bis;
   }
 
+  public static BufferedInputStream getUnzippedInputStreamBZip2(GenericZipTools jzt,
+                                                                BufferedInputStream bis) throws IOException  {
+    while (isBZip2S(bis))
+      bis = new BufferedInputStream(jzt.newBZip2InputStream(bis));
+    return bis;
+  }
+
+
   /**
    * Allow for base64-encoding check.
    * 
@@ -544,7 +596,9 @@ public class Rdr implements GenericLineReader {
 
        public static BufferedWriter getBufferedWriter(OutputStream os, String charSetName) {
                OutputStreamWriter osw = (OutputStreamWriter) Interface.getInstanceWithParams("java.io.OutputStreamWriter", 
-                               new Class<?>[] { java.io.OutputStream.class, String.class }, new Object[] {os, charSetName});
+                               new Class<?>[] { java.io.OutputStream.class, String.class }, 
+                               new Object[] { os, charSetName == null ? "UTF-8" : charSetName }
+               );
                /**
                 * @j2sNative
                 * return osw.getBufferedWriter();