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.DataInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.nio.ByteBuffer;
23  
24  import org.apache.commons.io.IOUtils;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.io.ByteBufferInputStream;
27  import org.apache.hadoop.hbase.io.TagCompressionContext;
28  import org.apache.hadoop.hbase.io.compress.Compression;
29  import org.apache.hadoop.hbase.io.crypto.Cipher;
30  import org.apache.hadoop.hbase.io.crypto.Decryptor;
31  import org.apache.hadoop.hbase.io.crypto.Encryption;
32  import org.apache.hadoop.hbase.io.hfile.HFileContext;
33  import org.apache.hadoop.hbase.util.Bytes;
34  
35  /**
36   * A default implementation of {@link HFileBlockDecodingContext}. It assumes the
37   * block data section is compressed as a whole.
38   *
39   * @see HFileBlockDefaultEncodingContext for the default compression context
40   *
41   */
42  @InterfaceAudience.Private
43  public class HFileBlockDefaultDecodingContext implements
44      HFileBlockDecodingContext {
45    private final HFileContext fileContext;
46    private TagCompressionContext tagCompressionContext;
47    
48    public HFileBlockDefaultDecodingContext(HFileContext fileContext) {
49      this.fileContext = fileContext;
50    }
51  
52    @Override
53    public void prepareDecoding(int onDiskSizeWithoutHeader, int uncompressedSizeWithoutHeader,
54        ByteBuffer blockBufferWithoutHeader, ByteBuffer onDiskBlock) throws IOException {
55      InputStream in = new DataInputStream(new ByteBufferInputStream(onDiskBlock));
56  
57      Encryption.Context cryptoContext = fileContext.getEncryptionContext();
58      if (cryptoContext != Encryption.Context.NONE) {
59  
60        Cipher cipher = cryptoContext.getCipher();
61        Decryptor decryptor = cipher.getDecryptor();
62        decryptor.setKey(cryptoContext.getKey());
63  
64        // Encrypted block format:
65        // +--------------------------+
66        // | byte iv length           |
67        // +--------------------------+
68        // | iv data ...              |
69        // +--------------------------+
70        // | encrypted block data ... |
71        // +--------------------------+
72  
73        int ivLength = in.read();
74        if (ivLength > 0) {
75          byte[] iv = new byte[ivLength];
76          IOUtils.readFully(in, iv);
77          decryptor.setIv(iv);
78          // All encrypted blocks will have a nonzero IV length. If we see an IV
79          // length of zero, this means the encoding context had 0 bytes of
80          // plaintext to encode.
81          decryptor.reset();
82          in = decryptor.createDecryptionStream(in);
83        }
84        onDiskSizeWithoutHeader -= Bytes.SIZEOF_BYTE + ivLength;
85      }
86  
87      Compression.Algorithm compression = fileContext.getCompression();
88      assert blockBufferWithoutHeader.hasArray();
89      if (compression != Compression.Algorithm.NONE) {
90        Compression.decompress(blockBufferWithoutHeader.array(),
91          blockBufferWithoutHeader.arrayOffset(), in, onDiskSizeWithoutHeader,
92          uncompressedSizeWithoutHeader, compression);
93      } else {
94        IOUtils.readFully(in, blockBufferWithoutHeader.array(),
95          blockBufferWithoutHeader.arrayOffset(), onDiskSizeWithoutHeader);
96      }
97    }
98  
99    @Override
100   public HFileContext getHFileContext() {
101     return this.fileContext;
102   }
103 
104   public TagCompressionContext getTagCompressionContext() {
105     return tagCompressionContext;
106   }
107 
108   public void setTagCompressionContext(TagCompressionContext tagCompressionContext) {
109     this.tagCompressionContext = tagCompressionContext;
110   }
111 }