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.protobuf.generated.CellProtos;
31
32 import com.google.protobuf.ByteString;
33 import org.apache.hadoop.classification.InterfaceStability;
34
35
36
37
38
39 @InterfaceAudience.Public
40 @InterfaceStability.Evolving
41 public class MessageCodec implements Codec {
42 static class MessageEncoder extends BaseEncoder {
43 MessageEncoder(final OutputStream out) {
44 super(out);
45 }
46
47 @Override
48 public void write(Cell cell) throws IOException {
49 checkFlushed();
50 CellProtos.Cell.Builder builder = CellProtos.Cell.newBuilder();
51
52
53 builder.setRow(ByteString.copyFrom(cell.getRowArray(), cell.getRowOffset(),
54 cell.getRowLength()));
55 builder.setFamily(ByteString.copyFrom(cell.getFamilyArray(), cell.getFamilyOffset(),
56 cell.getFamilyLength()));
57 builder.setQualifier(ByteString.copyFrom(cell.getQualifierArray(), cell.getQualifierOffset(),
58 cell.getQualifierLength()));
59 builder.setTimestamp(cell.getTimestamp());
60 builder.setCellType(CellProtos.CellType.valueOf(cell.getTypeByte()));
61 builder.setValue(ByteString.copyFrom(cell.getValueArray(), cell.getValueOffset(),
62 cell.getValueLength()));
63 CellProtos.Cell pbcell = builder.build();
64 pbcell.writeDelimitedTo(this.out);
65 }
66 }
67
68 static class MessageDecoder extends BaseDecoder {
69 MessageDecoder(final InputStream in) {
70 super(in);
71 }
72
73 protected Cell parseCell() throws IOException {
74 CellProtos.Cell pbcell = CellProtos.Cell.parseDelimitedFrom(this.in);
75 return CellUtil.createCell(pbcell.getRow().toByteArray(),
76 pbcell.getFamily().toByteArray(), pbcell.getQualifier().toByteArray(),
77 pbcell.getTimestamp(), (byte)pbcell.getCellType().getNumber(),
78 pbcell.getValue().toByteArray());
79 }
80 }
81
82 @Override
83 public Decoder getDecoder(InputStream is) {
84 return new MessageDecoder(is);
85 }
86
87 @Override
88 public Encoder getEncoder(OutputStream os) {
89 return new MessageEncoder(os);
90 }
91 }