added org.ibex.util.Encode.JavaSourceCode
authoradam <adam@megacz.com>
Wed, 29 Dec 2004 04:42:03 +0000 (04:42 +0000)
committeradam <adam@megacz.com>
Wed, 29 Dec 2004 04:42:03 +0000 (04:42 +0000)
darcs-hash:20041229044203-5007d-51d0438d9b6e4b82dfe2d0808d0f490c06a3a019.gz

src/org/ibex/util/Encode.java [new file with mode: 0644]

diff --git a/src/org/ibex/util/Encode.java b/src/org/ibex/util/Encode.java
new file mode 100644 (file)
index 0000000..7021dc2
--- /dev/null
@@ -0,0 +1,50 @@
+package org.ibex.util;
+import java.io.*;
+import java.util.*;
+import java.util.zip.*;
+
+public class Encode {
+
+    public static class JavaSourceCode {
+
+        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 String encode(String packageName, String className, InputStream is) throws IOException {
+
+            // compress first, since the encoded form has more entropy
+            ByteArrayOutputStream baos;
+            OutputStream os = new GZIPOutputStream(baos = new ByteArrayOutputStream());
+
+            byte[] buf = new byte[1024];
+            while(true) {
+                int numread = is.read(buf, 0, buf.length);
+                if (numread == -1) break;
+                os.write(buf, 0, numread);
+            }
+            os.close();
+            buf = baos.toByteArray();
+            
+            StringBuffer ret = new StringBuffer();
+            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");
+            for(int pos = 0; pos<buf.length;) {
+                ret.append("        \"");
+                for(int i=0; i<LINE_LENGTH && pos<buf.length; i++) {
+                    String cs = Integer.toOctalString(buf[pos++] & 0xff);
+                    while(cs.length() < 3) cs = "0" + cs;
+                    ret.append("\\" + cs);
+                }
+                ret.append("\" +\n");
+            }
+            ret.append("    \"\";\n");
+            ret.append("}\n");
+            return ret.toString();
+        }
+
+    }
+
+}
+