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.commons.io.IOUtils;
25 import org.apache.hadoop.classification.InterfaceAudience;
26 import org.apache.hadoop.hbase.Cell;
27 import org.apache.hadoop.hbase.CellUtil;
28 import org.apache.hadoop.hbase.util.Bytes;
29
30
31
32
33
34
35 @InterfaceAudience.Private
36 public class CellCodec implements Codec {
37 static class CellEncoder extends BaseEncoder {
38 CellEncoder(final OutputStream out) {
39 super(out);
40 }
41
42 @Override
43 public void write(Cell cell) throws IOException {
44 checkFlushed();
45
46 write(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
47
48 write(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
49
50 write(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
51
52 this.out.write(Bytes.toBytes(cell.getTimestamp()));
53
54 this.out.write(cell.getTypeByte());
55
56 write(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
57 }
58
59
60
61
62
63
64
65
66 private void write(final byte [] bytes, final int offset, final int length)
67 throws IOException {
68 this.out.write(Bytes.toBytes(length));
69 this.out.write(bytes, offset, length);
70 }
71 }
72
73 static class CellDecoder extends BaseDecoder {
74 public CellDecoder(final InputStream in) {
75 super(in);
76 }
77
78 protected Cell parseCell() throws IOException {
79 byte [] row = readByteArray(this.in);
80 byte [] family = readByteArray(in);
81 byte [] qualifier = readByteArray(in);
82 byte [] longArray = new byte[Bytes.SIZEOF_LONG];
83 IOUtils.readFully(this.in, longArray);
84 long timestamp = Bytes.toLong(longArray);
85 byte type = (byte) this.in.read();
86 byte [] value = readByteArray(in);
87 return CellUtil.createCell(row, family, qualifier, timestamp, type, value);
88 }
89
90
91
92
93
94 private byte [] readByteArray(final InputStream in) throws IOException {
95 byte [] intArray = new byte[Bytes.SIZEOF_INT];
96 IOUtils.readFully(in, intArray);
97 int length = Bytes.toInt(intArray);
98 byte [] bytes = new byte [length];
99 IOUtils.readFully(in, bytes);
100 return bytes;
101 }
102 }
103
104 @Override
105 public Decoder getDecoder(InputStream is) {
106 return new CellDecoder(is);
107 }
108
109 @Override
110 public Encoder getEncoder(OutputStream os) {
111 return new CellEncoder(os);
112 }
113 }