Search in sources :

Example 1 with CompressTool

use of org.h2.tools.CompressTool in project h2database by h2database.

the class TestCompress method testMultiThreaded.

private void testMultiThreaded() throws Exception {
    Task[] tasks = new Task[3];
    for (int i = 0; i < tasks.length; i++) {
        Task t = new Task() {

            @Override
            public void call() {
                CompressTool tool = CompressTool.getInstance();
                byte[] b = new byte[1024];
                Random r = new Random();
                while (!stop) {
                    r.nextBytes(b);
                    byte[] test = tool.expand(tool.compress(b, "LZF"));
                    assertEquals(b, test);
                }
            }
        };
        tasks[i] = t;
        t.execute();
    }
    Thread.sleep(1000);
    for (Task t : tasks) {
        t.get();
    }
}
Also used : Task(org.h2.util.Task) Random(java.util.Random) CompressTool(org.h2.tools.CompressTool)

Example 2 with CompressTool

use of org.h2.tools.CompressTool in project h2database by h2database.

the class TestCompress method testVariableEnd.

private void testVariableEnd() {
    CompressTool utils = CompressTool.getInstance();
    StringBuilder b = new StringBuilder();
    for (int i = 0; i < 90; i++) {
        b.append('0');
    }
    String prefix = b.toString();
    for (int i = 0; i < 100; i++) {
        b = new StringBuilder(prefix);
        for (int j = 0; j < i; j++) {
            b.append((char) ('1' + j));
        }
        String test = b.toString();
        byte[] in = test.getBytes();
        assertEquals(in, utils.expand(utils.compress(in, "LZF")));
    }
}
Also used : CompressTool(org.h2.tools.CompressTool)

Example 3 with CompressTool

use of org.h2.tools.CompressTool in project h2database by h2database.

the class TestCompress method testByteArray.

private void testByteArray(int len) throws IOException {
    Random r = new Random(len);
    for (int pattern = 0; pattern < 4; pattern++) {
        byte[] b = new byte[len];
        switch(pattern) {
            case 0:
                // leave empty
                break;
            case 1:
                {
                    r.nextBytes(b);
                    break;
                }
            case 2:
                {
                    for (int x = 0; x < len; x++) {
                        b[x] = (byte) (x & 10);
                    }
                    break;
                }
            case 3:
                {
                    for (int x = 0; x < len; x++) {
                        b[x] = (byte) (x / 10);
                    }
                    break;
                }
            default:
        }
        if (r.nextInt(2) < 1) {
            for (int x = 0; x < len; x++) {
                if (r.nextInt(20) < 1) {
                    b[x] = (byte) (r.nextInt(255));
                }
            }
        }
        CompressTool utils = CompressTool.getInstance();
        // level 9 is highest, strategy 2 is huffman only
        for (String a : new String[] { "LZF", "No", "Deflate", "Deflate level 9 strategy 2" }) {
            long time = System.nanoTime();
            byte[] out = utils.compress(b, a);
            byte[] test = utils.expand(out);
            if (testPerformance) {
                System.out.println("p:" + pattern + " len: " + out.length + " time: " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time) + " " + a);
            }
            assertEquals(b.length, test.length);
            assertEquals(b, test);
            Arrays.fill(test, (byte) 0);
            CompressTool.expand(out, test, 0);
            assertEquals(b, test);
        }
        for (String a : new String[] { null, "LZF", "DEFLATE", "ZIP", "GZIP" }) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            OutputStream out2 = CompressTool.wrapOutputStream(out, a, "test");
            IOUtils.copy(new ByteArrayInputStream(b), out2);
            out2.close();
            InputStream in = new ByteArrayInputStream(out.toByteArray());
            in = CompressTool.wrapInputStream(in, a, "test");
            out.reset();
            IOUtils.copy(in, out);
            assertEquals(b, out.toByteArray());
        }
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompressTool(org.h2.tools.CompressTool) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

CompressTool (org.h2.tools.CompressTool)3 Random (java.util.Random)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Task (org.h2.util.Task)1