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  package org.apache.commons.compress.compressors.lz4;
20  
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.util.Arrays;
26  
27  import org.junit.jupiter.api.Test;
28  
29  class FramedLZ4CompressorOutputStreamTest {
30  
31      @Test
32      void testFinishWithNoWrite() throws IOException {
33          final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
34          try (FramedLZ4CompressorOutputStream compressor = new FramedLZ4CompressorOutputStream(buffer,
35                  new FramedLZ4CompressorOutputStream.Parameters(FramedLZ4CompressorOutputStream.BlockSize.K64, true, false, false))) {
36              // do nothing here. this will test that flush on close doesn't throw any exceptions if no data is written.
37          }
38          assertTrue(buffer.size() == 15, "Only the trailer gets written.");
39      }
40  
41      @Test
42      void testWriteByteArrayVsWriteByte() throws IOException {
43          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
44          final byte[] bytes = "abcdefghijklmnop".getBytes();
45          try (FramedLZ4CompressorOutputStream compressor = new FramedLZ4CompressorOutputStream(buffer,
46                  new FramedLZ4CompressorOutputStream.Parameters(FramedLZ4CompressorOutputStream.BlockSize.K64, true, false, false))) {
47              compressor.write(bytes);
48              compressor.finish();
49              compressor.close();
50              assertTrue(compressor.isClosed());
51          }
52          final byte[] bulkOutput = buffer.toByteArray();
53          buffer = new ByteArrayOutputStream();
54          try (FramedLZ4CompressorOutputStream compressor = new FramedLZ4CompressorOutputStream(buffer,
55                  new FramedLZ4CompressorOutputStream.Parameters(FramedLZ4CompressorOutputStream.BlockSize.K64, true, false, false))) {
56              for (final byte element : bytes) {
57                  compressor.write(element);
58              }
59              compressor.finish();
60              compressor.close();
61              assertTrue(compressor.isClosed());
62          }
63          assertTrue(Arrays.equals(bulkOutput, buffer.toByteArray()));
64      }
65  
66  }