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