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