1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.codec;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.Cell;
26 import org.apache.hadoop.hbase.CellUtil;
27 import org.apache.hadoop.hbase.codec.BaseDecoder;
28 import org.apache.hadoop.hbase.codec.BaseEncoder;
29 import org.apache.hadoop.hbase.codec.Codec;
30 import org.apache.hadoop.hbase.codec.CodecException;
31 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
32
33 import com.google.protobuf.ByteString;
34 import org.apache.hadoop.classification.InterfaceStability;
35
36
37
38
39
40 @InterfaceAudience.Public
41 @InterfaceStability.Evolving
42 public class MessageCodec implements Codec {
43 static class MessageEncoder extends BaseEncoder {
44 MessageEncoder(final OutputStream out) {
45 super(out);
46 }
47
48 @Override
49 public void write(Cell cell) throws IOException {
50 checkFlushed();
51 HBaseProtos.Cell.Builder builder = HBaseProtos.Cell.newBuilder();
52
53
54 builder.setRow(ByteString.copyFrom(cell.getRowArray(), cell.getRowOffset(),
55 cell.getRowLength()));
56 builder.setFamily(ByteString.copyFrom(cell.getFamilyArray(), cell.getFamilyOffset(),
57 cell.getFamilyLength()));
58 builder.setQualifier(ByteString.copyFrom(cell.getQualifierArray(), cell.getQualifierOffset(),
59 cell.getQualifierLength()));
60 builder.setTimestamp(cell.getTimestamp());
61 builder.setCellType(HBaseProtos.CellType.valueOf(cell.getTypeByte()));
62 builder.setValue(ByteString.copyFrom(cell.getValueArray(), cell.getValueOffset(),
63 cell.getValueLength()));
64 HBaseProtos.Cell pbcell = builder.build();
65 try {
66 pbcell.writeDelimitedTo(this.out);
67 } catch (IOException e) {
68 throw new CodecException(e);
69 }
70 }
71 }
72
73 static class MessageDecoder extends BaseDecoder {
74 MessageDecoder(final InputStream in) {
75 super(in);
76 }
77
78 protected Cell parseCell() throws IOException {
79 HBaseProtos.Cell pbcell = HBaseProtos.Cell.parseDelimitedFrom(this.in);
80 return CellUtil.createCell(pbcell.getRow().toByteArray(),
81 pbcell.getFamily().toByteArray(), pbcell.getQualifier().toByteArray(),
82 pbcell.getTimestamp(), (byte)pbcell.getCellType().getNumber(),
83 pbcell.getValue().toByteArray());
84 }
85 }
86
87 @Override
88 public Decoder getDecoder(InputStream is) {
89 return new MessageDecoder(is);
90 }
91
92 @Override
93 public Encoder getEncoder(OutputStream os) {
94 return new MessageEncoder(os);
95 }
96 }