ndimiduk commented on code in PR #6708: URL: https://github.com/apache/hbase/pull/6708#discussion_r1971657606
########## hbase-common/src/main/java/org/apache/hadoop/hbase/io/compress/BlockDecompressorHelper.java: ########## @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.io.compress; + +import java.io.IOException; +import org.apache.hadoop.hbase.nio.ByteBuff; +import org.apache.yetus.audience.InterfaceAudience; + [email protected] +public class BlockDecompressorHelper { + + public interface RawDecompressor { + int decompress(ByteBuff output, ByteBuff input, int inputLen) throws IOException; + } + + /** + * Helper to decompress a ByteBuff that was created by a + * {@link org.apache.hadoop.io.compress.BlockCompressorStream}, or is at least in the same format. + * Parses the binary format and delegates actual decompression work to the provided + * {@link RawDecompressor}. + */ + public static int decompress(ByteBuff output, ByteBuff input, int inputSize, + RawDecompressor rawDecompressor) throws IOException { + int totalDecompressedBytes = 0; + int compressedBytesConsumed = 0; + + while (compressedBytesConsumed < inputSize) { + int decompressedBlockSize = rawReadInt(input); + compressedBytesConsumed += 4; + int decompressedBytesInBlock = 0; + + while (decompressedBytesInBlock < decompressedBlockSize) { + int compressedChunkSize = rawReadInt(input); + compressedBytesConsumed += 4; + int n = rawDecompressor.decompress(output, input, compressedChunkSize); + if (n <= 0) { + throw new IOException("Decompression failed. Compressed size: " + compressedChunkSize + + ", decompressed size: " + decompressedBlockSize); + } + compressedBytesConsumed += compressedChunkSize; + decompressedBytesInBlock += n; + totalDecompressedBytes += n; + } + } + return totalDecompressedBytes; + } + + private static int rawReadInt(ByteBuff input) { Review Comment: Okay makes sense. Please add a comment to the method that makes note of this endian-specific implementation. Maybe in the future we'll update our ByteBuff utilities to account for specific endian-ness. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
