1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.compress.compressors.lz4;
21
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.RandomAccessFile;
27 import java.nio.ByteBuffer;
28 import java.nio.channels.FileChannel;
29 import java.nio.charset.StandardCharsets;
30 import java.util.Base64;
31
32
33
34
35 public class CompressionDegradationTest {
36
37 private static String compress(final String value) throws IOException {
38 try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream(value.length());
39 FramedLZ4CompressorOutputStream compress = new FramedLZ4CompressorOutputStream(byteStream)) {
40 String compressedValue = null;
41 try {
42 compress.write(value.getBytes(StandardCharsets.UTF_8));
43 compress.finish();
44 compressedValue = Base64.getEncoder().encodeToString(byteStream.toByteArray());
45 } finally {
46 compress.close();
47 byteStream.close();
48 }
49
50 return compressedValue;
51 }
52 }
53
54 public static void main(final String[] args) throws Exception {
55 try (RandomAccessFile aFile = new RandomAccessFile("src/test/resources/org/apache/commons/compress/COMPRESS-649/some-900kb-text.txt", "r");
56 FileChannel inChannel = aFile.getChannel()) {
57 final long fileSize = inChannel.size();
58
59 final ByteBuffer buffer = ByteBuffer.allocate((int) fileSize);
60 inChannel.read(buffer);
61 buffer.flip();
62
63 final String rawPlan = new String(buffer.array(), StandardCharsets.UTF_8);
64 final long start = System.currentTimeMillis();
65 for (int i = 0; i < 80; i++) {
66 assertNotNull(compress(rawPlan));
67 }
68 final long end = System.currentTimeMillis();
69 final float sec = (end - start) / 1000F;
70 System.out.println(sec + " seconds");
71 }
72 }
73 }