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();
}
}
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")));
}
}
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());
}
}
}
Aggregations