1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.hadoop.classification.InterfaceAudience;
26 import org.apache.hadoop.classification.InterfaceStability;
27 import org.apache.hadoop.hbase.util.Bytes;
28
29
30
31
32 @InterfaceAudience.Private
33 @InterfaceStability.Evolving
34 public class Tag {
35 public final static int TYPE_LENGTH_SIZE = Bytes.SIZEOF_BYTE;
36 public final static int TAG_LENGTH_SIZE = Bytes.SIZEOF_SHORT;
37 public final static int INFRASTRUCTURE_SIZE = TYPE_LENGTH_SIZE + TAG_LENGTH_SIZE;
38
39 private final byte type;
40 private final byte[] bytes;
41 private int offset = 0;
42 private short length = 0;
43
44
45
46
47
48 public Tag(byte tagType, String tag) {
49 this(tagType, Bytes.toBytes(tag));
50 }
51
52
53
54
55
56 public Tag(byte tagType, byte[] tag) {
57
58
59
60 short tagLength = (short) ((tag.length & 0x0000ffff) + TYPE_LENGTH_SIZE);
61 length = (short) (TAG_LENGTH_SIZE + tagLength);
62 bytes = new byte[length];
63 int pos = Bytes.putShort(bytes, 0, tagLength);
64 pos = Bytes.putByte(bytes, pos, tagType);
65 Bytes.putBytes(bytes, pos, tag, 0, tag.length);
66 this.type = tagType;
67 }
68
69
70
71
72
73
74
75
76
77
78
79 public Tag(byte[] bytes, int offset) {
80 this(bytes, offset, getLength(bytes, offset));
81 }
82
83 private static short getLength(byte[] bytes, int offset) {
84 return (short) (TAG_LENGTH_SIZE + Bytes.toShort(bytes, offset));
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98 public Tag(byte[] bytes, int offset, short length) {
99 this.bytes = bytes;
100 this.offset = offset;
101 this.length = length;
102 this.type = bytes[offset + TAG_LENGTH_SIZE];
103 }
104
105
106
107
108 public byte[] getBuffer() {
109 return this.bytes;
110 }
111
112
113
114
115 public byte getType() {
116 return this.type;
117 }
118
119
120
121
122 public int getTagLength() {
123 return this.length - INFRASTRUCTURE_SIZE;
124 }
125
126
127
128
129 public int getTagOffset() {
130 return this.offset + INFRASTRUCTURE_SIZE;
131 }
132
133 public byte[] getValue() {
134 int tagLength = getTagLength();
135 byte[] tag = new byte[tagLength];
136 Bytes.putBytes(tag, 0, bytes, getTagOffset(), tagLength);
137 return tag;
138 }
139
140
141
142
143
144
145
146
147
148 public static List<Tag> asList(byte[] b, int offset, int length) {
149 List<Tag> tags = new ArrayList<Tag>();
150 int pos = offset;
151 while (pos < offset + length) {
152 short tagLen = Bytes.toShort(b, pos);
153 tags.add(new Tag(b, pos, (short) (tagLen + TAG_LENGTH_SIZE)));
154 pos += TAG_LENGTH_SIZE + tagLen;
155 }
156 return tags;
157 }
158
159
160
161
162
163
164
165
166
167 public static Tag getTag(byte[] b, int offset, int length, byte type) {
168 int pos = offset;
169 while (pos < offset + length) {
170 short tagLen = Bytes.toShort(b, pos);
171 if(b[pos + TAG_LENGTH_SIZE] == type) {
172 return new Tag(b, pos, (short) (tagLen + TAG_LENGTH_SIZE));
173 }
174 pos += TAG_LENGTH_SIZE + tagLen;
175 }
176 return null;
177 }
178
179
180
181
182 short getLength() {
183 return this.length;
184 }
185
186
187
188
189 int getOffset() {
190 return this.offset;
191 }
192 }