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  
23  import org.apache.commons.io.IOUtils;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.io.ByteBuffInputStream;
26  import org.apache.hadoop.hbase.io.TagCompressionContext;
27  import org.apache.hadoop.hbase.io.compress.Compression;
28  import org.apache.hadoop.hbase.io.crypto.Cipher;
29  import org.apache.hadoop.hbase.io.crypto.Decryptor;
30  import org.apache.hadoop.hbase.io.crypto.Encryption;
31  import org.apache.hadoop.hbase.io.hfile.HFileContext;
32  import org.apache.hadoop.hbase.nio.ByteBuff;
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        ByteBuff blockBufferWithoutHeader, ByteBuff onDiskBlock) throws IOException {
55      final ByteBuffInputStream byteBuffInputStream = new ByteBuffInputStream(onDiskBlock);
56      InputStream dataInputStream = new DataInputStream(byteBuffInputStream);
57  
58      try {
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 = dataInputStream.read();
76          if (ivLength > 0) {
77            byte[] iv = new byte[ivLength];
78            IOUtils.readFully(dataInputStream, 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            dataInputStream = decryptor.createDecryptionStream(dataInputStream);
85          }
86          onDiskSizeWithoutHeader -= Bytes.SIZEOF_BYTE + ivLength;
87        }
88  
89        Compression.Algorithm compression = fileContext.getCompression();
90        assert blockBufferWithoutHeader.hasArray();
91        if (compression != Compression.Algorithm.NONE) {
92          Compression.decompress(blockBufferWithoutHeader.array(),
93              blockBufferWithoutHeader.arrayOffset(), dataInputStream, onDiskSizeWithoutHeader,
94              uncompressedSizeWithoutHeader, compression);
95        } else {
96          IOUtils.readFully(dataInputStream, blockBufferWithoutHeader.array(),
97              blockBufferWithoutHeader.arrayOffset(), onDiskSizeWithoutHeader);
98        }
99      } finally {
100       byteBuffInputStream.close();
101       dataInputStream.close();
102     }
103   }
104 
105   @Override
106   public HFileContext getHFileContext() {
107     return this.fileContext;
108   }
109 
110   public TagCompressionContext getTagCompressionContext() {
111     return tagCompressionContext;
112   }
113 
114   public void setTagCompressionContext(TagCompressionContext tagCompressionContext) {
115     this.tagCompressionContext = tagCompressionContext;
116   }
117 }