public static String readline (InputStreamReader in) {
        int ch;
        StringBuffer line = null;
        try {
            while (true) {
                ch = in.read();
                if (ch == -1) {
                    if (line == null) return null;
                    return line.toString();
                }
                if (ch == 10 || ch == 13) {
                    // eol if we read other characters, ignore if not
                    if (line != null) return line.toString();
                }
                else {
                    if (line == null) line = new StringBuffer();
                    line.append((char) ch);
                }
            }
        }
        catch (IOException e) {
            if (line == null) return null;
            return line.toString();
        }
    }