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.hbase.io.compress.Compression;
26  import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
27  
28  /**
29   * A default implementation of {@link HFileBlockDecodingContext}. It assumes the
30   * block data section is compressed as a whole.
31   *
32   * @see HFileBlockDefaultEncodingContext for the default compression context
33   *
34   */
35  public class HFileBlockDefaultDecodingContext implements
36      HFileBlockDecodingContext {
37  
38    private final Compression.Algorithm compressAlgo;
39  
40    public HFileBlockDefaultDecodingContext(
41        Compression.Algorithm compressAlgo) {
42      this.compressAlgo = compressAlgo;
43    }
44  
45    @Override
46    public void prepareDecoding(int onDiskSizeWithoutHeader, int uncompressedSizeWithoutHeader,
47        ByteBuffer blockBufferWithoutHeader, byte[] onDiskBlock, int offset) throws IOException {
48      DataInputStream dis = new DataInputStream(new ByteArrayInputStream(onDiskBlock, offset,
49          onDiskSizeWithoutHeader));
50  
51      Compression.decompress(blockBufferWithoutHeader.array(),
52        blockBufferWithoutHeader.arrayOffset(), (InputStream) dis, onDiskSizeWithoutHeader,
53        uncompressedSizeWithoutHeader, compressAlgo);
54    }
55  
56    @Override
57    public Algorithm getCompression() {
58      return compressAlgo;
59    }
60  
61  }