import javax.imageio.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.*;
import java.security.NoSuchAlgorithmException;
import java.security.MessageDigest;
import java.lang.management.*;
import java.lang.reflect.*;
import java.net.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.List;
import java.util.zip.*;
import java.util.*;


public class main {
  static volatile long beatCount;
  static volatile int delay = 1000;
  static long booted;
  static volatile boolean loud = true;
  static int jumpEvery = 10;
  
  // We alternate between two machines as a test.
  static String machine1 = "teubizvjbppd";
  static String machine2 = "dhtvkmknsjym";
  
  public static void main(String[] args) throws Exception {
    booted = now();
    readLocally("beatCount");
    makeAndroid3("Heart.");
    print("Beat count " + beatCount + ". Beating at 1/" + delay + " ms...");
    
    while (true) {
      if (loud)
        print("beep!");
      ++beatCount;
      saveLocally("beatCount");
      if ((beatCount % jumpEvery) == 0) {
        print("Now I should be moved!");
        hyperMove(machine1, machine2);
        System.exit(0);
      }
      sleep(delay);
    }
  }
  
  static synchronized String answer(String s, List<String> history) {
    if (match3("get beat count", s))
      return "" + beatCount;
    if (match3("get delay", s))
      return "" + delay;
    if (match3("booted when", s))
      return "" + booted;
      
    return null;
  }

static void hyperMove(String machine1, String machine2) { try {
 
  String botID = formatSnippetID(getProgramID());

  String destMachine = getComputerID().equals(machine2) ? machine1 : machine2;
  print("Hyper-Move: Jumping to " + destMachine + "...");
  
  File zipPath = new File(getProgramDir(), botID + ".zip");
  zipBot(botID, zipPath);
  
  String title = "Data zip of " + botID + " on " + computerID() + " at " + now();
  String gummipw = getProgramID() + "-hypermove";
  String dataID = uploadDataSuperHigh(gummipw, title, zipPath);
  
  sendToRemoteBot("Identity Manager", format3("* is now in state * with data * to machine *", botID, "moving", dataID, destMachine));
  
  print("\nDone. Data ID: " + dataID);

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}



  static abstract class DialogIO {
    abstract boolean isStillConnected();
    abstract String readLineNoBlock();
    abstract boolean waitForLine();
    abstract void sendLine(String line);
    abstract boolean isLocalConnection();
    abstract Socket getSocket();
    abstract void close();
    
    String readLine() {
      waitForLine();
      return readLineNoBlock();
    }
  }
  
  static abstract class DialogHandler {
    abstract void run(DialogIO io);
  } // Dialog classes


static class Android3 {
  String greeting;
  boolean publicOverride; // optionally set this in client
  int startPort = 5000; // optionally set this in client
  Responder responder;
  boolean console = true;
  
  // set by system
  int port;
  DialogHandler handler;
  
  Android3(String greeting) {
  this.greeting = greeting;}
  Android3() {}
}

static abstract class Responder {
  abstract String answer(String s, List<String> history);
}

static Android3 makeAndroid3(final String greeting) {
  return makeAndroid3(new Android3(greeting));
}

static String makeAndroid3_callStaticAnswerMethod(String s, List<String> history) {
  String answer = (String) callOpt(getMainClass(), "answer", s, history);
  if (answer == null)
    answer = (String) callOpt(getMainClass(), "answer", s);
  return answer;
}

static Android3 makeAndroid3(final Android3 a) {
  if (a.responder == null)
    a.responder = new Responder() {
      String answer(String s, List<String> history) {
        return makeAndroid3_callStaticAnswerMethod(s, history);
      }
    };
   
  print(a.greeting);
  a.handler = makeAndroid3_makeDialogHandler(a);
  a.port = startDialogServerOnPortAbove(a.startPort, a.handler);

  if (a.console) {
    // Console handling stuff
    print("You may also type on this console.");
    Thread _t_0 = new Thread() {
public void run() {
try {

      List<String> history = new ArrayList<String>();
      String line;
      while ((line = readLine()) != null) {
        if ("bye".equals(line)) {
          print("> bye stranger");
          history = new ArrayList<String>();
        } else {
          history.add(line);
          history.add(makeAndroid3_getAnswer(line, history, a)); // prints answer on console too
        }
      }
    } catch (Exception _e) {
  throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } }
};
_t_0.start();
  }
  
  record(a);
  return a;
}

static DialogHandler makeAndroid3_makeDialogHandler(final Android3 a) {
  return new DialogHandler() {
public void run(final DialogIO io) {

    if (!a.publicOverride && !(publicCommOn() || io.isLocalConnection())) {
      io.sendLine("Sorry, not allowed");
      return;
    }
    
    String dialogID = randomID(8);
    
    io.sendLine(a.greeting + " / Your ID: " + dialogID);
    
    List<String> history = new ArrayList<String>();
    
    while (io.isStillConnected()) {
      if (io.waitForLine()) {
        final String line = io.readLineNoBlock();
        String s = dialogID + " at " + now() + ": " + quote(line);
        print(s);
        if ("bye".equals(line)) {
          io.sendLine("bye stranger");
          return;
        }
        Matches m = new Matches();
        history.add(line);
        String answer;
        if (match3("this is a continuation of talk *", s, m)
          || match3("hello bot! this is a continuation of talk *", s, m)) {
          dialogID = unquote(m.m[0]);
          answer = "ok";
        } else
          answer = makeAndroid3_getAnswer(line, history, a);
        history.add(answer);
        io.sendLine(answer);
        //appendToLog(logFile, s);
      }
    }
  
}};
}

static String makeAndroid3_getAnswer(String line, List<String> history, Android3 a) {
  String answer;
  try {
    answer = makeAndroid3_fallback(line, history, a.responder.answer(line, history));
  } catch (Throwable e) {
    e = getInnerException(e);
    e.printStackTrace();
    answer = e.toString();
  }
  print("> " + answer);
  return answer;
}

static String makeAndroid3_fallback(String s, List<String> history, String answer) {
  // Now we only do the safe thing instead of VM inspection - give out our process ID
  if (answer == null && match3("what is your pid", s))
    answer = getPID();
    
  if (answer == null) answer = "?";
  if (answer.indexOf('\n') >= 0 || answer.indexOf('\r') >= 0)
    answer = quote(answer);
  return answer;
}


  static class Matches { String[] m; }
  
  static boolean match3(String pat, String s) {
    return match3(pat, s, null);
  }
  
  static boolean match3(String pat, String s, Matches matches) {
    List<String> tokpat = parse3(pat), toks = parse3(s);
    String[] m = match2(tokpat, toks);
    //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
    if (m == null)
      return false;
    else {
      if (matches != null) matches.m = m;
      return true;
    }
  }


static String programID;

static synchronized void saveLocally(String variableName) {
  File textFile = new File(programDir(programID), variableName + ".text");
  File structureFile = new File(programDir(programID), variableName + ".structure");
  Object x = get(main.class, variableName);
  
  if (x == null) {
    textFile.delete();
    structureFile.delete();
  } else if (x instanceof String) {
    structureFile.delete();
    saveTextFile(textFile, (String) x);
  } else {
    textFile.delete();
    saveTextFile(structureFile, structure(x));
  }
}
static synchronized void readLocally(String varNames) {
  for (String variableName : codeTokensOnly(javaTok(varNames))) {
  File textFile = new File(programDir(programID), variableName + ".text");
  File structureFile = new File(programDir(programID), variableName + ".structure");
  
  String value = loadTextFile(textFile);
  if (value != null)
    set(main.class, variableName, value);
  else {
    value = loadTextFile(structureFile);
    if (value != null)
    set(main.class, variableName, unstructure(value));
  }
  }
}


static long now_virtualTime;
static long now() {
  return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}


static void sleep(long ms) {
  try {
    Thread.sleep(ms);
  } catch (Exception e) { throw new RuntimeException(e); }
}

static void sleep() { try {
 
  synchronized(main.class) { main.class.wait(); }

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static void print() {
  System.out.println();
}

static void print(Object o) {
  System.out.println(o);
}

static void print(long i) {
  System.out.println(i);
}

static int zipBot_compressionLevel = 9; // highest!

static void zipBot(String botToZip, File destZip) { try {
 
  File dir = getProgramDir(botToZip);
  
  mkdirsForFile(destZip);
  FileOutputStream fout = new FileOutputStream(destZip);
  ZipOutputStream zout = new ZipOutputStream(fout);
  zout.setLevel(zipBot_compressionLevel);
  int count = 0;
  if (dir.isDirectory())
    count += dir2zip(dir, zout, "JavaX-Data/" + botToZip + "/");
  if (count == 0) {
    print("Nothing to zip for bot " + botToZip);
    fout.close();
  } else {
    zout.close();
    print("Zipped " + botToZip + " (" + count + " entries) to " + destZip.getAbsolutePath() + " (" + destZip.length() + " bytes)");
  }

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static String programID() {
  return getProgramID();
}

static File getProgramDir() {
  return programDir();
}

static File getProgramDir(String snippetID) {
  return programDir(snippetID);
}

  static String sendToRemoteBot(String botName, String line) {
    //print("Sending to remote bot " + quote(botName) + ": " + quote(shorten(line, 100)));
    
    DialogIO io = talkTo("second.tinybrain.de", 4999);
    io.waitForLine();
    String greeting = io.readLineNoBlock();
    
    String s = format3("Please forward to bot *: *" , botName, line);
    print(">> " + s);
    io.sendLine(s);
    io.waitForLine();
    String answer = io.readLineNoBlock();
    io.close();
    return answer;
  }

  static String format3(String pat, Object... args) {
    List<String> tok = javaTok(pat);
    int argidx = 0;
    for (int i = 1; i < tok.size(); i += 2)
      if (tok.get(i).equals("*"))
        tok.set(i, format3_formatArg(argidx < args.length ? args[argidx++] : "null"));
    return join(tok);
  }
  
  static String format3_formatArg(Object arg) {
    if (arg == null) return "null";
    if (arg instanceof String) return quote((String) arg);
    return quote(structure(arg));
  }
  


  static String quote(String s) {
    if (s == null) return "null";
    return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\r", "\\r").replace("\n", "\\n") + "\"";
  }
  
  static String quote(long l) {
    return quote("" + l);
  }


  static Object callOpt(Object o, String method, Object... args) {
    try {
      if (o instanceof Class) {
        Method m = callOpt_findStaticMethod((Class) o, method, args, false);
        if (m == null) return null;
        m.setAccessible(true);
        return m.invoke(null, args);
      } else {
        Method m = callOpt_findMethod(o, method, args, false);
        if (m == null) return null;
        m.setAccessible(true);
        return m.invoke(o, args);
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  static Method callOpt_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
    Class _c = c;
    while (c != null) {
      for (Method m : c.getDeclaredMethods()) {
        if (debug)
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
        if (!m.getName().equals(method)) {
          if (debug) System.out.println("Method name mismatch: " + method);
          continue;
        }

        if ((m.getModifiers() & Modifier.STATIC) == 0 || !callOpt_checkArgs(m, args, debug))
          continue;

        return m;
      }
      c = c.getSuperclass();
    }
    return null;
  }

  static Method callOpt_findMethod(Object o, String method, Object[] args, boolean debug) {
    Class c = o.getClass();
    while (c != null) {
      for (Method m : c.getDeclaredMethods()) {
        if (debug)
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
        if (m.getName().equals(method) && callOpt_checkArgs(m, args, debug))
          return m;
      }
      c = c.getSuperclass();
    }
    return null;
  }

  private static boolean callOpt_checkArgs(Method m, Object[] args, boolean debug) {
    Class<?>[] types = m.getParameterTypes();
    if (types.length != args.length) {
      if (debug)
        System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
      return false;
    }
    for (int i = 0; i < types.length; i++)
      if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
        if (debug)
          System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
        return false;
      }
    return true;
  }



static String getComputerID() { try {
 
  return computerID();

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static Class getMainClass() { try {
 
  return Class.forName("main");

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static Throwable getInnerException(Throwable e) {
  while (e.getCause() != null)
    e = e.getCause();
  return e;
}

static boolean publicCommOn() {
  return "1".equals(loadTextFile(new File(userHome(), ".javax/public-communication")));
}

static List<Object> record_list = synchroList();

static void record(Object o) {
  record_list.add(o);
}

// try to get our current process ID
static String getPID() {
  String name = ManagementFactory.getRuntimeMXBean().getName();
  return name.replaceAll("@.*", "");
}
static String getProgramID() {
  return programID;
}

  static List<String> codeTokensOnly(List<String> tok) {
    List<String> l = new ArrayList<String>();
    for (int i = 1; i < tok.size(); i += 2)
      l.add(tok.get(i));
    return l;
  }

  static AtomicInteger dialogServer_clients = new AtomicInteger();
  
  static int startDialogServerOnPortAbove(int port, DialogHandler handler) {
    while (!startDialogServerIfPortAvailable(port, handler))
      ++port;
    return port;
  }
  
  static int startDialogServerOnPortAboveDaemon(int port, DialogHandler handler) {
    while (!startDialogServerIfPortAvailable(port, handler, true))
      ++port;
    return port;
  }
  
  static void startDialogServer(int port, DialogHandler handler) {
    if (!startDialogServerIfPortAvailable(port, handler))
      fail("Can't start dialog server on port " + port);
  }
  
  static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler) {
    return startDialogServerIfPortAvailable(port, handler, false);
  }
  
  static ServerSocket startDialogServer_serverSocket;
    
  static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler, boolean daemon) {
    ServerSocket serverSocket = null;
    try {
      serverSocket = new ServerSocket(port);
    } catch (IOException e) {
      // probably the port number is used - let's assume there already is a chat server.
      return false;
    }
    final ServerSocket _serverSocket = serverSocket;
    startDialogServer_serverSocket = serverSocket;

    Thread thread = new Thread("Socket accept port " + port) { public void run() {
     try {
      while (true) {
        try {
          final Socket s = _serverSocket.accept();
          
          print("connect from " + s.getInetAddress() + " - clients: " + dialogServer_clients.incrementAndGet());
          String threadName = "Handling client " + s.getInetAddress();

          Thread t2 = new Thread(threadName) {
           public void run() {
            try {
              final Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
              final BufferedReader in = new BufferedReader(
                new InputStreamReader(s.getInputStream(), "UTF-8"));
              DialogIO io = new DialogIO() {
                String line;
                boolean buff;
                
                Socket getSocket() { return s; }
                
                void close() {
                  try {
                    s.close();
                  } catch (IOException e) {
                    // whatever
                  }
                }
                
                // local means localhost - todo: test
                boolean isLocalConnection() {
                  return s.getInetAddress().isLoopbackAddress();
                }
                
                boolean isStillConnected() {
                  return !(buff || s.isClosed());
                }
                
                String readLineNoBlock() {
                  String l = line;
                  line = null;
                  return l;
                }
                
                boolean waitForLine() { try {
 
                  if (line != null) return true;
                  //print("Readline");
                  line = in.readLine();
                  //print("Readline done: " + line);
                  if (line == null) buff = true;
                  return line != null;
                
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
                
                void sendLine(String line) { try {
 
                  w.write(line + "\n");
                  w.flush();
                
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
              };
              
              try {
                handler.run(io);
              } finally {
                s.close();
              }
            } catch (IOException e) {
              print("[internal] " + e);
            } finally {
              print("client disconnect - " + dialogServer_clients.decrementAndGet() + " remaining");
            }
           }
          }; // Thread t2
          t2.setDaemon(true); // ?
          t2.start();
        } catch (SocketTimeoutException e) {
        }
      }   
     } catch (IOException e) {
       print("[internal] " + e);
     }
    }};
    if (daemon) thread.setDaemon(true);
    thread.start();
   
    print("Dialog server on port " + port + " started."); 
    return true;
  }

  static List<String> parse3(String s) {
    return dropPunctuation(javaTokPlusPeriod(s));
  }

static BufferedReader readLine_reader;

static String readLine() { try {
 
  if (readLine_reader == null)
    readLine_reader = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
  String s = readLine_reader.readLine();
  if (s != null)
    print(s);
  return s;

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static class DynamicObject {
  String className;
  Map<String, Object> fieldValues = new TreeMap<String, Object>();
}

// actually it's now almost the same as jsonDecode :)
static Object unstructure(String text) {
  final List<String> tok = javaTok(text);
  
  class X {
    int i = 1;

    Object parse() {
      String t = tok.get(i);
      if (t.startsWith("\"")) {
        String s = unquote(tok.get(i));
        i += 2;
        return s;
      }
      if (t.equals("{"))
        return parseMap();
      if (t.equals("["))
        return parseList();
      if (t.equals("null")) {
        i += 2; return null;
      }
      if (t.equals("false")) {
        i += 2; return false;
      }
      if (t.equals("true")) {
        i += 2; return true;
      }
      if (isInteger(t)) {
        i += 2; return Long.parseLong(t);
      }
      if (isJavaIdentifier(t)) {
        Class c = findClass(t);
        DynamicObject dO = null;
        Object o = null;
        if (c != null)
          o = nuObject(c);
        else {
          dO = new DynamicObject();
          dO.className = t;
        }
        i += 2;
        if (i < tok.size() && tok.get(i).equals("(")) {
          consume("(");
          while (!tok.get(i).equals(")")) {
            // It's like parsing a map.
            //Object key = parse();
            //if (tok.get(i).equals(")"))
            //  key = onlyField();
            String key = unquote(tok.get(i));
            i += 2;
            consume("=");
            Object value = parse();
            if (o != null)
              set(o, key, value);
            else
              dO.fieldValues.put(key, value);
            if (tok.get(i).equals(",")) i += 2;
          }
          consume(")");
        }
        return o != null ? o : dO;
      }
      throw new RuntimeException("Unknown token " + (i+1) + ": " + t);
    }
    
    Object parseList() {
      consume("[");
      List list = new ArrayList();
      while (!tok.get(i).equals("]")) {
        list.add(parse());
        if (tok.get(i).equals(",")) i += 2;
      }
      consume("]");
      return list;
    }
    
    Object parseMap() {
      consume("{");
      Map map = new TreeMap();
      while (!tok.get(i).equals("}")) {
        String key = unquote(tok.get(i));
        i += 2;
        consume("=");
        Object value = parse();
        map.put(key, value);
        if (tok.get(i).equals(",")) i += 2;
      }
      consume("}");
      return map;
    }
    
    void consume(String s) {
      if (!tok.get(i).equals(s)) {
        String prevToken = i-2 >= 0 ? tok.get(i-2) : "";
        String nextTokens = join(tok.subList(i, Math.min(i+4, tok.size())));
        fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");
      }
      i += 2;
    }
  }
  
  return new X().parse();
}

static File programDir() {
  return programDir(getProgramID());
}

static File programDir(String snippetID) {
  return new File(userHome(), "JavaX-Data/" + formatSnippetID(snippetID));
}


static String randomID(int length) {
  return makeRandomID(length);
}

// match2 matches multiple "*" (matches a single token) wildcards and zero or one "..." wildcards (matches multiple tokens)

static String[] match2(List<String> pat, List<String> tok) {
  // standard case (no ...)
  int i = pat.indexOf("...");
  if (i < 0) return match2_match(pat, tok);
  
  pat = new ArrayList<String>(pat); // We're modifying it, so copy first
  pat.set(i, "*");
  while (pat.size() < tok.size()) {
    pat.add(i, "*");
    pat.add(i+1, ""); // doesn't matter
  }
  
  return match2_match(pat, tok);
}

static String[] match2_match(List<String> pat, List<String> tok) {
  List<String> result = new ArrayList<String>();
  if (pat.size() != tok.size()) {
    /*if (debug)
      print("Size mismatch: " + structure(pat) + " vs " + structure(tok));*/
    return null;
  }
  for (int i = 1; i < pat.size(); i += 2) {
    String p = pat.get(i), t = tok.get(i);
    /*if (debug)
      print("Checking " + p + " against " + t);*/
    if ("*".equals(p))
      result.add(t);
    else if (!p.equalsIgnoreCase(t))
      return null;
  }
  return result.toArray(new String[result.size()]);
}


static String uploadData(String gummipw, String title, File dataFile) { try {
 
  return uploadData(gummipw, title, loadBinaryFile(dataFile), false);

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static String uploadDataSuperHigh(String gummipw, String title, File dataFile) { try {
 
  return uploadData(gummipw, title, loadBinaryFile(dataFile), true);

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static String uploadData(String gummipw, String title, byte[] data) throws IOException {
  return uploadData(gummipw, title, data, false);
}

static String uploadData(String gummipw, String title, byte[] data, boolean superhigh) throws IOException {
  if (gummipw == null || gummipw.length() == 0)
    throw new RuntimeException("Need gummi password.");
    
  System.out.println("Uploading:");
  System.out.println("  gummipw: " + gummipw);
  System.out.println("  " + title);
  int displayLength = 40;
  System.out.println("  " + data.length + " bytes of data");

  URL url = new URL("http://tinybrain.de:8080/tb-int/add_snippet.php");
  String postData =
    "gummipw=" + URLEncoder.encode(unnull(gummipw), "UTF-8")
    + "&blob=" + URLEncoder.encode(base64encode(data), "UTF-8")
    + "&name=" + URLEncoder.encode(unnull(title), "UTF-8")
    + "&type=" + 37 /* SN_DATA */
    + "&" + (superhigh ? "superhigh" : "high") + "=1&public=1";
  System.out.println("Post data length: " + postData.length());

  String contents = doPost(postData, url.openConnection(), url);
  System.out.println(contents);

  if (isInteger(contents)) {
    long id = parseSnippetID(contents);
    System.out.println("=> #" + id);
    return "#" + id;
  } else
    throw new RuntimeException("Server error: " + contents);
}

// replacement for class JavaTok
// maybe incomplete, might want to add floating point numbers
// todo also: extended multi-line strings

static List<String> javaTok(String s) {
  List<String> tok = new ArrayList<String>();
  int l = s.length();
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c; String cc;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      cc = s.substring(j, Math.min(j+2, l));
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (cc.equals("/*")) {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (cc.equals("//")) {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    tok.add(s.substring(i, j));
    i = j;
    if (i >= l) break;
    c = s.charAt(i); // cc is not needed in rest of loop body
    cc = s.substring(i, Math.min(i+2, l));

    // scan for non-whitespace
    if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        if (s.charAt(j) == opener) {
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && Character.isJavaIdentifierPart(s.charAt(j)));
    else if (Character.isDigit(c))
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    else if (cc.equals("[[")) {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else
      ++j;

    tok.add(s.substring(i, j));
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  return tok;
}


  /** 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";
    if (contents != null) {
      FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
      PrintWriter printWriter = new PrintWriter(outputStreamWriter);
      printWriter.print(contents);
      printWriter.close();
    }
    
    if (file.exists() && !file.delete())
      throw new IOException("Can't delete " + fileName);

    if (contents != null)
      if (!new File(tempFileName).renameTo(file))
        throw new IOException("Can't rename " + tempFileName + " to " + fileName);
  }
  
  public static void saveTextFile(File fileName, String contents) {
    try {
      saveTextFile(fileName.getPath(), contents);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }


  public static String loadTextFile(String fileName) {
    try {
      return loadTextFile(fileName, null);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  
  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, "UTF-8");
    return loadTextFile(inputStreamReader);
  }
  
  public static String loadTextFile(File fileName) {
    try {
      return loadTextFile(fileName, null);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public static String loadTextFile(File fileName, String defaultContents) throws IOException {
    try {
      return loadTextFile(fileName.getPath(), defaultContents);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  
  public static String loadTextFile(Reader reader) throws IOException {
    StringBuilder builder = new StringBuilder();
    try {
      char[] buffer = new char[1024];
      int n;
      while (-1 != (n = reader.read(buffer)))
        builder.append(buffer, 0, n);
        
    } finally {
      reader.close();
    }
    return builder.toString();
  }

  public static String unquote(String s) {
    if (s.startsWith("[")) {
      int i = 1;
      while (i < s.length() && s.charAt(i) == '=') ++i;
      if (i < s.length() && s.charAt(i) == '[') {
        String m = s.substring(1, i);
        if (s.endsWith("]" + m + "]"))
          return s.substring(i+1, s.length()-i-1);
      }
    }
    
    if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1) {
      String st = s.substring(1, s.length()-1);
      StringBuilder sb = new StringBuilder(st.length());
  
      for (int i = 0; i < st.length(); i++) {
        char ch = st.charAt(i);
        if (ch == '\\') {
          char nextChar = (i == st.length() - 1) ? '\\' : st
                  .charAt(i + 1);
          // Octal escape?
          if (nextChar >= '0' && nextChar <= '7') {
              String code = "" + nextChar;
              i++;
              if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
                      && st.charAt(i + 1) <= '7') {
                  code += st.charAt(i + 1);
                  i++;
                  if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
                          && st.charAt(i + 1) <= '7') {
                      code += st.charAt(i + 1);
                      i++;
                  }
              }
              sb.append((char) Integer.parseInt(code, 8));
              continue;
          }
          switch (nextChar) {
          case '\\':
              ch = '\\';
              break;
          case 'b':
              ch = '\b';
              break;
          case 'f':
              ch = '\f';
              break;
          case 'n':
              ch = '\n';
              break;
          case 'r':
              ch = '\r';
              break;
          case 't':
              ch = '\t';
              break;
          case '\"':
              ch = '\"';
              break;
          case '\'':
              ch = '\'';
              break;
          // Hex Unicode: u????
          case 'u':
              if (i >= st.length() - 5) {
                  ch = 'u';
                  break;
              }
              int code = Integer.parseInt(
                      "" + st.charAt(i + 2) + st.charAt(i + 3)
                              + st.charAt(i + 4) + st.charAt(i + 5), 16);
              sb.append(Character.toChars(code));
              i += 5;
              continue;
          }
          i++;
        }
        sb.append(ch);
      }
      return sb.toString();      
    } else
      return s; // return original
  }

  static void set(Object o, String field, Object value) {
    if (o instanceof Class) set((Class) o, field, value);
    else try {
      Field f = set_findField(o.getClass(), field);
      f.setAccessible(true);
      f.set(o, value);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  static void set(Class c, String field, Object value) {
    try {
      Field f = set_findStaticField(c, field);
      f.setAccessible(true);
      f.set(null, value);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  static Field set_findField(Class<?> c, String field) {
    for (Field f : c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
  }
  
  static Field set_findStaticField(Class<?> c, String field) {
    for (Field f : c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
        return f;
    throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
  }

static String _computerID;
public static String computerID() throws IOException {
  if (_computerID == null) {
    File file = new File(userHome(), ".tinybrain/computer-id");
    _computerID = loadTextFile(file.getPath(), null);
    if (_computerID == null) {
      _computerID = makeRandomID(12);
      saveTextFile(file.getPath(), _computerID);
    }
  }
  return _computerID;
}

static Object get(Object o, String field) {
  if (o instanceof Class) return get((Class) o, field);
  try {
    Field f = get_findField(o.getClass(), field);
    f.setAccessible(true);
    return f.get(o);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Object get(Class c, String field) {
  try {
    Field f = get_findStaticField(c, field);
    f.setAccessible(true);
    return f.get(null);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Field get_findStaticField(Class<?> c, String field) {
  for (Field f : c.getDeclaredFields())
    if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
      return f;
  throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}

static Field get_findField(Class<?> c, String field) {
  for (Field f : c.getDeclaredFields())
    if (f.getName().equals(field))
      return f;
  throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}

static String structure(Object o) {
  return structure(o, 0);
}

// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;
  
static String structure(Object o, int stringSizeLimit) {
  if (o == null) return "null";
  String name = o.getClass().getName();
  
  StringBuilder buf = new StringBuilder();
  
  if (o instanceof Collection) {
    for (Object x : (Collection) o) {
      if (buf.length() != 0) buf.append(", ");
      buf.append(structure(x, stringSizeLimit));
    }
    return "[" + buf + "]";
  }
  
  if (o instanceof Map) {
    for (Object e : ((Map) o).entrySet()) {
      if (buf.length() != 0) buf.append(", ");
      buf.append(structure(((Map.Entry) e).getKey(), stringSizeLimit));
      buf.append("=");
      buf.append(structure(((Map.Entry) e).getValue(), stringSizeLimit));
    }
    return "{" + buf + "}";
  }
  
  if (o.getClass().isArray()) {
    int n = Array.getLength(o);
    for (int i = 0; i < n; i++) {
      if (buf.length() != 0) buf.append(", ");
      buf.append(structure(Array.get(o, i), stringSizeLimit));
    }
    return "{" + buf + "}";
  }

  if (o instanceof String)
    return quote(stringSizeLimit != 0 ? shorten((String) o, stringSizeLimit) : (String) o);
  
  // Need more cases? This should cover all library classes...
  if (name.startsWith("java.") || name.startsWith("javax."))
    return String.valueOf(o);
    
  String shortName = o.getClass().getName().replaceAll("^main\\$", "");

  // TODO: go to superclasses too
  Field[] fields = o.getClass().getDeclaredFields();
  int numFields = 0;
  String fieldName = "";
  for (Field field : fields) {
    if ((field.getModifiers() & Modifier.STATIC) != 0)
      continue;
    Object value;
    try {
      field.setAccessible(true);
      value = field.get(o);
    } catch (Exception e) {
      value = "?";
    }
    
    fieldName = field.getName();
    
    // put special cases here...

    if (value != null) {
      if (buf.length() != 0) buf.append(", ");
      buf.append(fieldName + "=" + structure(value, stringSizeLimit));
    }
    ++numFields;
  }
  String b = buf.toString();
  
  if (numFields == 1 && structure_allowShortening)
    b = b.replaceAll("^" + fieldName + "=", ""); // drop field name if only one
  String s = shortName;
  if (buf.length() != 0)
    s += "(" + b + ")";
  return s;
}

static String formatSnippetID(String id) {
  return "#" + parseSnippetID(id);
}

static String formatSnippetID(long id) {
  return "#" + id;
}

  // TODO: subdirectories
  static int dir2zip(File inDir, ZipOutputStream outZip, String outPrefix) { try {
 
    int count = 0;
    for (File f : inDir.listFiles()) {
      if (f.isFile()) {
        System.out.println("Copying " + f.getName());
        ++count;
        outZip.putNextEntry(new ZipEntry(outPrefix + f.getName()));
        InputStream fin = new FileInputStream(f);
        copyStream(fin, outZip);
        fin.close();
      }
    }
    return count;
  
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
  
  static int dir2zip(File inDir, ZipOutputStream outZip) {
    return dir2zip(inDir, outZip, "");
  }
  
  static int dir2zip(String inDir, ZipOutputStream outZip) {
    return dir2zip(new File(inDir), outZip, "");
  }


static boolean isJavaIdentifier(String s) {
  if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0)))
    return false;
  for (int i = 1; i < s.length(); i++)
    if (!Character.isJavaIdentifierPart(s.charAt(i)))
      return false;
  return true;
}

// currently finds only inner classes of class "main"
// returns null on not found
// this is the simple version that is not case-tolerant
static Class findClass(String name) {
  try {
    return Class.forName("main$" + name);
  } catch (ClassNotFoundException e) {
    return null;
  }
}

static Object nuObject(Class c, Object... args) { try {
 
  Constructor m = nuObject_findConstructor(c, args);
  m.setAccessible(true);
  return m.newInstance(args);

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static Constructor nuObject_findConstructor(Class c, Object... args) {
  for (Constructor m : c.getDeclaredConstructors()) {
    if (!nuObject_checkArgs(m.getParameterTypes(), args, false))
      continue;
    return m;
  }
  throw new RuntimeException("Constructor with " + args.length + " matching parameter(s) not found in " + c.getName());
}

 static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) {
    if (types.length != args.length) {
      if (debug)
        System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
      return false;
    }
    for (int i = 0; i < types.length; i++)
      if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
        if (debug)
          System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
        return false;
      }
    return true;
  }
static DialogIO talkTo(int port) {
  return talkTo("localhost", port);
}

static DialogIO talkTo(String ip, int port) { try {
 
  print("Talking to " + ip + ":" + port);
  final Socket s = new Socket(ip, port);    

  final Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
  final BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
  return new DialogIO() {
    String line;
    boolean buff;
    
    boolean isLocalConnection() {
      return s.getInetAddress().isLoopbackAddress();
    }
    
    boolean isStillConnected() {
      return !(buff || s.isClosed());
    }
    
    String readLineNoBlock() {
      String l = line;
      line = null;
      return l;
    }
    
    boolean waitForLine() { try {
 
      if (line != null) return true;
      //print("Readline");
      line = in.readLine();
      //print("Readline done: " + line);
      if (line == null) buff = true;
      return line != null;
    
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
    
    void sendLine(String line) { try {
 
      w.write(line + "\n");
      w.flush();
    
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
    
    void close() {
      try {
        s.close();
      } catch (IOException e) {
        // whatever
      }
    }
    
    Socket getSocket() {
      return s;
    }
    
    void pushback(String l) {
      if (line != null)
        fail();
      line = l;
    }
  };

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static <A> List<A> synchroList() {
  return Collections.synchronizedList(new ArrayList<A>());
}

static <A> List<A> synchroList(List<A> l) {
  return Collections.synchronizedList(l);
}


// extended over Class.isInstance() to handle primitive types
static boolean isInstanceX(Class type, Object arg) {
  if (type == boolean.class) return arg instanceof Boolean;
  if (type == int.class) return arg instanceof Integer;
  if (type == long.class) return arg instanceof Long;
  if (type == float.class) return arg instanceof Float;
  if (type == short.class) return arg instanceof Short;
  if (type == char.class) return arg instanceof Character;
  if (type == byte.class) return arg instanceof Byte;
  return type.isInstance(arg);
}

public static void mkdirsForFile(File file) {
  File dir = file.getParentFile();
  if (dir != null) // is null if file is in current dir
    dir.mkdirs();
}


static List<String> dropPunctuation(List<String> tok) {
  tok = new ArrayList<String>(tok);
  for (int i = 1; i < tok.size(); i += 2) {
    String t = tok.get(i);
    if (t.length() == 1 && !Character.isLetter(t.charAt(0)) && !Character.isDigit(t.charAt(0)) && !t.equals("*")) {
      tok.set(i-1, tok.get(i-1) + tok.get(i+1));
      tok.remove(i);
      tok.remove(i);
      i -= 2;
    }
  }
  return tok;
}

// javaTok extended with "..." token.

static List<String> javaTokPlusPeriod(String s) {
  List<String> tok = new ArrayList<String>();
  int l = s.length();
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c; String cc;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      cc = s.substring(j, Math.min(j+2, l));
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (cc.equals("/*")) {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (cc.equals("//")) {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    tok.add(s.substring(i, j));
    i = j;
    if (i >= l) break;
    c = s.charAt(i); // cc is not needed in rest of loop body
    cc = s.substring(i, Math.min(i+2, l));

    // scan for non-whitespace
    if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        if (s.charAt(j) == opener) {
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && Character.isJavaIdentifierPart(s.charAt(j)));
    else if (Character.isDigit(c))
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    else if (cc.equals("[[")) {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else if (s.substring(j, Math.min(j+3, l)).equals("..."))
      j += 3;
    else
      ++j;

    tok.add(s.substring(i, j));
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  return tok;
}


  static String base64encode(byte[] a) {
    int aLen = a.length;
    int numFullGroups = aLen/3;
    int numBytesInPartialGroup = aLen - 3*numFullGroups;
    int resultLen = 4*((aLen + 2)/3);
    StringBuffer result = new StringBuffer(resultLen);
    char[] intToAlpha = intToBase64;

    // Translate all full groups from byte array elements to Base64
    int inCursor = 0;
    for (int i=0; i<numFullGroups; i++) {
      int byte0 = a[inCursor++] & 0xff;
      int byte1 = a[inCursor++] & 0xff;
      int byte2 = a[inCursor++] & 0xff;
      result.append(intToAlpha[byte0 >> 2]);
      result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
      result.append(intToAlpha[(byte1 << 2)&0x3f | (byte2 >> 6)]);
      result.append(intToAlpha[byte2 & 0x3f]);
    }

    // Translate partial group if present
    if (numBytesInPartialGroup != 0) {
      int byte0 = a[inCursor++] & 0xff;
      result.append(intToAlpha[byte0 >> 2]);
      if (numBytesInPartialGroup == 1) {
        result.append(intToAlpha[(byte0 << 4) & 0x3f]);
        result.append("==");
      } else {
        // assert numBytesInPartialGroup == 2;
        int byte1 = a[inCursor++] & 0xff;
        result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
        result.append(intToAlpha[(byte1 << 2)&0x3f]);
        result.append('=');
      }
    }
    // assert inCursor == a.length;
    // assert result.length() == resultLen;
    return result.toString();
  }

  /**
   * This array is a lookup table that translates 6-bit positive integer
   * index values into their "Base64 Alphabet" equivalents as specified
   * in Table 1 of RFC 2045.
   */
  static final char intToBase64[] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
  };

public static byte[] loadBinaryFile(String fileName) throws IOException {
  if (!new File(fileName).exists())
    return null;

  FileInputStream in = new FileInputStream(fileName);

  byte buf[] = new byte[1024];
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  int l;
  while (true) {
    l = in.read(buf);
    if (l <= 0) break;
    out.write(buf, 0, l);
  }
  in.close();
  return out.toByteArray();
}

public static byte[] loadBinaryFile(File file) throws IOException {
  return loadBinaryFile(file.getPath());
}


  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 String join(String glue, String[] strings) {
    return join(glue, Arrays.asList(strings));
  }
  
  public static String join(Iterable<String> strings) {
    return join("", strings);
  }
  
  public static String join(String[] strings) {
    return join("", strings);
  }



  static String unnull(String s) {
    return s == null ? "" : s;
  }

  static RuntimeException fail() {
    throw new RuntimeException("fail");
  }
  
  static RuntimeException fail(Object msg) {
    throw new RuntimeException(String.valueOf(msg));
  }

static String doPost(String urlParameters, String url) throws IOException {
  URL _url = new URL(url);
  return doPost(urlParameters, _url.openConnection(), _url);
}

static String doPost(String urlParameters, URLConnection conn, URL url) throws IOException {
  // connect and do POST
  conn.setDoOutput(true);

  OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
  writer.write(urlParameters);
  writer.flush();

  String contents = loadPage(conn, url);
  writer.close();
  return contents;
}

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

static String _userHome;
static String userHome() {
  if (_userHome == null) {
    if (isAndroid())
      _userHome = "/storage/sdcard0/";
    else
      _userHome = System.getProperty("user.home");
    //System.out.println("userHome: " + _userHome);
  }
  return _userHome;
}


static String makeRandomID(int length) {
  Random random = new Random();
  char[] id = new char[length];
  for (int i = 0; i < id.length; i++)
    id[i] = (char) ((int) 'a' + random.nextInt(26));
  return new String(id);
}

static String shorten(String s, int max) {
  return s.length() <= max ? s : s.substring(0, Math.min(s.length(), max)) + "...";
}

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

static boolean isAndroid() { return System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0; }

public static void copyStream(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);
  }
}

  public static String loadPage(String url) {
    try {
      if(url.startsWith("tb/"))
        url = "tinybrain.de:8080/" + url;
      if (url.indexOf("://") < 0)
        url = "http://" + url;
      return loadPage(new URL(url));
    } catch (IOException e) { throw new RuntimeException(e); }
  }
  
  public static String loadPage(URL url) {
    try {
      System.out.println("Loading: " + url.toExternalForm());
      URLConnection con = url.openConnection();
      return loadPage(con, url);
    } catch (IOException e) { throw new RuntimeException(e); }
  }

  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 = loadPage_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();
  }
  
  static String loadPage_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";
  }


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