added Encode.JavaSourceCode.decode()
authoradam <adam@megacz.com>
Wed, 29 Dec 2004 07:25:40 +0000 (07:25 +0000)
committeradam <adam@megacz.com>
Wed, 29 Dec 2004 07:25:40 +0000 (07:25 +0000)
darcs-hash:20041229072540-5007d-a998b9a7c0367d510326b8aee80afddb04099a8c.gz

src/org/ibex/util/Encode.java

index 7021dc2..9334d46 100644 (file)
@@ -10,6 +10,31 @@ public class Encode {
         public static final int LINE_LENGTH = 80 / 4;
         public static void main(String[] s) throws Exception { System.out.println(encode(s[0], s[1], System.in)); }
 
+        public static InputStream decode(String s) throws IOException {
+            return new GZIPInputStream(new StringInputStream(s)); }
+        
+        private static class StringInputStream extends InputStream {
+            private final String s;
+            private final int length;
+            private int pos = 0;
+            public StringInputStream(String s) { this.s = s; this.length = s.length(); }
+            public int read() {
+                byte[] b = new byte[1];
+                int numread = read(b, 0, 1);
+                if (numread == -1) return -1;
+                if (numread == 0) throw new Error();
+                return b[0] & 0xff;
+            }
+            public int read(byte[] b, int off, int len) {
+                for(int i=off; i<off+len; i++) {
+                    if (pos>=length) return i-off;
+                    //int out = s.charAt(pos++);
+                    b[i] = (byte)s.charAt(pos++);//(byte)(out > 127 ? 127-out : out);
+                }
+                return len;
+            }
+        }
+
         public static String encode(String packageName, String className, InputStream is) throws IOException {
 
             // compress first, since the encoded form has more entropy
@@ -29,7 +54,7 @@ public class Encode {
             ret.append("// generated by " + Encode.class.getName() + "\n\n");
             ret.append("package " + packageName + ";\n\n");
             ret.append("public class " + className + " {\n");
-            ret.append("    public static String data = \n");
+            ret.append("    public static final String data = \n");
             for(int pos = 0; pos<buf.length;) {
                 ret.append("        \"");
                 for(int i=0; i<LINE_LENGTH && pos<buf.length; i++) {