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   *   https://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  import org.apache.commons.io.RandomAccessFileMode;
33  
34  /**
35   * Tests COMPRESS-649.
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  }