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 import org.apache.commons.io.RandomAccessFileMode;
33
34
35
36
37 class CompressionDegradationTest {
38
39 private static String compress(final String value) throws IOException {
40 try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream(value.length());
41 FramedLZ4CompressorOutputStream compress = new FramedLZ4CompressorOutputStream(byteStream)) {
42 String compressedValue = null;
43 try {
44 compress.writeUtf8(value);
45 compress.finish();
46 compressedValue = Base64.getEncoder().encodeToString(byteStream.toByteArray());
47 } finally {
48 compress.close();
49 byteStream.close();
50 }
51
52 return compressedValue;
53 }
54 }
55
56 public static void main(final String[] args) throws Exception {
57 try (RandomAccessFile aFile = RandomAccessFileMode.READ_ONLY.create("src/test/resources/org/apache/commons/compress/COMPRESS-649/some-900kb-text.txt");
58 FileChannel inChannel = aFile.getChannel()) {
59 final long fileSize = inChannel.size();
60
61 final ByteBuffer buffer = ByteBuffer.allocate((int) fileSize);
62 inChannel.read(buffer);
63 buffer.flip();
64
65 final String rawPlan = new String(buffer.array(), StandardCharsets.UTF_8);
66 final long start = System.currentTimeMillis();
67 for (int i = 0; i < 80; i++) {
68 assertNotNull(compress(rawPlan));
69 }
70 final long end = System.currentTimeMillis();
71 final float sec = (end - start) / 1000F;
72 System.out.println(sec + " seconds");
73 }
74 }
75 }