• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 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 static com.google.flatbuffers.Constants.*;
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22 import java.nio.charset.Charset;
23 
24 /// @cond FLATBUFFERS_INTERNAL
25 
26 /**
27  * All tables in the generated code derive from this class, and add their own accessors.
28  */
29 public class Table {
30   public final static ThreadLocal<Charset> UTF8_CHARSET = new ThreadLocal<Charset>() {
31     @Override
32     protected Charset initialValue() {
33       return Charset.forName("UTF-8");
34     }
35   };
36   /** Used to hold the position of the `bb` buffer. */
37   protected int bb_pos;
38   /** The underlying ByteBuffer to hold the data of the Table. */
39   protected ByteBuffer bb;
40   /** Used to hold the vtable position. */
41   protected int vtable_start;
42   /** Used to hold the vtable size. */
43   protected int vtable_size;
44   Utf8 utf8 = Utf8.getDefault();
45 
46   /**
47    * Get the underlying ByteBuffer.
48    *
49    * @return Returns the Table's ByteBuffer.
50    */
getByteBuffer()51   public ByteBuffer getByteBuffer() { return bb; }
52 
53   /**
54    * Look up a field in the vtable.
55    *
56    * @param vtable_offset An `int` offset to the vtable in the Table's ByteBuffer.
57    * @return Returns an offset into the object, or `0` if the field is not present.
58    */
__offset(int vtable_offset)59   protected int __offset(int vtable_offset) {
60     return vtable_offset < vtable_size ? bb.getShort(vtable_start + vtable_offset) : 0;
61   }
62 
__offset(int vtable_offset, int offset, ByteBuffer bb)63   protected static int __offset(int vtable_offset, int offset, ByteBuffer bb) {
64     int vtable = bb.capacity() - offset;
65     return bb.getShort(vtable + vtable_offset - bb.getInt(vtable)) + vtable;
66   }
67 
68   /**
69    * Retrieve a relative offset.
70    *
71    * @param offset An `int` index into the Table's ByteBuffer containing the relative offset.
72    * @return Returns the relative offset stored at `offset`.
73    */
__indirect(int offset)74   protected int __indirect(int offset) {
75     return offset + bb.getInt(offset);
76   }
77 
__indirect(int offset, ByteBuffer bb)78   protected static int __indirect(int offset, ByteBuffer bb) {
79     return offset + bb.getInt(offset);
80   }
81 
82   /**
83    * Create a Java `String` from UTF-8 data stored inside the FlatBuffer.
84    *
85    * This allocates a new string and converts to wide chars upon each access,
86    * which is not very efficient. Instead, each FlatBuffer string also comes with an
87    * accessor based on __vector_as_bytebuffer below, which is much more efficient,
88    * assuming your Java program can handle UTF-8 data directly.
89    *
90    * @param offset An `int` index into the Table's ByteBuffer.
91    * @return Returns a `String` from the data stored inside the FlatBuffer at `offset`.
92    */
__string(int offset)93   protected String __string(int offset) {
94     offset += bb.getInt(offset);
95     int length = bb.getInt(offset);
96     return utf8.decodeUtf8(bb, offset + SIZEOF_INT, length);
97   }
98 
99   /**
100    * Get the length of a vector.
101    *
102    * @param offset An `int` index into the Table's ByteBuffer.
103    * @return Returns the length of the vector whose offset is stored at `offset`.
104    */
__vector_len(int offset)105   protected int __vector_len(int offset) {
106     offset += bb_pos;
107     offset += bb.getInt(offset);
108     return bb.getInt(offset);
109   }
110 
111   /**
112    * Get the start data of a vector.
113    *
114    * @param offset An `int` index into the Table's ByteBuffer.
115    * @return Returns the start of the vector data whose offset is stored at `offset`.
116    */
__vector(int offset)117   protected int __vector(int offset) {
118     offset += bb_pos;
119     return offset + bb.getInt(offset) + SIZEOF_INT;  // data starts after the length
120   }
121 
122   /**
123    * Get a whole vector as a ByteBuffer.
124    *
125    * This is efficient, since it only allocates a new {@link ByteBuffer} object,
126    * but does not actually copy the data, it still refers to the same bytes
127    * as the original ByteBuffer. Also useful with nested FlatBuffers, etc.
128    *
129    * @param vector_offset The position of the vector in the byte buffer
130    * @param elem_size The size of each element in the array
131    * @return The {@link ByteBuffer} for the array
132    */
__vector_as_bytebuffer(int vector_offset, int elem_size)133   protected ByteBuffer __vector_as_bytebuffer(int vector_offset, int elem_size) {
134     int o = __offset(vector_offset);
135     if (o == 0) return null;
136     ByteBuffer bb = this.bb.duplicate().order(ByteOrder.LITTLE_ENDIAN);
137     int vectorstart = __vector(o);
138     bb.position(vectorstart);
139     bb.limit(vectorstart + __vector_len(o) * elem_size);
140     return bb;
141   }
142 
143   /**
144    * Initialize vector as a ByteBuffer.
145    *
146    * This is more efficient than using duplicate, since it doesn't copy the data
147    * nor allocattes a new {@link ByteBuffer}, creating no garbage to be collected.
148    *
149    * @param bb The {@link ByteBuffer} for the array
150    * @param vector_offset The position of the vector in the byte buffer
151    * @param elem_size The size of each element in the array
152    * @return The {@link ByteBuffer} for the array
153    */
__vector_in_bytebuffer(ByteBuffer bb, int vector_offset, int elem_size)154   protected ByteBuffer __vector_in_bytebuffer(ByteBuffer bb, int vector_offset, int elem_size) {
155     int o = this.__offset(vector_offset);
156     if (o == 0) return null;
157     int vectorstart = __vector(o);
158     bb.rewind();
159     bb.limit(vectorstart + __vector_len(o) * elem_size);
160     bb.position(vectorstart);
161     return bb;
162   }
163 
164   /**
165    * Initialize any Table-derived type to point to the union at the given `offset`.
166    *
167    * @param t A `Table`-derived type that should point to the union at `offset`.
168    * @param offset An `int` index into the Table's ByteBuffer.
169    * @return Returns the Table that points to the union at `offset`.
170    */
__union(Table t, int offset)171   protected Table __union(Table t, int offset) {
172     offset += bb_pos;
173     t.bb_pos = offset + bb.getInt(offset);
174     t.bb = bb;
175     t.vtable_start = t.bb_pos - bb.getInt(t.bb_pos);
176     t.vtable_size = bb.getShort(t.vtable_start);
177     return t;
178   }
179 
180   /**
181    * Check if a {@link ByteBuffer} contains a file identifier.
182    *
183    * @param bb A {@code ByteBuffer} to check if it contains the identifier
184    * `ident`.
185    * @param ident A `String` identifier of the FlatBuffer file.
186    * @return True if the buffer contains the file identifier
187    */
__has_identifier(ByteBuffer bb, String ident)188   protected static boolean __has_identifier(ByteBuffer bb, String ident) {
189     if (ident.length() != FILE_IDENTIFIER_LENGTH)
190         throw new AssertionError("FlatBuffers: file identifier must be length " +
191                                  FILE_IDENTIFIER_LENGTH);
192     for (int i = 0; i < FILE_IDENTIFIER_LENGTH; i++) {
193       if (ident.charAt(i) != (char)bb.get(bb.position() + SIZEOF_INT + i)) return false;
194     }
195     return true;
196   }
197 
198   /**
199    * Sort tables by the key.
200    *
201    * @param offsets An 'int' indexes of the tables into the bb.
202    * @param bb A {@code ByteBuffer} to get the tables.
203    */
sortTables(int[] offsets, final ByteBuffer bb)204   protected void sortTables(int[] offsets, final ByteBuffer bb) {
205     Integer[] off = new Integer[offsets.length];
206     for (int i = 0; i < offsets.length; i++) off[i] = offsets[i];
207     java.util.Arrays.sort(off, new java.util.Comparator<Integer>() {
208       public int compare(Integer o1, Integer o2) {
209         return keysCompare(o1, o2, bb);
210       }
211     });
212     for (int i = 0; i < offsets.length; i++) offsets[i] = off[i];
213   }
214 
215   /**
216    * Compare two tables by the key.
217    *
218    * @param o1 An 'Integer' index of the first key into the bb.
219    * @param o2 An 'Integer' index of the second key into the bb.
220    * @param bb A {@code ByteBuffer} to get the keys.
221    */
keysCompare(Integer o1, Integer o2, ByteBuffer bb)222   protected int keysCompare(Integer o1, Integer o2, ByteBuffer bb) { return 0; }
223 
224   /**
225    * Compare two strings in the buffer.
226    *
227    * @param offset_1 An 'int' index of the first string into the bb.
228    * @param offset_2 An 'int' index of the second string into the bb.
229    * @param bb A {@code ByteBuffer} to get the strings.
230    */
compareStrings(int offset_1, int offset_2, ByteBuffer bb)231   protected static int compareStrings(int offset_1, int offset_2, ByteBuffer bb) {
232     offset_1 += bb.getInt(offset_1);
233     offset_2 += bb.getInt(offset_2);
234     int len_1 = bb.getInt(offset_1);
235     int len_2 = bb.getInt(offset_2);
236     int startPos_1 = offset_1 + SIZEOF_INT;
237     int startPos_2 = offset_2 + SIZEOF_INT;
238     int len = Math.min(len_1, len_2);
239     for(int i = 0; i < len; i++) {
240       if (bb.get(i + startPos_1) != bb.get(i + startPos_2))
241         return bb.get(i + startPos_1) - bb.get(i + startPos_2);
242     }
243     return len_1 - len_2;
244   }
245 
246   /**
247    * Compare string from the buffer with the 'String' object.
248    *
249    * @param offset_1 An 'int' index of the first string into the bb.
250    * @param key Second string as a byte array.
251    * @param bb A {@code ByteBuffer} to get the first string.
252    */
compareStrings(int offset_1, byte[] key, ByteBuffer bb)253   protected static int compareStrings(int offset_1, byte[] key, ByteBuffer bb) {
254     offset_1 += bb.getInt(offset_1);
255     int len_1 = bb.getInt(offset_1);
256     int len_2 = key.length;
257     int startPos_1 = offset_1 + Constants.SIZEOF_INT;
258     int len = Math.min(len_1, len_2);
259     for (int i = 0; i < len; i++) {
260       if (bb.get(i + startPos_1) != key[i])
261         return bb.get(i + startPos_1) - key[i];
262     }
263     return len_1 - len_2;
264   }
265 
266   /**
267    * Resets the internal state with a null {@code ByteBuffer} and a zero position.
268    *
269    * This method exists primarily to allow recycling Table instances without risking memory leaks
270    * due to {@code ByteBuffer} references. The instance will be unusable until it is assigned
271    * again to a {@code ByteBuffer}.
272    */
__reset()273   public void __reset() {
274     bb = null;
275     bb_pos = 0;
276     vtable_start = 0;
277     vtable_size = 0;
278   }
279 }
280 
281 /// @endcond
282