Search in sources :

Example 21 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class TestBinaryArithmeticStream method testRandomized.

private void testRandomized(int len) throws IOException {
    Random r = new Random();
    int seed = r.nextInt();
    r.setSeed(seed);
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    Out out = new Out(byteOut);
    for (int i = 0; i < len; i++) {
        int prob = r.nextInt(BinaryArithmeticStream.MAX_PROBABILITY);
        out.writeBit(r.nextBoolean(), prob);
    }
    out.flush();
    byteOut.write(r.nextInt(255));
    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    In in = new In(byteIn);
    r.setSeed(seed);
    for (int i = 0; i < len; i++) {
        int prob = r.nextInt(BinaryArithmeticStream.MAX_PROBABILITY);
        boolean expected = r.nextBoolean();
        boolean got = in.readBit(prob);
        assertEquals(expected, got);
    }
    assertEquals(r.nextInt(255), byteIn.read());
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) In(org.h2.dev.util.BinaryArithmeticStream.In) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Out(org.h2.dev.util.BinaryArithmeticStream.Out)

Example 22 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class TestBitStream method testBitStream.

private void testBitStream() {
    Random r = new Random();
    for (int test = 0; test < 10000; test++) {
        ByteArrayOutputStream buff = new ByteArrayOutputStream();
        int len = r.nextInt(40);
        Out out = new Out(buff);
        long seed = r.nextLong();
        Random r2 = new Random(seed);
        for (int i = 0; i < len; i++) {
            out.writeBit(r2.nextBoolean() ? 1 : 0);
        }
        out.close();
        In in = new In(new ByteArrayInputStream(buff.toByteArray()));
        r2 = new Random(seed);
        int i = 0;
        for (; i < len; i++) {
            int expected = r2.nextBoolean() ? 1 : 0;
            assertEquals(expected, in.readBit());
        }
        for (; i % 8 != 0; i++) {
            assertEquals(0, in.readBit());
        }
        assertEquals(-1, in.readBit());
    }
}
Also used : Random(java.util.Random) In(org.h2.dev.util.BitStream.In) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Out(org.h2.dev.util.BitStream.Out)

Example 23 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class TableView method createTempView.

/**
 * Create a temporary view out of the given query.
 *
 * @param session the session
 * @param owner the owner of the query
 * @param name the view name
 * @param query the query
 * @param topQuery the top level query
 * @return the view table
 */
public static TableView createTempView(Session session, User owner, String name, Query query, Query topQuery) {
    Schema mainSchema = session.getDatabase().getSchema(Constants.SCHEMA_MAIN);
    String querySQL = query.getPlanSQL();
    TableView v = new TableView(mainSchema, 0, name, querySQL, query.getParameters(), null, /* column templates */
    session, false, /* allow recursive */
    true, /* literals have already been checked when parsing original query */
    false, /* is table expression */
    false);
    if (v.createException != null) {
        throw v.createException;
    }
    v.setTopQuery(topQuery);
    v.setOwner(owner);
    v.setTemporary(true);
    return v;
}
Also used : Schema(org.h2.schema.Schema)

Example 24 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class CompressTool method expand.

/**
 * INTERNAL
 */
public static void expand(byte[] in, byte[] out, int outPos) {
    int algorithm = in[0];
    Compressor compress = getCompressor(algorithm);
    try {
        int len = readVariableInt(in, 1);
        int start = 1 + getVariableIntLength(len);
        compress.expand(in, start, in.length - start, out, outPos, len);
    } catch (Exception e) {
        throw DbException.get(ErrorCode.COMPRESSION_ERROR, e);
    }
}
Also used : Compressor(org.h2.compress.Compressor) IOException(java.io.IOException) DbException(org.h2.message.DbException)

Example 25 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class GenerateHelp method main.

/**
 * This method is called when executing this application from the command
 * line.
 *
 * @param args the command line parameters
 */
public static void main(String... args) throws Exception {
    String in = "src/docsrc/help/help.csv";
    String out = "src/main/org/h2/res/help.csv";
    Csv csv = new Csv();
    csv.setLineCommentCharacter('#');
    ResultSet rs = csv.read(in, null, null);
    SimpleResultSet rs2 = new SimpleResultSet();
    ResultSetMetaData meta = rs.getMetaData();
    int columnCount = meta.getColumnCount() - 1;
    for (int i = 0; i < columnCount; i++) {
        rs2.addColumn(meta.getColumnLabel(1 + i), Types.VARCHAR, 0, 0);
    }
    while (rs.next()) {
        Object[] row = new Object[columnCount];
        for (int i = 0; i < columnCount; i++) {
            String s = rs.getString(1 + i);
            if (i == 3) {
                int len = s.length();
                int end = 0;
                for (; end < len; end++) {
                    char ch = s.charAt(end);
                    if (ch == '.') {
                        end++;
                        break;
                    }
                    if (ch == '"') {
                        do {
                            end++;
                        } while (end < len && s.charAt(end) != '"');
                    }
                }
                s = s.substring(0, end);
            }
            row[i] = s;
        }
        rs2.addRow(row);
    }
    BufferedWriter writer = new BufferedWriter(new FileWriter(out));
    writer.write("# Copyright 2004-2018 H2 Group. " + "Multiple-Licensed under the MPL 2.0,\n" + "# and the EPL 1.0 " + "(http://h2database.com/html/license.html).\n" + "# Initial Developer: H2 Group)\n");
    csv = new Csv();
    csv.setLineSeparator("\n");
    csv.write(writer, rs2);
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SimpleResultSet(org.h2.tools.SimpleResultSet) Csv(org.h2.tools.Csv) FileWriter(java.io.FileWriter) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) BufferedWriter(java.io.BufferedWriter)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SQLException (java.sql.SQLException)17 DbException (org.h2.message.DbException)17 Random (java.util.Random)11 ResultSet (java.sql.ResultSet)10 InputStream (java.io.InputStream)9 Statement (java.sql.Statement)9 Connection (java.sql.Connection)7 PrintStream (java.io.PrintStream)6 Properties (java.util.Properties)6 Task (org.h2.util.Task)6 BufferedOutputStream (java.io.BufferedOutputStream)5 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 PipedInputStream (java.io.PipedInputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)4