View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.io.encoding;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.DataInputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.nio.ByteBuffer;
24  
25  import org.apache.hadoop.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.io.compress.Compression;
27  import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
28  
29  /**
30   * A default implementation of {@link HFileBlockDecodingContext}. It assumes the
31   * block data section is compressed as a whole.
32   *
33   * @see HFileBlockDefaultEncodingContext for the default compression context
34   *
35   */
36  @InterfaceAudience.Private
37  public class HFileBlockDefaultDecodingContext implements
38      HFileBlockDecodingContext {
39  
40    private final Compression.Algorithm compressAlgo;
41  
42    public HFileBlockDefaultDecodingContext(
43        Compression.Algorithm compressAlgo) {
44      this.compressAlgo = compressAlgo;
45    }
46  
47    @Override
48    public void prepareDecoding(int onDiskSizeWithoutHeader, int uncompressedSizeWithoutHeader,
49        ByteBuffer blockBufferWithoutHeader, byte[] onDiskBlock, int offset) throws IOException {
50      DataInputStream dis = new DataInputStream(new ByteArrayInputStream(onDiskBlock, offset,
51          onDiskSizeWithoutHeader));
52  
53      Compression.decompress(blockBufferWithoutHeader.array(),
54        blockBufferWithoutHeader.arrayOffset(), (InputStream) dis, onDiskSizeWithoutHeader,
55        uncompressedSizeWithoutHeader, compressAlgo);
56    }
57  
58    @Override
59    public Algorithm getCompression() {
60      return compressAlgo;
61    }
62  
63  }