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