1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.nio.ByteBuffer;
22
23 import org.apache.hadoop.classification.InterfaceAudience;
24 import org.apache.hadoop.classification.InterfaceStability;
25
26 import com.google.common.annotations.VisibleForTesting;
27
28
29
30
31
32
33
34 @InterfaceAudience.Public
35 @InterfaceStability.Evolving
36 public class SimplePositionedByteRange extends SimpleByteRange implements PositionedByteRange {
37
38
39
40
41
42
43
44
45
46
47
48 private int position = 0;
49
50
51
52
53
54 public SimplePositionedByteRange() {
55 super();
56 }
57
58
59
60
61
62
63
64 public SimplePositionedByteRange(int capacity) {
65 super(capacity);
66 }
67
68
69
70
71
72 public SimplePositionedByteRange(byte[] bytes) {
73 super(bytes);
74 }
75
76
77
78
79
80
81
82
83 public SimplePositionedByteRange(byte[] bytes, int offset, int length) {
84 super(bytes, offset, length);
85 }
86
87 @Override
88 public PositionedByteRange unset() {
89 this.position = 0;
90 super.unset();
91 return this;
92 }
93
94 @Override
95 public PositionedByteRange set(int capacity) {
96 this.position = 0;
97 super.set(capacity);
98 return this;
99 }
100
101 @Override
102 public PositionedByteRange set(byte[] bytes) {
103 this.position = 0;
104 super.set(bytes);
105 return this;
106 }
107
108 @Override
109 public PositionedByteRange set(byte[] bytes, int offset, int length) {
110 this.position = 0;
111 super.set(bytes, offset, length);
112 return this;
113 }
114
115
116
117
118
119
120
121 @Override
122 public PositionedByteRange setOffset(int offset) {
123 this.position = 0;
124 super.setOffset(offset);
125 return this;
126 }
127
128
129
130
131
132
133
134
135 @Override
136 public PositionedByteRange setLength(int length) {
137 this.position = Math.min(position, length);
138 super.setLength(length);
139 return this;
140 }
141
142 @Override
143 public int getPosition() { return position; }
144
145 @Override
146 public PositionedByteRange setPosition(int position) { this.position = position; return this; }
147
148 @Override
149 public int getRemaining() { return length - position; }
150
151 @Override
152 public byte peek() { return bytes[offset + position]; }
153
154 @Override
155 public byte get() { return get(position++); }
156
157 @Override
158 public PositionedByteRange get(byte[] dst) {
159 if (0 == dst.length) return this;
160 return this.get(dst, 0, dst.length);
161 }
162
163 @Override
164 public PositionedByteRange get(byte[] dst, int offset, int length) {
165 if (0 == length) return this;
166 super.get(this.position, dst, offset, length);
167 this.position += length;
168 return this;
169 }
170
171 @Override
172 public PositionedByteRange put(byte val) {
173 put(position++, val);
174 return this;
175 }
176
177 @Override
178 public PositionedByteRange put(byte[] val) {
179 if (0 == val.length) return this;
180 return this.put(val, 0, val.length);
181 }
182
183 @Override
184 public PositionedByteRange put(byte[] val, int offset, int length) {
185 if (0 == length) return this;
186 super.put(position, val, offset, length);
187 this.position += length;
188 return this;
189 }
190
191
192
193
194
195 @VisibleForTesting
196 PositionedByteRange flip() {
197 clearHashCache();
198 length = position;
199 position = offset;
200 return this;
201 }
202
203
204
205
206
207 @VisibleForTesting
208 PositionedByteRange clear() {
209 clearHashCache();
210 position = 0;
211 length = bytes.length - offset;
212 return this;
213 }
214
215
216
217 @Override
218 public PositionedByteRange get(int index, byte[] dst) { super.get(index, dst); return this; }
219
220 @Override
221 public PositionedByteRange get(int index, byte[] dst, int offset, int length) {
222 super.get(index, dst, offset, length);
223 return this;
224 }
225
226 @Override
227 public PositionedByteRange put(int index, byte val) { super.put(index, val); return this; }
228
229 @Override
230 public PositionedByteRange put(int index, byte[] val) { super.put(index, val); return this; }
231
232 @Override
233 public PositionedByteRange put(int index, byte[] val, int offset, int length) {
234 super.put(index, val, offset, length);
235 return this;
236 }
237
238 @Override
239 public PositionedByteRange deepCopy() {
240 SimplePositionedByteRange clone = new SimplePositionedByteRange(deepCopyToNewArray());
241 clone.position = this.position;
242 return clone;
243 }
244
245 @Override
246 public PositionedByteRange shallowCopy() {
247 SimplePositionedByteRange clone = new SimplePositionedByteRange(bytes, offset, length);
248 clone.position = this.position;
249 return clone;
250 }
251
252 @Override
253 public PositionedByteRange shallowCopySubRange(int innerOffset, int copyLength) {
254 SimplePositionedByteRange clone =
255 new SimplePositionedByteRange(bytes, offset + innerOffset, copyLength);
256 clone.position = this.position;
257 return clone;
258 }
259 }