Search in sources :

Example 96 with LineNumberReader

use of java.io.LineNumberReader in project gephi by gephi.

the class ImporterSpreadsheetCSV method autoDetectFieldDelimiter.

private void autoDetectFieldDelimiter() {
    // Very simple naive detector but should work in most cases:
    try (LineNumberReader reader = ImportUtils.getTextReader(FileUtil.toFileObject(file))) {
        String line = reader.readLine();
        // Check for typical delimiter chars in the header
        int commaCount = 0;
        int semicolonCount = 0;
        int tabCount = 0;
        int spaceCount = 0;
        boolean inQuote = false;
        for (char c : line.toCharArray()) {
            if (c == '"' || c == '\'') {
                inQuote = !inQuote;
            }
            if (!inQuote) {
                switch(c) {
                    case ',':
                        commaCount++;
                        break;
                    case ';':
                        semicolonCount++;
                        break;
                    case '\t':
                        tabCount++;
                        break;
                    case ' ':
                        spaceCount++;
                        break;
                }
            }
        }
        int max = Collections.max(Arrays.asList(commaCount, semicolonCount, tabCount, spaceCount));
        if (commaCount == max) {
            fieldDelimiter = ',';
        } else if (semicolonCount == max) {
            fieldDelimiter = ';';
        } else if (tabCount == max) {
            fieldDelimiter = '\t';
        } else if (spaceCount == max) {
            fieldDelimiter = ' ';
        }
    } catch (IOException ex) {
    }
}
Also used : IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 97 with LineNumberReader

use of java.io.LineNumberReader in project gephi by gephi.

the class ImportUtils method getTextReader.

/**
 * Returns a <code>LineNumberReader</code> for <code>inputStream</code>. The
 * stream must be a character stream. The charset is detected automatically.
 *
 * @param stream the stream that is to be read
 * @return a reader for the character stream
 * @throws IOException if the stream can't be read
 */
public static LineNumberReader getTextReader(InputStream stream) throws IOException {
    LineNumberReader reader;
    CharsetToolkit charsetToolkit = new CharsetToolkit(stream);
    reader = (LineNumberReader) charsetToolkit.getReader();
    return reader;
}
Also used : CharsetToolkit(org.gephi.utils.CharsetToolkit) LineNumberReader(java.io.LineNumberReader)

Example 98 with LineNumberReader

use of java.io.LineNumberReader in project jackrabbit-oak by apache.

the class QueryRecorder method main.

public static void main(String... args) throws IOException {
    // command line version: read from a file
    LineNumberReader reader = new LineNumberReader(new BufferedReader(new FileReader(args[0])));
    TreeSet<String> sorted = new TreeSet<String>();
    int lineCount = 0;
    while (true) {
        String line = reader.readLine();
        if (line == null) {
            break;
        }
        sorted.add(simplify(line));
        lineCount++;
    }
    reader.close();
    for (String s : sorted) {
        System.out.println(s);
    }
    System.out.println("sorted: " + sorted.size() + " original: " + lineCount);
}
Also used : TreeSet(java.util.TreeSet) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) LineNumberReader(java.io.LineNumberReader)

Example 99 with LineNumberReader

use of java.io.LineNumberReader in project h2database by h2database.

the class TestShell method test.

private void test(final boolean commandLineArgs) throws IOException {
    PipedInputStream testIn = new PipedInputStream();
    PipedOutputStream out = new PipedOutputStream(testIn);
    toolOut = new PrintStream(out, true);
    out = new PipedOutputStream();
    PrintStream testOut = new PrintStream(out, true);
    toolIn = new PipedInputStream(out);
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            try {
                Shell shell = new Shell();
                shell.setIn(toolIn);
                shell.setOut(toolOut);
                shell.setErr(toolOut);
                if (commandLineArgs) {
                    shell.runTool("-url", "jdbc:h2:mem:", "-user", "sa", "-password", "sa");
                } else {
                    shell.runTool();
                }
            } finally {
                toolOut.close();
            }
        }
    };
    task.execute();
    InputStreamReader reader = new InputStreamReader(testIn);
    lineReader = new LineNumberReader(reader);
    read("");
    read("Welcome to H2 Shell");
    read("Exit with");
    if (!commandLineArgs) {
        read("[Enter]");
        testOut.println("jdbc:h2:mem:");
        read("URL");
        testOut.println("");
        read("Driver");
        testOut.println("sa");
        read("User");
        testOut.println("sa");
        read("Password");
    }
    read("Commands are case insensitive");
    read("help or ?");
    read("list");
    read("maxwidth");
    read("autocommit");
    read("history");
    read("quit or exit");
    read("");
    testOut.println("history");
    read("sql> No history");
    testOut.println("1");
    read("sql> Not found");
    testOut.println("select 1 a;");
    read("sql> A");
    read("1");
    read("(1 row,");
    testOut.println("history");
    read("sql> #1: select 1 a");
    read("To re-run a statement, type the number and press and enter");
    testOut.println("1");
    read("sql> select 1 a");
    read("A");
    read("1");
    read("(1 row,");
    testOut.println("select 'x' || space(1000) large, 'y' small;");
    read("sql> LARGE");
    read("x");
    read("(data is partially truncated)");
    read("(1 row,");
    testOut.println("select x, 's' s from system_range(0, 10001);");
    read("sql> X    | S");
    for (int i = 0; i < 10000; i++) {
        read((i + "     ").substring(0, 4) + " | s");
    }
    for (int i = 10000; i <= 10001; i++) {
        read((i + "     ").substring(0, 5) + " | s");
    }
    read("(10002 rows,");
    testOut.println("select error;");
    read("sql> Error:");
    if (read("").startsWith("Column \"ERROR\" not found")) {
        read("");
    }
    testOut.println("create table test(id int primary key, name varchar)\n;");
    read("sql> ...>");
    testOut.println("insert into test values(1, 'Hello');");
    read("sql>");
    testOut.println("select null n, * from test;");
    read("sql> N    | ID | NAME");
    read("null | 1  | Hello");
    read("(1 row,");
    // test history
    for (int i = 0; i < 30; i++) {
        testOut.println("select " + i + " ID from test;");
        read("sql> ID");
        read("" + i);
        read("(1 row,");
    }
    testOut.println("20");
    read("sql> select 10 ID from test");
    read("ID");
    read("10");
    read("(1 row,");
    testOut.println("maxwidth");
    read("sql> Usage: maxwidth <integer value>");
    read("Maximum column width is now 100");
    testOut.println("maxwidth 80");
    read("sql> Maximum column width is now 80");
    testOut.println("autocommit");
    read("sql> Usage: autocommit [true|false]");
    read("Autocommit is now true");
    testOut.println("autocommit false");
    read("sql> Autocommit is now false");
    testOut.println("autocommit true");
    read("sql> Autocommit is now true");
    testOut.println("list");
    read("sql> Result list mode is now on");
    testOut.println("select 1 first, 2 second;");
    read("sql> FIRST : 1");
    read("SECOND: 2");
    read("(1 row, ");
    testOut.println("select x from system_range(1, 3);");
    read("sql> X: 1");
    read("");
    read("X: 2");
    read("");
    read("X: 3");
    read("(3 rows, ");
    testOut.println("select x, 2 as y from system_range(1, 3) where 1 = 0;");
    read("sql> X");
    read("Y");
    read("(0 rows, ");
    testOut.println("list");
    read("sql> Result list mode is now off");
    testOut.println("help");
    read("sql> Commands are case insensitive");
    read("help or ?");
    read("list");
    read("maxwidth");
    read("autocommit");
    read("history");
    read("quit or exit");
    read("");
    testOut.println("exit");
    read("sql>");
    task.get();
}
Also used : PrintStream(java.io.PrintStream) Task(org.h2.util.Task) Shell(org.h2.tools.Shell) InputStreamReader(java.io.InputStreamReader) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) LineNumberReader(java.io.LineNumberReader)

Example 100 with LineNumberReader

use of java.io.LineNumberReader in project h2database by h2database.

the class Profiler method readRunnableStackTraces.

private static List<Object[]> readRunnableStackTraces(int pid) {
    try {
        String jstack = exec("jstack", "" + pid);
        LineNumberReader r = new LineNumberReader(new StringReader(jstack));
        return readStackTrace(r);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : StringReader(java.io.StringReader) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Aggregations

LineNumberReader (java.io.LineNumberReader)401 IOException (java.io.IOException)203 InputStreamReader (java.io.InputStreamReader)167 FileReader (java.io.FileReader)102 File (java.io.File)79 InputStream (java.io.InputStream)64 StringReader (java.io.StringReader)64 ArrayList (java.util.ArrayList)54 FileInputStream (java.io.FileInputStream)33 BufferedReader (java.io.BufferedReader)27 PrintWriter (java.io.PrintWriter)25 HashMap (java.util.HashMap)22 FileNotFoundException (java.io.FileNotFoundException)20 BufferedWriter (java.io.BufferedWriter)16 FileWriter (java.io.FileWriter)16 Pattern (java.util.regex.Pattern)16 Test (org.junit.jupiter.api.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Reader (java.io.Reader)14