use of org.h2.compress.LZFInputStream 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);
}
}
use of org.h2.compress.LZFInputStream 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");
}
Aggregations