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