/
BaseVector.java
96 lines (86 loc) · 2.64 KB
/
BaseVector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.flatbuffers;
import java.nio.ByteBuffer;
/// @cond FLATBUFFERS_INTERNAL
/**
* All vector access objects derive from this class, and add their own accessors.
*/
public class BaseVector {
/** Used to hold the vector data position. */
private int vector;
/** Used to hold the vector size. */
private int length;
/** Used to hold the vector element size in table. */
private int element_size;
/** The underlying ByteBuffer to hold the data of the vector. */
protected ByteBuffer bb;
/**
* Get the start data of a vector.
*
* @return Returns the start of the vector data.
*/
protected int __vector() {
return vector;
}
/**
* Gets the element position in vector's ByteBuffer.
*
* @param j An `int` index of element into a vector.
* @return Returns the position of the vector element in a ByteBuffer.
*/
protected int __element(int j) {
return vector + j * element_size;
}
/**
* Re-init the internal state with an external buffer {@code ByteBuffer}, an offset within and
* element size.
*
* This method exists primarily to allow recycling vector instances without risking memory leaks
* due to {@code ByteBuffer} references.
*/
protected void __reset(int _vector, int _element_size, ByteBuffer _bb) {
bb = _bb;
if (bb != null) {
vector = _vector;
length = bb.getInt(_vector - Constants.SIZEOF_INT);
element_size = _element_size;
} else {
vector = 0;
length = 0;
element_size = 0;
}
}
/**
* Resets the internal state with a null {@code ByteBuffer} and a zero position.
*
* This method exists primarily to allow recycling vector instances without risking memory leaks
* due to {@code ByteBuffer} references. The instance will be unusable until it is assigned
* again to a {@code ByteBuffer}.
*/
public void reset() {
__reset(0, 0, null);
}
/**
* Get the length of a vector.
*
* @return Returns the length of the vector.
*/
public int length() {
return length;
}
}
/// @endcond