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 * The portion of this file denoted by 'Copied from com.google.protobuf.CodedOutputStream'
19 * is from Protocol Buffers v2.5.0 under the following license
21 * Copyright 2008 Google Inc. All rights reserved.
22 * http://code.google.com/p/protobuf/
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions are
28 * * Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * * Redistributions in binary form must reproduce the above
31 * copyright notice, this list of conditions and the following disclaimer
32 * in the documentation and/or other materials provided with the
34 * * Neither the name of Google Inc. nor the names of its
35 * contributors may be used to endorse or promote products derived from
36 * this software without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 package org
.apache
.hadoop
.hbase
.util
;
53 import org
.apache
.yetus
.audience
.InterfaceAudience
;
56 * Extends the basic {@link AbstractPositionedByteRange} implementation with
57 * position support and it is a mutable version. {@code position} is considered transient,
58 * not fundamental to the definition of the range, and does not participate in
59 * {@link #compareTo(ByteRange)}, {@link #hashCode()}, or
60 * {@link #equals(Object)}. {@code Position} is retained by copy operations.
62 @InterfaceAudience.Public
63 @edu.umd
.cs
.findbugs
.annotations
.SuppressWarnings("EQ_DOESNT_OVERRIDE_EQUALS")
64 public class SimplePositionedMutableByteRange
extends AbstractPositionedByteRange
{
66 * Create a new {@code PositionedByteRange} lacking a backing array and with
67 * an undefined viewport.
69 public SimplePositionedMutableByteRange() {
74 * Create a new {@code PositionedByteRange} over a new backing array of size
75 * {@code capacity}. The range's offset and length are 0 and {@code capacity},
79 * the size of the backing array.
81 public SimplePositionedMutableByteRange(int capacity
) {
82 this(new byte[capacity
]);
86 * Create a new {@code PositionedByteRange} over the provided {@code bytes}.
91 public SimplePositionedMutableByteRange(byte[] bytes
) {
96 * Create a new {@code PositionedByteRange} over the provided {@code bytes}.
101 * The offset into {@code bytes} considered the beginning of this
104 * The length of this range.
106 public SimplePositionedMutableByteRange(byte[] bytes
, int offset
, int length
) {
107 set(bytes
, offset
, length
);
111 public PositionedByteRange
unset() {
121 public PositionedByteRange
set(int capacity
) {
124 this.limit
= capacity
;
129 public PositionedByteRange
set(byte[] bytes
) {
132 this.limit
= bytes
.length
;
137 public PositionedByteRange
set(byte[] bytes
, int offset
, int length
) {
139 super.set(bytes
, offset
, length
);
145 * Update the beginning of this range. {@code offset + length} may not be
146 * greater than {@code bytes.length}. Resets {@code position} to 0.
149 * the new start of this range.
153 public PositionedByteRange
setOffset(int offset
) {
155 super.setOffset(offset
);
160 * Update the length of this range. {@code offset + length} should not be
161 * greater than {@code bytes.length}. If {@code position} is greater than the
162 * new {@code length}, sets {@code position} to {@code length}.
165 * The new length of this range.
169 public PositionedByteRange
setLength(int length
) {
170 this.position
= Math
.min(position
, length
);
171 super.setLength(length
);
176 public PositionedByteRange
put(byte val
) {
177 put(position
++, val
);
182 public PositionedByteRange
put(byte[] val
) {
185 return this.put(val
, 0, val
.length
);
189 public PositionedByteRange
put(byte[] val
, int offset
, int length
) {
192 put(position
, val
, offset
, length
);
193 this.position
+= length
;
198 public PositionedByteRange
get(int index
, byte[] dst
) {
199 super.get(index
, dst
);
204 public PositionedByteRange
get(int index
, byte[] dst
, int offset
, int length
) {
205 super.get(index
, dst
, offset
, length
);
210 public PositionedByteRange
put(int index
, byte val
) {
211 bytes
[offset
+ index
] = val
;
216 public PositionedByteRange
put(int index
, byte[] val
) {
219 return put(index
, val
, 0, val
.length
);
223 public PositionedByteRange
put(int index
, byte[] val
, int offset
, int length
) {
226 System
.arraycopy(val
, offset
, this.bytes
, this.offset
+ index
, length
);
231 public PositionedByteRange
deepCopy() {
232 SimplePositionedMutableByteRange clone
= new SimplePositionedMutableByteRange(
233 deepCopyToNewArray());
234 clone
.position
= this.position
;
239 public PositionedByteRange
shallowCopy() {
240 SimplePositionedMutableByteRange clone
= new SimplePositionedMutableByteRange(bytes
, offset
,
242 clone
.position
= this.position
;
247 public PositionedByteRange
shallowCopySubRange(int innerOffset
, int copyLength
) {
248 SimplePositionedMutableByteRange clone
= new SimplePositionedMutableByteRange(bytes
, offset
249 + innerOffset
, copyLength
);
250 clone
.position
= this.position
;
255 public PositionedByteRange
putShort(short val
) {
256 putShort(position
, val
);
257 position
+= Bytes
.SIZEOF_SHORT
;
262 public PositionedByteRange
putInt(int val
) {
263 putInt(position
, val
);
264 position
+= Bytes
.SIZEOF_INT
;
269 public PositionedByteRange
putLong(long val
) {
270 putLong(position
, val
);
271 position
+= Bytes
.SIZEOF_LONG
;
276 public int putVLong(long val
) {
277 int len
= putVLong(position
, val
);
283 public PositionedByteRange
putShort(int index
, short val
) {
284 // This writing is same as BB's putShort. When byte[] is wrapped in a BB and
286 // one can get the same result.
287 bytes
[offset
+ index
+ 1] = (byte) val
;
289 bytes
[offset
+ index
] = (byte) val
;
295 public PositionedByteRange
putInt(int index
, int val
) {
296 // This writing is same as BB's putInt. When byte[] is wrapped in a BB and
297 // call getInt(), one
298 // can get the same result.
299 for (int i
= Bytes
.SIZEOF_INT
- 1; i
> 0; i
--) {
300 bytes
[offset
+ index
+ i
] = (byte) val
;
303 bytes
[offset
+ index
] = (byte) val
;
309 public PositionedByteRange
putLong(int index
, long val
) {
310 // This writing is same as BB's putLong. When byte[] is wrapped in a BB and
311 // call putLong(), one
312 // can get the same result.
313 for (int i
= Bytes
.SIZEOF_LONG
- 1; i
> 0; i
--) {
314 bytes
[offset
+ index
+ i
] = (byte) val
;
317 bytes
[offset
+ index
] = (byte) val
;
322 // Copied from com.google.protobuf.CodedOutputStream v2.5.0 writeRawVarint64
324 public int putVLong(int index
, long val
) {
327 if ((val
& ~
0x7F) == 0) {
328 bytes
[offset
+ index
+ rPos
] = (byte) val
;
331 bytes
[offset
+ index
+ rPos
] = (byte) ((val
& 0x7F) | 0x80);
339 // end copied from protobuf