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