added Encode.JavaSourceCode.decode()
[org.ibex.util.git] / src / org / ibex / util / Encode.java
1 package org.ibex.util;
2 import java.io.*;
3 import java.util.*;
4 import java.util.zip.*;
5
6 public class Encode {
7
8     public static class JavaSourceCode {
9
10         public static final int LINE_LENGTH = 80 / 4;
11         public static void main(String[] s) throws Exception { System.out.println(encode(s[0], s[1], System.in)); }
12
13         public static InputStream decode(String s) throws IOException {
14             return new GZIPInputStream(new StringInputStream(s)); }
15         
16         private static class StringInputStream extends InputStream {
17             private final String s;
18             private final int length;
19             private int pos = 0;
20             public StringInputStream(String s) { this.s = s; this.length = s.length(); }
21             public int read() {
22                 byte[] b = new byte[1];
23                 int numread = read(b, 0, 1);
24                 if (numread == -1) return -1;
25                 if (numread == 0) throw new Error();
26                 return b[0] & 0xff;
27             }
28             public int read(byte[] b, int off, int len) {
29                 for(int i=off; i<off+len; i++) {
30                     if (pos>=length) return i-off;
31                     //int out = s.charAt(pos++);
32                     b[i] = (byte)s.charAt(pos++);//(byte)(out > 127 ? 127-out : out);
33                 }
34                 return len;
35             }
36         }
37
38         public static String encode(String packageName, String className, InputStream is) throws IOException {
39
40             // compress first, since the encoded form has more entropy
41             ByteArrayOutputStream baos;
42             OutputStream os = new GZIPOutputStream(baos = new ByteArrayOutputStream());
43
44             byte[] buf = new byte[1024];
45             while(true) {
46                 int numread = is.read(buf, 0, buf.length);
47                 if (numread == -1) break;
48                 os.write(buf, 0, numread);
49             }
50             os.close();
51             buf = baos.toByteArray();
52             
53             StringBuffer ret = new StringBuffer();
54             ret.append("// generated by " + Encode.class.getName() + "\n\n");
55             ret.append("package " + packageName + ";\n\n");
56             ret.append("public class " + className + " {\n");
57             ret.append("    public static final String data = \n");
58             for(int pos = 0; pos<buf.length;) {
59                 ret.append("        \"");
60                 for(int i=0; i<LINE_LENGTH && pos<buf.length; i++) {
61                     String cs = Integer.toOctalString(buf[pos++] & 0xff);
62                     while(cs.length() < 3) cs = "0" + cs;
63                     ret.append("\\" + cs);
64                 }
65                 ret.append("\" +\n");
66             }
67             ret.append("    \"\";\n");
68             ret.append("}\n");
69             return ret.toString();
70         }
71
72     }
73
74 }
75