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.
8 import java.util.zip.*;
12 public static class JavaSourceCode {
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)); }
17 public static InputStream decode(String s) throws IOException {
18 return new GZIPInputStream(new StringInputStream(s)); }
20 private static class StringInputStream extends InputStream {
21 private final String s;
22 private final int length;
24 public StringInputStream(String s) { this.s = s; this.length = s.length(); }
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();
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);
42 public static String encode(String packageName, String className, InputStream is) throws IOException {
44 // compress first, since the encoded form has more entropy
45 ByteArrayOutputStream baos;
46 OutputStream os = new GZIPOutputStream(baos = new ByteArrayOutputStream());
48 byte[] buf = new byte[1024];
50 int numread = is.read(buf, 0, buf.length);
51 if (numread == -1) break;
52 os.write(buf, 0, numread);
55 buf = baos.toByteArray();
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;) {
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);
71 ret.append(" \"\";\n");
73 return ret.toString();