2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.util
;
20 import org
.apache
.yetus
.audience
.InterfaceAudience
;
21 import org
.apache
.yetus
.audience
.InterfaceStability
;
24 * Extends the basic {@link SimpleByteRange} implementation with position
25 * support. {@code position} is considered transient, not fundamental to the
26 * definition of the range, and does not participate in
27 * {@link #compareTo(ByteRange)}, {@link #hashCode()}, or
28 * {@link #equals(Object)}. {@code Position} is retained by copy operations.
30 @InterfaceAudience.Private
31 @InterfaceStability.Evolving
32 public abstract class AbstractPositionedByteRange
extends AbstractByteRange
implements
35 * The current index into the range. Like {@link java.nio.ByteBuffer} position, it
36 * points to the next value that will be read/written in the array. It
37 * provides the appearance of being 0-indexed, even though its value is
38 * calculated according to offset.
40 * Position is considered transient and does not participate in
41 * {@link #equals(Object)} or {@link #hashCode()} comparisons.
44 protected int position
= 0;
46 protected int limit
= 0;
49 public PositionedByteRange
set(int capacity
) {
52 this.limit
= capacity
;
57 public PositionedByteRange
set(byte[] bytes
) {
60 this.limit
= bytes
.length
;
65 public PositionedByteRange
set(byte[] bytes
, int offset
, int length
) {
67 super.set(bytes
, offset
, length
);
73 * Update the beginning of this range. {@code offset + length} may not be
74 * greater than {@code bytes.length}. Resets {@code position} to 0.
77 * the new start of this range.
81 public PositionedByteRange
setOffset(int offset
) {
83 super.setOffset(offset
);
88 * Update the length of this range. {@code offset + length} should not be
89 * greater than {@code bytes.length}. If {@code position} is greater than the
90 * new {@code length}, sets {@code position} to {@code length}.
93 * The new length of this range.
97 public PositionedByteRange
setLength(int length
) {
98 this.position
= Math
.min(position
, length
);
99 super.setLength(length
);
104 public int getPosition() {
109 public PositionedByteRange
setPosition(int position
) {
110 this.position
= position
;
115 public int getRemaining() {
116 return length
- position
;
121 return bytes
[offset
+ position
];
126 return get(position
++);
130 public PositionedByteRange
get(byte[] dst
) {
131 if (0 == dst
.length
) {
135 return this.get(dst
, 0, dst
.length
); // be clear we're calling self, not super
139 public PositionedByteRange
get(byte[] dst
, int offset
, int length
) {
144 super.get(this.position
, dst
, offset
, length
);
145 this.position
+= length
;
152 public PositionedByteRange
get(int index
, byte[] dst
) {
153 super.get(index
, dst
);
158 public PositionedByteRange
get(int index
, byte[] dst
, int offset
, int length
) {
159 super.get(index
, dst
, offset
, length
);
164 public short getShort() {
165 short s
= getShort(position
);
166 position
+= Bytes
.SIZEOF_SHORT
;
171 public int getInt() {
172 int i
= getInt(position
);
173 position
+= Bytes
.SIZEOF_INT
;
178 public long getLong() {
179 long l
= getLong(position
);
180 position
+= Bytes
.SIZEOF_LONG
;
185 public long getVLong() {
186 long p
= getVLong(position
);
187 position
+= getVLongSize(p
);
192 public PositionedByteRange
setLimit(int limit
) {
198 public int getLimit() {