Search in sources :

Example 1 with LZFOutputStream

use of org.h2.compress.LZFOutputStream in project h2database by h2database.

the class TestStreams method testLZFStreams.

private void testLZFStreams() throws IOException {
    Random random = new Random(1);
    int max = getSize(100, 1000);
    for (int i = 0; i < max; i += 3) {
        byte[] buffer = getRandomBytes(random);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        LZFOutputStream comp = new LZFOutputStream(out);
        if (random.nextInt(10) == 1) {
            comp.write(buffer);
        } else {
            for (int j = 0; j < buffer.length; ) {
                int[] sizes = { 0, 1, random.nextInt(100), random.nextInt(100000) };
                int size = sizes[random.nextInt(sizes.length)];
                size = Math.min(size, buffer.length - j);
                if (size == 1) {
                    comp.write(buffer[j]);
                } else {
                    comp.write(buffer, j, size);
                }
                j += size;
            }
        }
        comp.close();
        byte[] compressed = out.toByteArray();
        ByteArrayInputStream in = new ByteArrayInputStream(compressed);
        LZFInputStream decompress = new LZFInputStream(in);
        byte[] test = new byte[buffer.length];
        for (int j = 0; j < buffer.length; ) {
            int[] sizes = { 0, 1, random.nextInt(100), random.nextInt(100000) };
            int size = sizes[random.nextInt(sizes.length)];
            if (size == 1) {
                int x = decompress.read();
                if (x < 0) {
                    break;
                }
                test[j++] = (byte) x;
            } else {
                size = Math.min(size, test.length - j);
                int l = decompress.read(test, j, size);
                if (l < 0) {
                    break;
                }
                j += l;
            }
        }
        decompress.close();
        assertEquals(buffer, test);
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) LZFInputStream(org.h2.compress.LZFInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LZFOutputStream(org.h2.compress.LZFOutputStream)

Example 2 with LZFOutputStream

use of org.h2.compress.LZFOutputStream in project h2database by h2database.

the class TestStreams method testLZFStreamClose.

private void testLZFStreamClose() throws IOException {
    String fileName = getBaseDir() + "/temp";
    FileUtils.createDirectories(FileUtils.getParent(fileName));
    OutputStream fo = FileUtils.newOutputStream(fileName, false);
    LZFOutputStream out = new LZFOutputStream(fo);
    out.write("Hello".getBytes());
    out.close();
    InputStream fi = FileUtils.newInputStream(fileName);
    LZFInputStream in = new LZFInputStream(fi);
    byte[] buff = new byte[100];
    assertEquals(5, in.read(buff));
    in.read();
    in.close();
    FileUtils.delete(getBaseDir() + "/temp");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) LZFInputStream(org.h2.compress.LZFInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LZFOutputStream(org.h2.compress.LZFOutputStream) LZFInputStream(org.h2.compress.LZFInputStream) LZFOutputStream(org.h2.compress.LZFOutputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 LZFInputStream (org.h2.compress.LZFInputStream)2 LZFOutputStream (org.h2.compress.LZFOutputStream)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Random (java.util.Random)1