View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Tests COMPRESS-649.
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  }