Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

814
LINES

< > BotCompany Repo | #2000360 // x12.java

New Tinybrain snippet

1  
/**
2  
 JavaX runner version 12 as embeddable class (non-public)
3  
4  
 Changes to v11:
5  
 -Ported to Windows
6  
7  
 */
8  
9  
class x12 {
10  
  static final String version = "JavaX 12";
11  
12  
  static boolean verbose = false, translate = false, list = false, virtualizeTranslators = true;
13  
  static boolean preferCached = false;
14  
  static List<String> mainTranslators = new ArrayList<String>();
15  
  private static Map<Long, String> memSnippetCache = new HashMap<Long, String>();
16  
  private static int processesStarted;
17  
18  
  public static void main(String[] args) throws Exception {
19  
    File ioBaseDir = new File("."), inputDir = null, outputDir = null;
20  
    String src = ".";
21  
    for (int i = 0; i < args.length; i++) {
22  
      String arg = args[i];
23  
      if (arg.equals("-v") || arg.equals("-verbose"))
24  
        verbose = true;
25  
      else if (arg.equals("-finderror"))
26  
        verbose = true;
27  
      else if (arg.equals("-offline") || arg.equalsIgnoreCase("-prefercached"))
28  
        preferCached = true;
29  
      else if (arg.equals("-novirt"))
30  
        virtualizeTranslators = false;
31  
      else if (arg.equals("translate"))
32  
        translate = true;
33  
      else if (arg.equals("list"))
34  
        list = true;
35  
      else if (arg.equals("run")) {
36  
        // it's the default command anyway
37  
      } else if (arg.startsWith("input="))
38  
        inputDir = new File(arg.substring(6));
39  
      else if (arg.startsWith("output="))
40  
        outputDir = new File(arg.substring(7));
41  
      else if (arg.equals("with"))
42  
        mainTranslators.add(args[++i]);
43  
      else
44  
        src = arg;
45  
    }
46  
47  
    if (virtualizeTranslators && !preferCached)
48  
      initDiskSnippetCache(TempDirMaker_make());
49  
50  
    if (inputDir != null) {
51  
      ioBaseDir = TempDirMaker_make();
52  
      System.out.println("Taking input from: " + inputDir.getAbsolutePath());
53  
      System.out.println("Output is in: " + new File(ioBaseDir, "output").getAbsolutePath());
54  
      copyInput(inputDir, new File(ioBaseDir, "input"));
55  
    }
56  
57  
    javax4(src, ioBaseDir, translate, list);
58  
59  
    if (outputDir != null) {
60  
      copyInput(new File(ioBaseDir, "output"), outputDir);
61  
      System.out.println("Output copied to: " + outputDir.getAbsolutePath());
62  
    }
63  
64  
    if (verbose)
65  
      System.out.println("Processes started: " + processesStarted);
66  
  }
67  
68  
  public static void javax4(String src, File ioDir, boolean translate, boolean list) throws Exception {
69  
    File srcDir;
70  
    if (isSnippetID(src))
71  
      srcDir = loadSnippetAsMainJava(src);
72  
    else {
73  
      srcDir = new File(src);
74  
      if (!new File(srcDir, "main.java").exists()) {
75  
        System.out.println("This is " + version  + ".\n" +
76  
          "No main.java found, exiting");
77  
        return;
78  
      }
79  
    }
80  
81  
    // translate
82  
83  
    List<File> libraries = new ArrayList<File>();
84  
    File X = srcDir;
85  
    X = applyTranslators(X, mainTranslators, libraries);
86  
    X = defaultTranslate(X, libraries);
87  
88  
    // list or run
89  
90  
    if (translate)
91  
      System.out.println("Program translated to: " + X.getAbsolutePath());
92  
    else if (list)
93  
      System.out.println(loadTextFile(new File(X, "main.java").getPath(), null));
94  
    else
95  
      javax2(X, ioDir, false, false, libraries);
96  
  }
97  
98  
  private static File defaultTranslate(File x, List<File> libraries_out) throws Exception {
99  
    x = luaPrintToJavaPrint(x);
100  
    x = autoTranslate(x, libraries_out);
101  
    return x;
102  
  }
103  
104  
  private static File autoTranslate(File x, List<File> libraries_out) throws Exception {
105  
    String main = loadTextFile(new File(x, "main.java").getPath(), null);
106  
    List<String> lines = toLines(main);
107  
    List<String> translators = findTranslators(lines);
108  
    if (translators.isEmpty())
109  
      return x;
110  
111  
    main = fromLines(lines);
112  
    File newDir = TempDirMaker_make();
113  
    saveTextFile(new File(newDir, "main.java").getPath(), main);
114  
    return applyTranslators(newDir, translators, libraries_out);
115  
  }
116  
117  
  private static List<String> findTranslators(List<String> lines) {
118  
    List<String> translators = new ArrayList<String>();
119  
    Pattern pattern = Pattern.compile("^!([0-9# \t]+)");
120  
    for (ListIterator<String> iterator = lines.listIterator(); iterator.hasNext(); ) {
121  
      String line = iterator.next();
122  
      line = line.trim();
123  
      Matcher matcher = pattern.matcher(line);
124  
      if (matcher.find()) {
125  
        translators.addAll(Arrays.asList(matcher.group(1).split("[ \t]+")));
126  
        iterator.remove();
127  
      }
128  
    }
129  
    return translators;
130  
  }
131  
132  
  public static List<String> toLines(String s) {
133  
    List<String> lines = new ArrayList<String>();
134  
    int start = 0;
135  
    while (true) {
136  
      int i = toLines_nextLineBreak(s, start);
137  
      if (i < 0) {
138  
        if (s.length() > start) lines.add(s.substring(start));
139  
        break;
140  
      }
141  
142  
      lines.add(s.substring(start, i));
143  
      if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
144  
        i += 2;
145  
      else
146  
        ++i;
147  
148  
      start = i;
149  
    }
150  
    return lines;
151  
  }
152  
153  
  private static int toLines_nextLineBreak(String s, int start) {
154  
    for (int i = start; i < s.length(); i++) {
155  
      char c = s.charAt(i);
156  
      if (c == '\r' || c == '\n')
157  
        return i;
158  
    }
159  
    return -1;
160  
  }
161  
162  
  public static String fromLines(List<String> lines) {
163  
    StringBuilder buf = new StringBuilder();
164  
    for (String line : lines) {
165  
      buf.append(line).append('\n');
166  
    }
167  
    return buf.toString();
168  
  }
169  
170  
  private static File applyTranslators(File x, List<String> translators, List<File> libraries_out) throws Exception {
171  
    for (String translator : translators)
172  
      x = applyTranslator(x, translator, libraries_out);
173  
    return x;
174  
  }
175  
176  
  // also takes a library
177  
  private static File applyTranslator(File x, String translator, List<File> libraries_out) throws Exception {
178  
    if (verbose)
179  
      System.out.println("Using translator " + translator + " on sources in " + x.getPath());
180  
181  
    File newDir = runTranslatorOnInput(translator, null, x, !verbose, libraries_out);
182  
183  
    if (!new File(newDir, "main.java").exists()) {
184  
      throw new Exception("Translator " + translator + " did not generate main.java");
185  
      // TODO: show translator output
186  
    }
187  
    if (verbose)
188  
      System.out.println("Translated with " + translator + " from " + x.getPath() + " to " + newDir.getPath());
189  
    x = newDir;
190  
    return x;
191  
  }
192  
193  
  private static File luaPrintToJavaPrint(File x) throws IOException {
194  
    File newDir = TempDirMaker_make();
195  
    String code = loadTextFile(new File(x, "main.java").getPath(), null);
196  
    code = luaPrintToJavaPrint(code);
197  
    if (verbose)
198  
      System.out.println(code);
199  
    saveTextFile(new File(newDir, "main.java").getPath(), code);
200  
    return newDir;
201  
  }
202  
203  
  public static String luaPrintToJavaPrint(String code) {
204  
    return ("\n" + code).replaceAll(
205  
      "(\n\\s*)print (\".*\")",
206  
      "$1System.out.println($2);").substring(1);
207  
  }
208  
209  
  public static File loadSnippetAsMainJava(String snippetID) throws IOException {
210  
    File srcDir = TempDirMaker_make();
211  
    saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippet(snippetID));
212  
    return srcDir;
213  
  }
214  
215  
  public static File loadSnippetAsMainJavaVerified(String snippetID, String hash) throws IOException {
216  
    File srcDir = TempDirMaker_make();
217  
    saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippetVerified(snippetID, hash));
218  
    return srcDir;
219  
  }
220  
221  
  /** returns output dir */
222  
  private static File runTranslatorOnInput(String snippetID, String hash, File input, boolean silent,
223  
                                           List<File> libraries_out) throws Exception {
224  
    File libraryFile = DiskSnippetCache_getLibrary(parseSnippetID(snippetID));
225  
    if (libraryFile != null) {
226  
      loadLibrary(snippetID, libraries_out, libraryFile);
227  
      return input;
228  
    }
229  
230  
    File srcDir = hash == null ? loadSnippetAsMainJava(snippetID)
231  
      : loadSnippetAsMainJavaVerified(snippetID, hash);
232  
233  
    long mainJavaSize = new File(srcDir, "main.java").length();
234  
235  
    if (mainJavaSize == 0) { // no text in snippet? assume it's a library
236  
      loadLibrary(snippetID, libraries_out, libraryFile);
237  
      return input;
238  
    }
239  
240  
    List<File> libraries = new ArrayList<File>();
241  
    srcDir = defaultTranslate(srcDir, libraries);
242  
    boolean runInProcess = false;
243  
    File ioBaseDir = TempDirMaker_make();
244  
245  
    if (virtualizeTranslators) {
246  
      if (verbose) System.out.println("Virtualizing translator");
247  
248  
      //srcDir = applyTranslator(srcDir, "#2000351"); // I/O-virtualize the translator
249  
      // that doesn't work because it recurses infinitely...
250  
251  
      // So we do it right here:
252  
      String s = loadTextFile(new File(srcDir, "main.java").getPath(), null);
253  
      s = s.replaceAll("new\\s+File\\(", "virtual.newFile(");
254  
      s = s.replaceAll("new\\s+FileInputStream\\(", "virtual.newFileInputStream(");
255  
      s = s.replaceAll("new\\s+FileOutputStream\\(", "virtual.newFileOutputStream(");
256  
      s += "\n\n" + loadSnippet("#2000355"); // load class virtual
257  
258  
      // change baseDir
259  
      s = s.replace("virtual_baseDir = \"\";",
260  
        "virtual_baseDir = " + javaQuote(ioBaseDir.getAbsolutePath()) + ";");
261  
262  
      // forward snippet cache
263  
      s = s.replace("static File DiskSnippetCache_dir;",
264  
        "static File DiskSnippetCache_dir = new File(" + javaQuote(DiskSnippetCache_dir.getAbsolutePath()) + ");");
265  
      s = s.replace("static boolean preferCached = false;", "static boolean preferCached = true;");
266  
267  
      if (verbose) {
268  
        System.out.println("==BEGIN VIRTUALIZED TRANSLATOR==");
269  
        System.out.println(s);
270  
        System.out.println("==END VIRTUALIZED TRANSLATOR==");
271  
      }
272  
      saveTextFile(new File(srcDir, "main.java").getPath(), s);
273  
274  
      // TODO: silence translator also
275  
      runInProcess = true;
276  
    }
277  
278  
    return runJavaX(ioBaseDir, srcDir, input, silent, runInProcess, libraries);
279  
  }
280  
281  
  private static void loadLibrary(String snippetID, List<File> libraries_out, File libraryFile) throws IOException {
282  
    if (verbose)
283  
      System.out.println("Assuming " + snippetID + " is a library.");
284  
285  
    if (libraryFile == null) {
286  
      byte[] data;
287  
      try {
288  
        URL url = new URL("http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + parseSnippetID(snippetID)
289  
          + "&contentType=application/binary");
290  
        System.err.println("Loading library: " + url);
291  
        data = loadBinaryPage(url.openConnection(), url);
292  
        if (verbose)
293  
          System.err.println("Bytes loaded: " + data.length);
294  
      } catch (FileNotFoundException e) {
295  
        throw new IOException("Binary snippet #" + snippetID + " not found or not public");
296  
      }
297  
      DiskSnippetCache_putLibrary(parseSnippetID(snippetID), data);
298  
      libraryFile = DiskSnippetCache_getLibrary(parseSnippetID(snippetID));
299  
    }
300  
301  
    if (!libraries_out.contains(libraryFile))
302  
      libraries_out.add(libraryFile);
303  
  }
304  
305  
  /** returns output dir */
306  
  private static File runJavaX(File ioBaseDir, File originalSrcDir, File originalInput,
307  
                               boolean silent, boolean runInProcess,
308  
                               List<File> libraries) throws Exception {
309  
    File srcDir = new File(ioBaseDir, "src");
310  
    File inputDir = new File(ioBaseDir, "input");
311  
    File outputDir = new File(ioBaseDir, "output");
312  
    copyInput(originalSrcDir, srcDir);
313  
    copyInput(originalInput, inputDir);
314  
    javax2(srcDir, ioBaseDir, silent, runInProcess, libraries);
315  
    return outputDir;
316  
  }
317  
318  
  private static void copyInput(File src, File dst) throws IOException {
319  
    copyDirectory(src, dst);
320  
  }
321  
322  
  public static boolean hasFile(File inputDir, String name) {
323  
    return new File(inputDir, name).exists();
324  
  }
325  
326  
  public static void copyDirectory(File src, File dst) throws IOException {
327  
    if (verbose) System.out.println("Copying " + src.getAbsolutePath() + " to " + dst.getAbsolutePath());
328  
    dst.mkdirs();
329  
    File[] files = src.listFiles();
330  
    if (files == null) return;
331  
    for (File file : files) {
332  
      File dst1 = new File(dst, file.getName());
333  
      if (file.isDirectory())
334  
        copyDirectory(file, dst1);
335  
      else {
336  
        if (verbose) System.out.println("Copying " + file.getAbsolutePath() + " to " + dst1.getAbsolutePath());
337  
        copy(file, dst1);
338  
      }
339  
    }
340  
  }
341  
342  
  /** Quickly copy a file without a progress bar or any other fancy GUI... :) */
343  
  public static void copy(File src, File dest) throws IOException {
344  
    FileInputStream inputStream = new FileInputStream(src);
345  
    FileOutputStream outputStream = new FileOutputStream(dest);
346  
    try {
347  
      copy(inputStream, outputStream);
348  
      inputStream.close();
349  
    } finally {
350  
      outputStream.close();
351  
    }
352  
  }
353  
354  
  public static void copy(InputStream in, OutputStream out) throws IOException {
355  
    byte[] buf = new byte[65536];
356  
    while (true) {
357  
      int n = in.read(buf);
358  
      if (n <= 0) return;
359  
      out.write(buf, 0, n);
360  
    }
361  
  }
362  
363  
  /** writes safely (to temp file, then rename) */
364  
  public static void saveTextFile(String fileName, String contents) throws IOException {
365  
    File file = new File(fileName);
366  
    File parentFile = file.getParentFile();
367  
    if (parentFile != null)
368  
      parentFile.mkdirs();
369  
    String tempFileName = fileName + "_temp";
370  
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
371  
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles);
372  
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
373  
    printWriter.print(contents);
374  
    printWriter.close();
375  
    if (file.exists() && !file.delete())
376  
      throw new IOException("Can't delete " + fileName);
377  
378  
    if (!new File(tempFileName).renameTo(file))
379  
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
380  
  }
381  
382  
  /** writes safely (to temp file, then rename) */
383  
  public static void saveBinaryFile(String fileName, byte[] contents) throws IOException {
384  
    File file = new File(fileName);
385  
    File parentFile = file.getParentFile();
386  
    if (parentFile != null)
387  
      parentFile.mkdirs();
388  
    String tempFileName = fileName + "_temp";
389  
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
390  
    fileOutputStream.write(contents);
391  
    fileOutputStream.close();
392  
    if (file.exists() && !file.delete())
393  
      throw new IOException("Can't delete " + fileName);
394  
395  
    if (!new File(tempFileName).renameTo(file))
396  
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
397  
  }
398  
399  
  public static String loadTextFile(String fileName, String defaultContents) throws IOException {
400  
    if (!new File(fileName).exists())
401  
      return defaultContents;
402  
403  
    FileInputStream fileInputStream = new FileInputStream(fileName);
404  
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles);
405  
    return loadTextFile(inputStreamReader);
406  
  }
407  
408  
  public static String loadTextFile(Reader reader) throws IOException {
409  
    StringBuilder builder = new StringBuilder();
410  
    try {
411  
      BufferedReader bufferedReader = new BufferedReader(reader);
412  
      String line;
413  
      while ((line = bufferedReader.readLine()) != null)
414  
        builder.append(line).append('\n');
415  
    } finally {
416  
      reader.close();
417  
    }
418  
    return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
419  
  }
420  
421  
  static File DiskSnippetCache_dir;
422  
423  
  public static void initDiskSnippetCache(File dir) {
424  
    DiskSnippetCache_dir = dir;
425  
    dir.mkdirs();
426  
  }
427  
428  
  // Data files are immutable, use centralized cache
429  
  public static synchronized File DiskSnippetCache_getLibrary(long snippetID) throws IOException {
430  
    File file = new File(getGlobalCache(), "data_" + snippetID + ".jar");
431  
    if (verbose)
432  
      System.out.println("Checking data cache: " + file.getPath());
433  
    return file.exists() ? file : null;
434  
  }
435  
436  
  public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
437  
    return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
438  
  }
439  
440  
  private static File DiskSnippetCache_getFile(long snippetID) {
441  
    return new File(DiskSnippetCache_dir, "" + snippetID);
442  
  }
443  
444  
  public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
445  
    saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
446  
  }
447  
448  
  public static synchronized void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException {
449  
    saveBinaryFile(new File(getGlobalCache(), "data_" + snippetID).getPath() + ".jar", data);
450  
  }
451  
452  
  public static File DiskSnippetCache_getDir() {
453  
    return DiskSnippetCache_dir;
454  
  }
455  
456  
  public static void initSnippetCache() {
457  
    if (DiskSnippetCache_dir == null)
458  
      initDiskSnippetCache(getGlobalCache());
459  
  }
460  
461  
  private static File getGlobalCache() {
462  
    File file = new File(System.getProperty("user.home"), ".tinybrain/snippet-cache");
463  
    file.mkdirs();
464  
    return file;
465  
  }
466  
467  
  public static String loadSnippetVerified(String snippetID, String hash) throws IOException {
468  
    String text = loadSnippet(snippetID);
469  
    String realHash = getHash(text.getBytes("UTF-8"));
470  
    if (!realHash.equals(hash)) {
471  
      String msg;
472  
      if (hash.isEmpty())
473  
        msg = "Here's your hash for " + snippetID + ", please put in your program: " + realHash;
474  
      else
475  
        msg = "Hash mismatch for " + snippetID + ": " + realHash + " (new) vs " + hash + " - has tinybrain.de been hacked??";
476  
      throw new RuntimeException(msg);
477  
    }
478  
    return text;
479  
  }
480  
481  
  public static String getHash(byte[] data) {
482  
    return bytesToHex(getFullFingerprint(data));
483  
  }
484  
485  
  public static byte[] getFullFingerprint(byte[] data) {
486  
    try {
487  
      return java.security.MessageDigest.getInstance("MD5").digest(data);
488  
    } catch (java.security.NoSuchAlgorithmException e) {
489  
      throw new RuntimeException(e);
490  
    }
491  
  }
492  
493  
  public static String bytesToHex(byte[] bytes) {
494  
    return bytesToHex(bytes, 0, bytes.length);
495  
  }
496  
497  
  public static String bytesToHex(byte[] bytes, int ofs, int len) {
498  
    StringBuilder stringBuilder = new StringBuilder(len*2);
499  
    for (int i = 0; i < len; i++) {
500  
      String s = "0" + Integer.toHexString(bytes[ofs+i]);
501  
      stringBuilder.append(s.substring(s.length()-2, s.length()));
502  
    }
503  
    return stringBuilder.toString();
504  
  }
505  
506  
  public static String loadSnippet(String snippetID) throws IOException {
507  
    return loadSnippet(parseSnippetID(snippetID));
508  
  }
509  
510  
  public static long parseSnippetID(String snippetID) {
511  
    return Long.parseLong(shortenSnippetID(snippetID));
512  
  }
513  
514  
  private static String shortenSnippetID(String snippetID) {
515  
    if (snippetID.startsWith("#"))
516  
      snippetID = snippetID.substring(1);
517  
    String httpBlaBla = "http://tinybrain.de/";
518  
    if (snippetID.startsWith(httpBlaBla))
519  
      snippetID = snippetID.substring(httpBlaBla.length());
520  
    return snippetID;
521  
  }
522  
523  
  public static boolean isSnippetID(String snippetID) {
524  
    snippetID = shortenSnippetID(snippetID);
525  
    return isInteger(snippetID) && Long.parseLong(snippetID) != 0;
526  
  }
527  
528  
  public static boolean isInteger(String s) {
529  
    return Pattern.matches("\\-?\\d+", s);
530  
  }
531  
532  
  public static String loadSnippet(long snippetID) throws IOException {
533  
    String text = memSnippetCache.get(snippetID);
534  
    if (text != null)
535  
      return text;
536  
537  
    if (preferCached) {
538  
      initSnippetCache();
539  
      text = DiskSnippetCache_get(snippetID);
540  
      if (text != null)
541  
        return text;
542  
    }
543  
544  
    try {
545  
      URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID);
546  
      text = loadPage(url);
547  
    } catch (FileNotFoundException e) {
548  
      throw new IOException("Snippet #" + snippetID + " not found or not public");
549  
    }
550  
551  
    memSnippetCache.put(snippetID, text);
552  
553  
    try {
554  
      initSnippetCache();
555  
      DiskSnippetCache_put(snippetID, text);
556  
    } catch (IOException e) {
557  
      System.err.println("Minor warning: Couldn't save snippet to cache ("  + DiskSnippetCache_getDir() + ")");
558  
    }
559  
560  
    return text;
561  
  }
562  
563  
  private static String loadPage(URL url) throws IOException {
564  
    System.err.println("Loading: " + url.toExternalForm());
565  
    URLConnection con = url.openConnection();
566  
    return loadPage(con, url);
567  
  }
568  
569  
  public static String loadPage(URLConnection con, URL url) throws IOException {
570  
    String contentType = con.getContentType();
571  
    if (contentType == null)
572  
      throw new IOException("Page could not be read: " + url);
573  
    //Log.info("Content-Type: " + contentType);
574  
    String charset = guessCharset(contentType);
575  
    Reader r = new InputStreamReader(con.getInputStream(), charset);
576  
    StringBuilder buf = new StringBuilder();
577  
    while (true) {
578  
      int ch = r.read();
579  
      if (ch < 0)
580  
        break;
581  
      //Log.info("Chars read: " + buf.length());
582  
      buf.append((char) ch);
583  
    }
584  
    return buf.toString();
585  
  }
586  
587  
  public static byte[] loadBinaryPage(URLConnection con, URL url) throws IOException {
588  
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
589  
    InputStream inputStream = con.getInputStream();
590  
    while (true) {
591  
      int ch = inputStream.read();
592  
      if (ch < 0)
593  
        break;
594  
      buf.write(ch);
595  
    }
596  
    inputStream.close();
597  
    return buf.toByteArray();
598  
  }
599  
600  
  public static String guessCharset(String contentType) {
601  
    Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
602  
    Matcher m = p.matcher(contentType);
603  
    /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */
604  
    return m.matches() ? m.group(1) : "ISO-8859-1";
605  
  }
606  
607  
  /** runs a transpiled set of sources */
608  
  public static void javax2(File srcDir, File ioBaseDir, boolean silent, boolean runInProcess,
609  
                            List<File> libraries) throws Exception {
610  
    // collect sources
611  
612  
    List<File> sources = new ArrayList<File>();
613  
    if (verbose) System.out.println("Scanning for sources in " + srcDir.getPath());
614  
    scanForSources(srcDir, sources, true);
615  
    if (sources.isEmpty()) {
616  
      System.out.println("No sources found");
617  
      return;
618  
    }
619  
620  
    // compile
621  
622  
    File optionsFile = File.createTempFile("javax", "");
623  
    File classesDir = TempDirMaker_make();
624  
    if (verbose) System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath());
625  
    String options = "-d " + bashQuote(classesDir.getPath());
626  
    writeOptions(sources, libraries, optionsFile, options);
627  
    classesDir.mkdirs();
628  
    String javacOutput = invokeJavac(optionsFile);
629  
630  
    // run
631  
632  
    if (verbose) System.out.println("Running program (" + srcDir.getAbsolutePath()
633  
      + ") on io dir " + ioBaseDir.getAbsolutePath() + (runInProcess ? "[in-process]" : "") + "\n");
634  
    runProgram(javacOutput, classesDir, ioBaseDir, silent, runInProcess, libraries);
635  
  }
636  
637  
  private static void runProgram(String javacOutput, File classesDir, File ioBaseDir,
638  
                                 boolean silent, boolean runInProcess,
639  
                                 List<File> libraries) throws Exception {
640  
    // print javac output if compile failed and it hasn't been printed yet
641  
    boolean didNotCompile = !hasFile(classesDir, "main.class");
642  
    if (verbose || didNotCompile)
643  
      System.out.println(javacOutput);
644  
    if (didNotCompile)
645  
      return;
646  
647  
    if (runInProcess
648  
      || (ioBaseDir.getAbsolutePath().equals(new File(".").getAbsolutePath()) && !silent)) {
649  
      runProgramQuick(classesDir, libraries);
650  
      return;
651  
    }
652  
653  
    boolean echoOK = false;
654  
    // TODO: add libraries to class path
655  
    String bashCmd = "(cd " + bashQuote(ioBaseDir.getAbsolutePath()) + " && (java -cp "
656  
      + bashQuote(classesDir.getAbsolutePath()) + " main" + (echoOK ? "; echo ok" : "") + "))";
657  
    if (verbose) System.out.println(bashCmd);
658  
    String output = backtick(bashCmd);
659  
    if (verbose || !silent)
660  
      System.out.println(output);
661  
  }
662  
663  
  private static void runProgramQuick(File classesDir, List<File> libraries) throws Exception {
664  
    // collect urls
665  
    URL[] urls = new URL[libraries.size()+1];
666  
    urls[0] = classesDir.toURI().toURL();
667  
    for (int i = 0; i < libraries.size(); i++)
668  
      urls[i+1] = libraries.get(i).toURI().toURL();
669  
670  
    // make class loader
671  
    URLClassLoader classLoader = new URLClassLoader(urls);
672  
673  
    // load JavaX main class
674  
    Class<?> mainClass = classLoader.loadClass("main");
675  
676  
    // run main method
677  
    java.lang.reflect.Method main = mainClass.getMethod("main", String[].class);
678  
    main.invoke(null, (Object) new String[0]);
679  
  }
680  
681  
  private static String invokeJavac(File optionsFile) throws IOException {
682  
    String output;
683  
    try {
684  
      output = invokeEcj(optionsFile);
685  
    } catch (NoClassDefFoundError e) {
686  
      if (verbose) {
687  
        System.err.println("ecj not found - using javac");
688  
        e.printStackTrace();
689  
      }
690  
      output = backtick("javac " + bashQuote("@" + optionsFile.getPath()));
691  
    }
692  
    if (verbose) System.out.println(output);
693  
    return output;
694  
  }
695  
696  
  // throws ClassNotFoundError if ecj is not in classpath
697  
  static String invokeEcj(File optionsFile) {
698  
    StringWriter writer = new StringWriter();
699  
    PrintWriter printWriter = new PrintWriter(writer);
700  
    org.eclipse.jdt.core.compiler.CompilationProgress progress = null;
701  
    org.eclipse.jdt.core.compiler.batch.BatchCompiler.compile(
702  
      new String[] { "@" + optionsFile.getPath(), "-source", "1.7" },
703  
      printWriter,
704  
      printWriter,
705  
      progress);
706  
    return writer.toString();
707  
  }
708  
709  
  private static void writeOptions(List<File> sources, List<File> libraries,
710  
                                   File optionsFile, String moreOptions) throws IOException {
711  
    FileWriter writer = new FileWriter(optionsFile);
712  
    for (File source : sources)
713  
      writer.write(bashQuote(source.getPath()) + " ");
714  
    if (!libraries.isEmpty()) {
715  
      List<String> cp = new ArrayList<String>();
716  
      for (File lib : libraries)
717  
        cp.add(lib.getAbsolutePath());
718  
      writer.write("-cp " + bashQuote(join(File.pathSeparator, cp)) + " ");
719  
    }
720  
    writer.write(moreOptions);
721  
    writer.close();
722  
  }
723  
724  
  private static void scanForSources(File source, List<File> sources, boolean topLevel) {
725  
    if (source.isFile() && source.getName().endsWith(".java"))
726  
      sources.add(source);
727  
    else if (source.isDirectory() && !isSkippedDirectoryName(source.getName(), topLevel)) {
728  
      File[] files = source.listFiles();
729  
      for (File file : files)
730  
        scanForSources(file, sources, false);
731  
    }
732  
  }
733  
734  
  private static boolean isSkippedDirectoryName(String name, boolean topLevel) {
735  
    if (topLevel) return false; // input or output ok as highest directory (intentionally specified by user, not just found by a directory scan in which case we probably don't want it. it's more like heuristics actually.)
736  
    return name.equalsIgnoreCase("input") || name.equalsIgnoreCase("output");
737  
  }
738  
739  
  public static String backtick(String cmd) throws IOException {
740  
    ++processesStarted;
741  
    File outFile = File.createTempFile("_backtick", "");
742  
    File scriptFile = File.createTempFile("_backtick", isWindows() ? ".bat" : "");
743  
744  
    String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1";
745  
    //Log.info("[Backtick] " + command);
746  
    try {
747  
      saveTextFile(scriptFile.getPath(), command);
748  
      String[] command2;
749  
      if (isWindows())
750  
        command2 = new String[] { scriptFile.getPath() };
751  
      else
752  
        command2 = new String[] { "/bin/bash", scriptFile.getPath() };
753  
      Process process = Runtime.getRuntime().exec(command2);
754  
      try {
755  
        process.waitFor();
756  
      } catch (InterruptedException e) {
757  
        throw new RuntimeException(e);
758  
      }
759  
      process.exitValue();
760  
      return loadTextFile(outFile.getPath(), "");
761  
    } finally {
762  
      scriptFile.delete();
763  
    }
764  
  }
765  
766  
  /** possibly improvable */
767  
  public static String javaQuote(String text) {
768  
    return bashQuote(text);
769  
  }
770  
771  
  /** possibly improvable */
772  
  public static String bashQuote(String text) {
773  
    if (text == null) return null;
774  
    return "\"" + text
775  
      .replace("\\", "\\\\")
776  
      .replace("\"", "\\\"")
777  
      .replace("\n", "\\n")
778  
      .replace("\r", "\\r") + "\"";
779  
  }
780  
781  
  public final static String charsetForTextFiles = "UTF8";
782  
783  
  static long TempDirMaker_lastValue;
784  
785  
  public static File TempDirMaker_make() {
786  
    File dir = new File(System.getProperty("user.home"), ".javax/" + TempDirMaker_newValue());
787  
    dir.mkdirs();
788  
    return dir;
789  
  }
790  
791  
  private static long TempDirMaker_newValue() {
792  
    long value;
793  
    do
794  
      value = System.currentTimeMillis();
795  
    while (value == TempDirMaker_lastValue);
796  
    TempDirMaker_lastValue = value;
797  
    return value;
798  
  }
799  
800  
  public static String join(String glue, Iterable<String> strings) {
801  
    StringBuilder buf = new StringBuilder();
802  
    Iterator<String> i = strings.iterator();
803  
    if (i.hasNext()) {
804  
      buf.append(i.next());
805  
      while (i.hasNext())
806  
        buf.append(glue).append(i.next());
807  
    }
808  
    return buf.toString();
809  
  }
810  
811  
  public static boolean isWindows() {
812  
    return System.getProperty("os.name").contains("Windows");
813  
  }
814  
}

Author comment

Began life as a copy of #2000359

download  show line numbers   

Snippet is not live.

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
296 #1000604 (pitcher) 2015-08-18 00:54:43
253 #1000610 (pitcher) Edit suggestion:
!636
!629

main {
static Object androidContext;
static String programID;

public static void main(String[] args) throws Exception {
/**
JavaX runner version 12 as embeddable class (non-public)

Changes to v11:
-Ported to Windows

*/

class x12 {
static final String version = "JavaX 12";

static boolean verbose = false, translate = false, list = false, virtualizeTranslators = true;
static boolean preferCached = false;
static List<String> mainTranslators = new ArrayList<String>();
private static Map<Long, String> memSnippetCache = new HashMap<Long, String>();
private static int processesStarted;

public static void main(String[] args) throws Exception {
File ioBaseDir = new File("."), inputDir = null, outputDir = null;
String src = ".";
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (arg.equals("-v") || arg.equals("-verbose"))
verbose = true;
else if (arg.equals("-finderror"))
verbose = true;
else if (arg.equals("-offline") || arg.equalsIgnoreCase("-prefercached"))
preferCached = true;
else if (arg.equals("-novirt"))
virtualizeTranslators = false;
else if (arg.equals("translate"))
translate = true;
else if (arg.equals("list"))
list = true;
else if (arg.equals("run")) {
// it's the default command anyway
} else if (arg.startsWith("input="))
inputDir = new File(arg.substring(6));
else if (arg.startsWith("output="))
outputDir = new File(arg.substring(7));
else if (arg.equals("with"))
mainTranslators.add(args[++i]);
else
src = arg;
}

if (virtualizeTranslators && !preferCached)
initDiskSnippetCache(TempDirMaker_make());

if (inputDir != null) {
ioBaseDir = TempDirMaker_make();
System.out.println("Taking input from: " + inputDir.getAbsolutePath());
System.out.println("Output is in: " + new File(ioBaseDir, "output").getAbsolutePath());
copyInput(inputDir, new File(ioBaseDir, "input"));
}

javax4(src, ioBaseDir, translate, list);

if (outputDir != null) {
copyInput(new File(ioBaseDir, "output"), outputDir);
System.out.println("Output copied to: " + outputDir.getAbsolutePath());
}

if (verbose)
System.out.println("Processes started: " + processesStarted);
}

public static void javax4(String src, File ioDir, boolean translate, boolean list) throws Exception {
File srcDir;
if (isSnippetID(src))
srcDir = loadSnippetAsMainJava(src);
else {
srcDir = new File(src);
if (!new File(srcDir, "main.java").exists()) {
System.out.println("This is " + version + ".\n" +
"No main.java found, exiting");
return;
}
}

// translate

List<File> libraries = new ArrayList<File>();
File X = srcDir;
X = applyTranslators(X, mainTranslators, libraries);
X = defaultTranslate(X, libraries);

// list or run

if (translate)
System.out.println("Program translated to: " + X.getAbsolutePath());
else if (list)
System.out.println(loadTextFile(new File(X, "main.java").getPath(), null));
else
javax2(X, ioDir, false, false, libraries);
}

private static File defaultTranslate(File x, List<File> libraries_out) throws Exception {
x = luaPrintToJavaPrint(x);
x = autoTranslate(x, libraries_out);
return x;
}

private static File autoTranslate(File x, List<File> libraries_out) throws Exception {
String main = loadTextFile(new File(x, "main.java").getPath(), null);
List<String> lines = toLines(main);
List<String> translators = findTranslators(lines);
if (translators.isEmpty())
return x;

main = fromLines(lines);
File newDir = TempDirMaker_make();
saveTextFile(new File(newDir, "main.java").getPath(), main);
return applyTranslators(newDir, translators, libraries_out);
}

private static List<String> findTranslators(List<String> lines) {
List<String> translators = new ArrayList<String>();
Pattern pattern = Pattern.compile("^!([0-9# \t]+)");
for (ListIterator<String> iterator = lines.listIterator(); iterator.hasNext(); ) {
String line = iterator.next();
line = line.trim();
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
translators.addAll(Arrays.asList(matcher.group(1).split("[ \t]+")));
iterator.remove();
}
}
return translators;
}

public static List<String> toLines(String s) {
List<String> lines = new ArrayList<String>();
int start = 0;
while (true) {
int i = toLines_nextLineBreak(s, start);
if (i < 0) {
if (s.length() > start) lines.add(s.substring(start));
break;
}

lines.add(s.substring(start, i));
if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
i += 2;
else
++i;

start = i;
}
return lines;
}

private static int toLines_nextLineBreak(String s, int start) {
for (int i = start; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '\r' || c == '\n')
return i;
}
return -1;
}

public static String fromLines(List<String> lines) {
StringBuilder buf = new StringBuilder();
for (String line : lines) {
buf.append(line).append('\n');
}
return buf.toString();
}

private static File applyTranslators(File x, List<String> translators, List<File> libraries_out) throws Exception {
for (String translator : translators)
x = applyTranslator(x, translator, libraries_out);
return x;
}

// also takes a library
private static File applyTranslator(File x, String translator, List<File> libraries_out) throws Exception {
if (verbose)
System.out.println("Using translator " + translator + " on sources in " + x.getPath());

File newDir = runTranslatorOnInput(translator, null, x, !verbose, libraries_out);

if (!new File(newDir, "main.java").exists()) {
throw new Exception("Translator " + translator + " did not generate main.java");
// TODO: show translator output
}
if (verbose)
System.out.println("Translated with " + translator + " from " + x.getPath() + " to " + newDir.getPath());
x = newDir;
return x;
}

private static File luaPrintToJavaPrint(File x) throws IOException {
File newDir = TempDirMaker_make();
String code = loadTextFile(new File(x, "main.java").getPath(), null);
code = luaPrintToJavaPrint(code);
if (verbose)
System.out.println(code);
saveTextFile(new File(newDir, "main.java").getPath(), code);
return newDir;
}

public static String luaPrintToJavaPrint(String code) {
return ("\n" + code).replaceAll(
"(\n\\s*)print (\".*\")",
"$1System.out.println($2);").substring(1);
}

public static File loadSnippetAsMainJava(String snippetID) throws IOException {
File srcDir = TempDirMaker_make();
saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippet(snippetID));
return srcDir;
}

public static File loadSnippetAsMainJavaVerified(String snippetID, String hash) throws IOException {
File srcDir = TempDirMaker_make();
saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippetVerified(snippetID, hash));
return srcDir;
}

/** returns output dir */
private static File runTranslatorOnInput(String snippetID, String hash, File input, boolean silent,
List<File> libraries_out) throws Exception {
File libraryFile = DiskSnippetCache_getLibrary(parseSnippetID(snippetID));
if (libraryFile != null) {
loadLibrary(snippetID, libraries_out, libraryFile);
return input;
}

File srcDir = hash == null ? loadSnippetAsMainJava(snippetID)
: loadSnippetAsMainJavaVerified(snippetID, hash);

long mainJavaSize = new File(srcDir, "main.java").length();

if (mainJavaSize == 0) { // no text in snippet? assume it's a library
loadLibrary(snippetID, libraries_out, libraryFile);
return input;
}

List<File> libraries = new ArrayList<File>();
srcDir = defaultTranslate(srcDir, libraries);
boolean runInProcess = false;
File ioBaseDir = TempDirMaker_make();

if (virtualizeTranslators) {
if (verbose) System.out.println("Virtualizing translator");

//srcDir = applyTranslator(srcDir, "#2000351"); // I/O-virtualize the translator
// that doesn't work because it recurses infinitely...

// So we do it right here:
String s = loadTextFile(new File(srcDir, "main.java").getPath(), null);
s = s.replaceAll("new\\s+File\\(", "virtual.newFile(");
s = s.replaceAll("new\\s+FileInputStream\\(", "virtual.newFileInputStream(");
s = s.replaceAll("new\\s+FileOutputStream\\(", "virtual.newFileOutputStream(");
s += "\n\n" + loadSnippet("#2000355"); // load class virtual

// change baseDir
s = s.replace("virtual_baseDir = \"\";",
"virtual_baseDir = " + javaQuote(ioBaseDir.getAbsolutePath()) + ";");

// forward snippet cache
s = s.replace("static File DiskSnippetCache_dir;",
"static File DiskSnippetCache_dir = new File(" + javaQuote(DiskSnippetCache_dir.getAbsolutePath()) + ");");
s = s.replace("static boolean preferCached = false;", "static boolean preferCached = true;");

if (verbose) {
System.out.println("==BEGIN VIRTUALIZED TRANSLATOR==");
System.out.println(s);
System.out.println("==END VIRTUALIZED TRANSLATOR==");
}
saveTextFile(new File(srcDir, "main.java").getPath(), s);

// TODO: silence translator also
runInProcess = true;
}

return runJavaX(ioBaseDir, srcDir, input, silent, runInProcess, libraries);
}

private static void loadLibrary(String snippetID, List<File> libraries_out, File libraryFile) throws IOException {
if (verbose)
System.out.println("Assuming " + snippetID + " is a library.");

if (libraryFile == null) {
byte[] data;
try {
URL url = new URL("http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + parseSnippetID(snippetID)
+ "&contentType=application/binary");
System.err.println("Loading library: " + url);
data = loadBinaryPage(url.openConnection(), url);
if (verbose)
System.err.println("Bytes loaded: " + data.length);
} catch (FileNotFoundException e) {
throw new IOException("Binary snippet #" + snippetID + " not found or not public");
}
DiskSnippetCache_putLibrary(parseSnippetID(snippetID), data);
libraryFile = DiskSnippetCache_getLibrary(parseSnippetID(snippetID));
}

if (!libraries_out.contains(libraryFile))
libraries_out.add(libraryFile);
}

/** returns output dir */
private static File runJavaX(File ioBaseDir, File originalSrcDir, File originalInput,
boolean silent, boolean runInProcess,
List<File> libraries) throws Exception {
File srcDir = new File(ioBaseDir, "src");
File inputDir = new File(ioBaseDir, "input");
File outputDir = new File(ioBaseDir, "output");
copyInput(originalSrcDir, srcDir);
copyInput(originalInput, inputDir);
javax2(srcDir, ioBaseDir, silent, runInProcess, libraries);
return outputDir;
}

private static void copyInput(File src, File dst) throws IOException {
copyDirectory(src, dst);
}

public static boolean hasFile(File inputDir, String name) {
return new File(inputDir, name).exists();
}

public static void copyDirectory(File src, File dst) throws IOException {
if (verbose) System.out.println("Copying " + src.getAbsolutePath() + " to " + dst.getAbsolutePath());
dst.mkdirs();
File[] files = src.listFiles();
if (files == null) return;
for (File file : files) {
File dst1 = new File(dst, file.getName());
if (file.isDirectory())
copyDirectory(file, dst1);
else {
if (verbose) System.out.println("Copying " + file.getAbsolutePath() + " to " + dst1.getAbsolutePath());
copy(file, dst1);
}
}
}

/** Quickly copy a file without a progress bar or any other fancy GUI... :) */
public static void copy(File src, File dest) throws IOException {
FileInputStream inputStream = new FileInputStream(src);
FileOutputStream outputStream = new FileOutputStream(dest);
try {
copy(inputStream, outputStream);
inputStream.close();
} finally {
outputStream.close();
}
}

public static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[65536];
while (true) {
int n = in.read(buf);
if (n <= 0) return;
out.write(buf, 0, n);
}
}

/** writes safely (to temp file, then rename) */
public static void saveTextFile(String fileName, String contents) throws IOException {
File file = new File(fileName);
File parentFile = file.getParentFile();
if (parentFile != null)
parentFile.mkdirs();
String tempFileName = fileName + "_temp";
FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles);
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
printWriter.print(contents);
printWriter.close();
if (file.exists() && !file.delete())
throw new IOException("Can't delete " + fileName);

if (!new File(tempFileName).renameTo(file))
throw new IOException("Can't rename " + tempFileName + " to " + fileName);
}

/** writes safely (to temp file, then rename) */
public static void saveBinaryFile(String fileName, byte[] contents) throws IOException {
File file = new File(fileName);
File parentFile = file.getParentFile();
if (parentFile != null)
parentFile.mkdirs();
String tempFileName = fileName + "_temp";
FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
fileOutputStream.write(contents);
fileOutputStream.close();
if (file.exists() && !file.delete())
throw new IOException("Can't delete " + fileName);

if (!new File(tempFileName).renameTo(file))
throw new IOException("Can't rename " + tempFileName + " to " + fileName);
}

public static String loadTextFile(String fileName, String defaultContents) throws IOException {
if (!new File(fileName).exists())
return defaultContents;

FileInputStream fileInputStream = new FileInputStream(fileName);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles);
return loadTextFile(inputStreamReader);
}

public static String loadTextFile(Reader reader) throws IOException {
StringBuilder builder = new StringBuilder();
try {
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null)
builder.append(line).append('\n');
} finally {
reader.close();
}
return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
}

static File DiskSnippetCache_dir;

public static void initDiskSnippetCache(File dir) {
DiskSnippetCache_dir = dir;
dir.mkdirs();
}

// Data files are immutable, use centralized cache
public static synchronized File DiskSnippetCache_getLibrary(long snippetID) throws IOException {
File file = new File(getGlobalCache(), "data_" + snippetID + ".jar");
if (verbose)
System.out.println("Checking data cache: " + file.getPath());
return file.exists() ? file : null;
}

public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
}

private static File DiskSnippetCache_getFile(long snippetID) {
return new File(DiskSnippetCache_dir, "" + snippetID);
}

public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
}

public static synchronized void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException {
saveBinaryFile(new File(getGlobalCache(), "data_" + snippetID).getPath() + ".jar", data);
}

public static File DiskSnippetCache_getDir() {
return DiskSnippetCache_dir;
}

public static void initSnippetCache() {
if (DiskSnippetCache_dir == null)
initDiskSnippetCache(getGlobalCache());
}

private static File getGlobalCache() {
File file = new File(System.getProperty("user.home"), ".tinybrain/snippet-cache");
file.mkdirs();
return file;
}

public static String loadSnippetVerified(String snippetID, String hash) throws IOException {
String text = loadSnippet(snippetID);
String realHash = getHash(text.getBytes("UTF-8"));
if (!realHash.equals(hash)) {
String msg;
if (hash.isEmpty())
msg = "Here's your hash for " + snippetID + ", please put in your program: " + realHash;
else
msg = "Hash mismatch for " + snippetID + ": " + realHash + " (new) vs " + hash + " - has tinybrain.de been hacked??";
throw new RuntimeException(msg);
}
return text;
}

public static String getHash(byte[] data) {
return bytesToHex(getFullFingerprint(data));
}

public static byte[] getFullFingerprint(byte[] data) {
try {
return java.security.MessageDigest.getInstance("MD5").digest(data);
} catch (java.security.NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

public static String bytesToHex(byte[] bytes) {
return bytesToHex(bytes, 0, bytes.length);
}

public static String bytesToHex(byte[] bytes, int ofs, int len) {
StringBuilder stringBuilder = new StringBuilder(len*2);
for (int i = 0; i < len; i++) {
String s = "0" + Integer.toHexString(bytes[ofs+i]);
stringBuilder.append(s.substring(s.length()-2, s.length()));
}
return stringBuilder.toString();
}

public static String loadSnippet(String snippetID) throws IOException {
return loadSnippet(parseSnippetID(snippetID));
}

public static long parseSnippetID(String snippetID) {
return Long.parseLong(shortenSnippetID(snippetID));
}

private static String shortenSnippetID(String snippetID) {
if (snippetID.startsWith("#"))
snippetID = snippetID.substring(1);
String httpBlaBla = "http://tinybrain.de/";
if (snippetID.startsWith(httpBlaBla))
snippetID = snippetID.substring(httpBlaBla.length());
return snippetID;
}

public static boolean isSnippetID(String snippetID) {
snippetID = shortenSnippetID(snippetID);
return isInteger(snippetID) && Long.parseLong(snippetID) != 0;
}

public static boolean isInteger(String s) {
return Pattern.matches("\\-?\\d+", s);
}

public static String loadSnippet(long snippetID) throws IOException {
String text = memSnippetCache.get(snippetID);
if (text != null)
return text;

if (preferCached) {
initSnippetCache();
text = DiskSnippetCache_get(snippetID);
if (text != null)
return text;
}

try {
URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID);
text = loadPage(url);
} catch (FileNotFoundException e) {
throw new IOException("Snippet #" + snippetID + " not found or not public");
}

memSnippetCache.put(snippetID, text);

try {
initSnippetCache();
DiskSnippetCache_put(snippetID, text);
} catch (IOException e) {
System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")");
}

return text;
}

private static String loadPage(URL url) throws IOException {
System.err.println("Loading: " + url.toExternalForm());
URLConnection con = url.openConnection();
return loadPage(con, url);
}

public static String loadPage(URLConnection con, URL url) throws IOException {
String contentType = con.getContentType();
if (contentType == null)
throw new IOException("Page could not be read: " + url);
//Log.info("Content-Type: " + contentType);
String charset = guessCharset(contentType);
Reader r = new InputStreamReader(con.getInputStream(), charset);
StringBuilder buf = new StringBuilder();
while (true) {
int ch = r.read();
if (ch < 0)
break;
//Log.info("Chars read: " + buf.length());
buf.append((char) ch);
}
return buf.toString();
}

public static byte[] loadBinaryPage(URLConnection con, URL url) throws IOException {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
InputStream inputStream = con.getInputStream();
while (true) {
int ch = inputStream.read();
if (ch < 0)
break;
buf.write(ch);
}
inputStream.close();
return buf.toByteArray();
}

public static String guessCharset(String contentType) {
Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
Matcher m = p.matcher(contentType);
/* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */
return m.matches() ? m.group(1) : "ISO-8859-1";
}

/** runs a transpiled set of sources */
public static void javax2(File srcDir, File ioBaseDir, boolean silent, boolean runInProcess,
List<File> libraries) throws Exception {
// collect sources

List<File> sources = new ArrayList<File>();
if (verbose) System.out.println("Scanning for sources in " + srcDir.getPath());
scanForSources(srcDir, sources, true);
if (sources.isEmpty()) {
System.out.println("No sources found");
return;
}

// compile

File optionsFile = File.createTempFile("javax", "");
File classesDir = TempDirMaker_make();
if (verbose) System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath());
String options = "-d " + bashQuote(classesDir.getPath());
writeOptions(sources, libraries, optionsFile, options);
classesDir.mkdirs();
String javacOutput = invokeJavac(optionsFile);

// run

if (verbose) System.out.println("Running program (" + srcDir.getAbsolutePath()
+ ") on io dir " + ioBaseDir.getAbsolutePath() + (runInProcess ? "[in-process]" : "") + "\n");
runProgram(javacOutput, classesDir, ioBaseDir, silent, runInProcess, libraries);
}

private static void runProgram(String javacOutput, File classesDir, File ioBaseDir,
boolean silent, boolean runInProcess,
List<File> libraries) throws Exception {
// print javac output if compile failed and it hasn't been printed yet
boolean didNotCompile = !hasFile(classesDir, "main.class");
if (verbose || didNotCompile)
System.out.println(javacOutput);
if (didNotCompile)
return;

if (runInProcess
|| (ioBaseDir.getAbsolutePath().equals(new File(".").getAbsolutePath()) && !silent)) {
runProgramQuick(classesDir, libraries);
return;
}

boolean echoOK = false;
// TODO: add libraries to class path
String bashCmd = "(cd " + bashQuote(ioBaseDir.getAbsolutePath()) + " && (java -cp "
+ bashQuote(classesDir.getAbsolutePath()) + " main" + (echoOK ? "; echo ok" : "") + "))";
if (verbose) System.out.println(bashCmd);
String output = backtick(bashCmd);
if (verbose || !silent)
System.out.println(output);
}

private static void runProgramQuick(File classesDir, List<File> libraries) throws Exception {
// collect urls
URL[] urls = new URL[libraries.size()+1];
urls[0] = classesDir.toURI().toURL();
for (int i = 0; i < libraries.size(); i++)
urls[i+1] = libraries.get(i).toURI().toURL();

// make class loader
URLClassLoader classLoader = new URLClassLoader(urls);

// load JavaX main class
Class<?> mainClass = classLoader.loadClass("main");

// run main method
java.lang.reflect.Method main = mainClass.getMethod("main", String[].class);
main.invoke(null, (Object) new String[0]);
}

private static String invokeJavac(File optionsFile) throws IOException {
String output;
try {
output = invokeEcj(optionsFile);
} catch (NoClassDefFoundError e) {
if (verbose) {
System.err.println("ecj not found - using javac");
e.printStackTrace();
}
output = backtick("javac " + bashQuote("@" + optionsFile.getPath()));
}
if (verbose) System.out.println(output);
return output;
}

// throws ClassNotFoundError if ecj is not in classpath
static String invokeEcj(File optionsFile) {
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
org.eclipse.jdt.core.compiler.CompilationProgress progress = null;
org.eclipse.jdt.core.compiler.batch.BatchCompiler.compile(
new String[] { "@" + optionsFile.getPath(), "-source", "1.7" },
printWriter,
printWriter,
progress);
return writer.toString();
}

private static void writeOptions(List<File> sources, List<File> libraries,
File optionsFile, String moreOptions) throws IOException {
FileWriter writer = new FileWriter(optionsFile);
for (File source : sources)
writer.write(bashQuote(source.getPath()) + " ");
if (!libraries.isEmpty()) {
List<String> cp = new ArrayList<String>();
for (File lib : libraries)
cp.add(lib.getAbsolutePath());
writer.write("-cp " + bashQuote(join(File.pathSeparator, cp)) + " ");
}
writer.write(moreOptions);
writer.close();
}

private static void scanForSources(File source, List<File> sources, boolean topLevel) {
if (source.isFile() && source.getName().endsWith(".java"))
sources.add(source);
else if (source.isDirectory() && !isSkippedDirectoryName(source.getName(), topLevel)) {
File[] files = source.listFiles();
for (File file : files)
scanForSources(file, sources, false);
}
}

private static boolean isSkippedDirectoryName(String name, boolean topLevel) {
if (topLevel) return false; // input or output ok as highest directory (intentionally specified by user, not just found by a directory scan in which case we probably don't want it. it's more like heuristics actually.)
return name.equalsIgnoreCase("input") || name.equalsIgnoreCase("output");
}

public static String backtick(String cmd) throws IOException {
++processesStarted;
File outFile = File.createTempFile("_backtick", "");
File scriptFile = File.createTempFile("_backtick", isWindows() ? ".bat" : "");

String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1";
//Log.info("[Backtick] " + command);
try {
saveTextFile(scriptFile.getPath(), command);
String[] command2;
if (isWindows())
command2 = new String[] { scriptFile.getPath() };
else
command2 = new String[] { "/bin/bash", scriptFile.getPath() };
Process process = Runtime.getRuntime().exec(command2);
try {
process.waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
process.exitValue();
return loadTextFile(outFile.getPath(), "");
} finally {
scriptFile.delete();
}
}

/** possibly improvable */
public static String javaQuote(String text) {
return bashQuote(text);
}

/** possibly improvable */
public static String bashQuote(String text) {
if (text == null) return null;
return "\"" + text
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r") + "\"";
}

public final static String charsetForTextFiles = "UTF8";

static long TempDirMaker_lastValue;

public static File TempDirMaker_make() {
File dir = new File(System.getProperty("user.home"), ".javax/" + TempDirMaker_newValue());
dir.mkdirs();
return dir;
}

private static long TempDirMaker_newValue() {
long value;
do
value = System.currentTimeMillis();
while (value == TempDirMaker_lastValue);
TempDirMaker_lastValue = value;
return value;
}

public static String join(String glue, Iterable<String> strings) {
StringBuilder buf = new StringBuilder();
Iterator<String> i = strings.iterator();
if (i.hasNext()) {
buf.append(i.next());
while (i.hasNext())
buf.append(glue).append(i.next());
}
return buf.toString();
}

public static boolean isWindows() {
return System.getProperty("os.name").contains("Windows");
}
}

}}
2015-08-18 00:53:03

add comment

Snippet ID: #2000360
Snippet name: x12.java
Eternal ID of this version: #2000360/1
Text MD5: df28fc376d3a6c79798fa2b60b5a85ce
Author: stefan
Category: javax
Type: New Tinybrain snippet
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-05-21 18:15:35
Source code size: 29549 bytes / 814 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 933 / 468
Referenced in: [show references]