001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.compress;
019
020import static org.junit.jupiter.api.Assertions.assertArrayEquals;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022
023import java.io.ByteArrayInputStream;
024import java.io.ByteArrayOutputStream;
025import java.io.InputStream;
026import java.io.OutputStream;
027import java.util.Random;
028import org.apache.hadoop.hbase.testclassification.MiscTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.io.IOUtils;
031import org.apache.hadoop.io.compress.Compressor;
032import org.apache.hadoop.io.compress.Decompressor;
033import org.apache.hadoop.io.compress.SnappyCodec;
034import org.apache.hadoop.io.compress.snappy.SnappyCompressor;
035import org.junit.jupiter.api.Tag;
036import org.junit.jupiter.api.Test;
037
038/**
039 * Round trip through {@link Compression.Algorithm#SNAPPY} using its default codec, Hadoop's
040 * {@link SnappyCodec}. Hadoop's SnappyCompressor sets the output buffer limit to 0 before calling
041 * snappy-java, which some snappy-java releases (1.1.10.9, 1.1.10.10) reject. See HBASE-30538.
042 */
043@Tag(MiscTests.TAG)
044@Tag(SmallTests.TAG)
045public class TestHadoopSnappyCodec {
046
047  @Test
048  public void testRoundTrip() throws Exception {
049    Compression.Algorithm algo = Compression.Algorithm.SNAPPY;
050    byte[] data = new byte[256 * 1024];
051    Random rand = new Random(0x30538);
052    for (int i = 0; i < data.length; i++) {
053      // Compressible but not trivial
054      data[i] = (byte) ('a' + rand.nextInt(8));
055    }
056
057    ByteArrayOutputStream compressed = new ByteArrayOutputStream();
058    Compressor compressor = algo.getCompressor();
059    assertEquals(SnappyCompressor.class, compressor.getClass());
060    try (OutputStream os = algo.createCompressionStream(compressed, compressor, 0)) {
061      os.write(data);
062    } finally {
063      algo.returnCompressor(compressor);
064    }
065
066    byte[] result = new byte[data.length];
067    Decompressor decompressor = algo.getDecompressor();
068    try (InputStream is = algo.createDecompressionStream(
069      new ByteArrayInputStream(compressed.toByteArray()), decompressor, 0)) {
070      IOUtils.readFully(is, result, 0, result.length);
071    } finally {
072      algo.returnDecompressor(decompressor);
073    }
074    assertArrayEquals(data, result);
075  }
076}