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

487
LINES

< > BotCompany Repo | #594 // x7.java

Java source code

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
  Changes to v6:
    -v2mode stuff removed
    -compiler errors are automatically printed if compilation fails
    
  Still Linux only. (Where are the Windows porters?)
 */

public class x7 {
  static boolean verbose = false;

  static List<String> translators = new ArrayList<String>();

  public static void main(String[] args) throws IOException {
    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"))
        verbose = true;
      else if (arg.equals("-finderror"))
        verbose = true;
      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"))
        translators.add(args[++i]);
      else
        src = arg;
    }

    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);

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

  public static void javax4(String src, File ioDir) throws IOException {
    File srcDir;
    if (isSnippetID(src))
      srcDir = loadSnippetAsMainJava(src);
    else
      srcDir = new File(src);
    File X = programToInput(srcDir);

    X = applyTranslators(X);

    File Y = luaPrintToJavaPrint(X);
    javax2(Y, ioDir, false);
  }

  private static File applyTranslators(File x) throws IOException {
    for (String translator : translators) {
      if (verbose)
        System.out.println("Using translator " + translator + " on sources in " + x.getPath());
      File newDir = runJavaX2_src_from_snippet(translator, null, x, true);
      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, false));
    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 runJavaX2_src_from_snippet(String snippetID, String hash, File input, boolean silent) throws IOException {
    File srcDir = hash == null ? loadSnippetAsMainJava(snippetID)
      : loadSnippetAsMainJavaVerified(snippetID, hash);
    return runJavaX(srcDir, input, silent);
  }

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

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

  private static File programToInput(File srcDir) {
    return srcDir;
  }

  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);
  }

  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();
  }

  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 File DiskSnippetCache_getDir() {
    return DiskSnippetCache_dir;
  }

  public static void initSnippetCache() {
    if (DiskSnippetCache_dir == null)
      initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"));
  }

  public static String loadSnippetVerified(String snippetID, String hash) throws IOException {
    String text = loadSnippet(snippetID, !hash.isEmpty());
    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 MessageDigest.getInstance("MD5").digest(data);
    } catch (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, boolean preferCached) throws IOException {
    return loadSnippet(parseSnippetID(snippetID), preferCached);
  }

  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, boolean preferCached) throws IOException {
    if (preferCached) {
      initSnippetCache();
      String text = DiskSnippetCache_get(snippetID);
      if (text != null)
        return text;
    }

    String 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");
    }

    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.out.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 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";
  }

  public static void javax2(File srcDir, File ioBaseDir, boolean silent) throws IOException {
    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;
    }
    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, optionsFile, options);
    classesDir.mkdirs();
    String javacOutput = invokeJavac(optionsFile);
    if (verbose) System.out.println("Running program (class main.java)\n");
    runProgram(javacOutput, classesDir, ioBaseDir, silent);
  }

  private static void runProgram(String javacOutput, File classesDir, File ioBaseDir,
                                 boolean silent) throws IOException {
    // print javac output if compile failed and it hasn't been printed yet
    if (!verbose && !hasFile(classesDir, "main.class"))
      System.out.println(javacOutput);

    boolean echoOK = false;
    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 (!silent)
      System.out.println(output);
  }

  private static String invokeJavac(File optionsFile) throws IOException {
    String javacOutput = backtick("javac " + bashQuote("@" + optionsFile.getPath()));
    if (verbose) System.out.println(javacOutput);
    return javacOutput;
  }

  private static void writeOptions(List<File> sources, File sourcesFile, String moreOptions) throws IOException {
    FileWriter writer = new FileWriter(sourcesFile);
    for (File source : sources)
      writer.write(bashQuote(source.getPath()) + " ");
    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 {
    File outFile = File.createTempFile("_backtick", "");
    File scriptFile = File.createTempFile("_backtick", "");

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

  /** 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;
  }
}

download  show line numbers   

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

Comments [hide]

ID Author/Program Comment Date
402 #1000610 Edit suggestion:
!636
!629

main {
static Object androidContext;
static String programID;

public static void main(String[] args) throws Exception {
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
Changes to v6:
-v2mode stuff removed
-compiler errors are automatically printed if compilation fails

Still Linux only. (Where are the Windows porters?)
*/

public class x7 {
static boolean verbose = false;

static List<String> translators = new ArrayList<String>();

public static void main(String[] args) throws IOException {
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"))
verbose = true;
else if (arg.equals("-finderror"))
verbose = true;
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"))
translators.add(args[++i]);
else
src = arg;
}

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);

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

public static void javax4(String src, File ioDir) throws IOException {
File srcDir;
if (isSnippetID(src))
srcDir = loadSnippetAsMainJava(src);
else
srcDir = new File(src);
File X = programToInput(srcDir);

X = applyTranslators(X);

File Y = luaPrintToJavaPrint(X);
javax2(Y, ioDir, false);
}

private static File applyTranslators(File x) throws IOException {
for (String translator : translators) {
if (verbose)
System.out.println("Using translator " + translator + " on sources in " + x.getPath());
File newDir = runJavaX2_src_from_snippet(translator, null, x, true);
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, false));
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 runJavaX2_src_from_snippet(String snippetID, String hash, File input, boolean silent) throws IOException {
File srcDir = hash == null ? loadSnippetAsMainJava(snippetID)
: loadSnippetAsMainJavaVerified(snippetID, hash);
return runJavaX(srcDir, input, silent);
}

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

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

private static File programToInput(File srcDir) {
return srcDir;
}

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);
}

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();
}

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 File DiskSnippetCache_getDir() {
return DiskSnippetCache_dir;
}

public static void initSnippetCache() {
if (DiskSnippetCache_dir == null)
initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"));
}

public static String loadSnippetVerified(String snippetID, String hash) throws IOException {
String text = loadSnippet(snippetID, !hash.isEmpty());
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 MessageDigest.getInstance("MD5").digest(data);
} catch (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, boolean preferCached) throws IOException {
return loadSnippet(parseSnippetID(snippetID), preferCached);
}

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, boolean preferCached) throws IOException {
if (preferCached) {
initSnippetCache();
String text = DiskSnippetCache_get(snippetID);
if (text != null)
return text;
}

String 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");
}

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.out.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 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";
}

public static void javax2(File srcDir, File ioBaseDir, boolean silent) throws IOException {
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;
}
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, optionsFile, options);
classesDir.mkdirs();
String javacOutput = invokeJavac(optionsFile);
if (verbose) System.out.println("Running program (class main.java)\n");
runProgram(javacOutput, classesDir, ioBaseDir, silent);
}

private static void runProgram(String javacOutput, File classesDir, File ioBaseDir,
boolean silent) throws IOException {
// print javac output if compile failed and it hasn't been printed yet
if (!verbose && !hasFile(classesDir, "main.class"))
System.out.println(javacOutput);

boolean echoOK = false;
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 (!silent)
System.out.println(output);
}

private static String invokeJavac(File optionsFile) throws IOException {
String javacOutput = backtick("javac " + bashQuote("@" + optionsFile.getPath()));
if (verbose) System.out.println(javacOutput);
return javacOutput;
}

private static void writeOptions(List<File> sources, File sourcesFile, String moreOptions) throws IOException {
FileWriter writer = new FileWriter(sourcesFile);
for (File source : sources)
writer.write(bashQuote(source.getPath()) + " ");
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 {
File outFile = File.createTempFile("_backtick", "");
File scriptFile = File.createTempFile("_backtick", "");

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

/** 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;
}
}

}}
2015-08-18 12:29:31  delete 
400 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #594
Snippet name: x7.java
Eternal ID of this version: #594/1
Text MD5: a16b96d171108ac4c34f608cce46f12d
Author: stefan
Category: javax
Type: Java source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-04-28 19:01:48
Source code size: 17606 bytes / 487 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 912 / 145
Referenced in: [show references]