1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37
38
39
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
65
66
67
68
69
70
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
79
80
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 }