| Name | Size | Mode | Actions |
|---|---|---|---|
| ByteBufferUtil.java | 1611 | 0644 | editdlrm |
| Constants.java | 1967 | 0644 | editdlrm |
| FlatBufferBuilder.java | 38133 | 0644 | editdlrm |
| Struct.java | 1865 | 0644 | editdlrm |
| Table.java | 10272 | 0644 | editdlrm |
| Utf8.java | 7136 | 0644 | editdlrm |
| Utf8Old.java | 3232 | 0644 | editdlrm |
| Utf8Safe.java | 17907 | 0644 | editdlrm |
/srv/osrm/osrm-backend/third_party/flatbuffers/java/com/google/flatbuffers/FlatBufferBuilder.java (38133B)
* The expected sequence of calls is: *
* For example, to create an array of strings, do: *
{@code
* // Need 10 strings
* FlatBufferBuilder builder = new FlatBufferBuilder(existingBuffer);
* int[] offsets = new int[10];
*
* for (int i = 0; i < 10; i++) {
* offsets[i] = fbb.createString(" " + i);
* }
*
* // Have the strings in the buffer, but don't have a vector.
* // Add a vector that references the newly created strings:
* builder.startVector(4, offsets.length, 4);
*
* // Add each string to the newly created vector
* // The strings are added in reverse order since the buffer
* // is filled in back to front
* for (int i = offsets.length - 1; i >= 0; i--) {
* builder.addOffset(offsets[i]);
* }
*
* // Finish off the vector
* int offsetOfTheVector = fbb.endVector();
* }
*
* @param elem_size The size of each element in the array.
* @param num_elems The number of elements in the array.
* @param alignment The alignment of the array.
*/
public void startVector(int elem_size, int num_elems, int alignment) {
notNested();
vector_num_elems = num_elems;
prep(SIZEOF_INT, elem_size * num_elems);
prep(alignment, elem_size * num_elems); // Just in case alignment > int.
nested = true;
}
/**
* Finish off the creation of an array and all its elements. The array
* must be created with {@link #startVector(int, int, int)}.
*
* @return The offset at which the newly created array starts.
* @see #startVector(int, int, int)
*/
public int endVector() {
if (!nested)
throw new AssertionError("FlatBuffers: endVector called without startVector");
nested = false;
putInt(vector_num_elems);
return offset();
}
/// @endcond
/**
* Create a new array/vector and return a ByteBuffer to be filled later.
* Call {@link #endVector} after this method to get an offset to the beginning
* of vector.
*
* @param elem_size the size of each element in bytes.
* @param num_elems number of elements in the vector.
* @param alignment byte alignment.
* @return ByteBuffer with position and limit set to the space allocated for the array.
*/
public ByteBuffer createUnintializedVector(int elem_size, int num_elems, int alignment) {
int length = elem_size * num_elems;
startVector(elem_size, num_elems, alignment);
bb.position(space -= length);
// Slice and limit the copy vector to point to the 'array'
ByteBuffer copy = bb.slice().order(ByteOrder.LITTLE_ENDIAN);
copy.limit(length);
return copy;
}
/**
* Create a vector of tables.
*
* @param offsets Offsets of the tables.
* @return Returns offset of the vector.
*/
public int createVectorOfTables(int[] offsets) {
notNested();
startVector(Constants.SIZEOF_INT, offsets.length, Constants.SIZEOF_INT);
for(int i = offsets.length - 1; i >= 0; i--) addOffset(offsets[i]);
return endVector();
}
/**
* Create a vector of sorted by the key tables.
*
* @param obj Instance of the table subclass.
* @param offsets Offsets of the tables.
* @return Returns offset of the sorted vector.
*/
public * For example, using the "Monster" code found on the "landing page". An * object of type `Monster` can be created using the following code: * *
{@code
* int testArrayOfString = Monster.createTestarrayofstringVector(fbb, new int[] {
* fbb.createString("test1"),
* fbb.createString("test2")
* });
*
* Monster.startMonster(fbb);
* Monster.addPos(fbb, Vec3.createVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0,
* Color.Green, (short)5, (byte)6));
* Monster.addHp(fbb, (short)80);
* Monster.addName(fbb, str);
* Monster.addInventory(fbb, inv);
* Monster.addTestType(fbb, (byte)Any.Monster);
* Monster.addTest(fbb, mon2);
* Monster.addTest4(fbb, test4);
* Monster.addTestarrayofstring(fbb, testArrayOfString);
* int mon = Monster.endMonster(fbb);
* }
* * Here: *
* It's not recommended to call this method directly. If it's called manually, you must ensure * to audit all calls to it whenever fields are added or removed from your schema. This is * automatically done by the code generated by the `FlatBuffers` compiler. * * @param numfields The number of fields found in this object. */ public void startTable(int numfields) { notNested(); if (vtable == null || vtable.length < numfields) vtable = new int[numfields]; vtable_in_use = numfields; Arrays.fill(vtable, 0, vtable_in_use, 0); nested = true; object_start = offset(); } /** * Add a `boolean` to a table at `o` into its vtable, with value `x` and default `d`. * * @param o The index into the vtable. * @param x A `boolean` to put into the buffer, depending on how defaults are handled. If * `force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the * default value, it can be skipped. * @param d A `boolean` default value to compare against when `force_defaults` is `false`. */ public void addBoolean(int o, boolean x, boolean d) { if(force_defaults || x != d) { addBoolean(x); slot(o); } } /** * Add a `byte` to a table at `o` into its vtable, with value `x` and default `d`. * * @param o The index into the vtable. * @param x A `byte` to put into the buffer, depending on how defaults are handled. If * `force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the * default value, it can be skipped. * @param d A `byte` default value to compare against when `force_defaults` is `false`. */ public void addByte (int o, byte x, int d) { if(force_defaults || x != d) { addByte (x); slot(o); } } /** * Add a `short` to a table at `o` into its vtable, with value `x` and default `d`. * * @param o The index into the vtable. * @param x A `short` to put into the buffer, depending on how defaults are handled. If * `force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the * default value, it can be skipped. * @param d A `short` default value to compare against when `force_defaults` is `false`. */ public void addShort (int o, short x, int d) { if(force_defaults || x != d) { addShort (x); slot(o); } } /** * Add an `int` to a table at `o` into its vtable, with value `x` and default `d`. * * @param o The index into the vtable. * @param x An `int` to put into the buffer, depending on how defaults are handled. If * `force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the * default value, it can be skipped. * @param d An `int` default value to compare against when `force_defaults` is `false`. */ public void addInt (int o, int x, int d) { if(force_defaults || x != d) { addInt (x); slot(o); } } /** * Add a `long` to a table at `o` into its vtable, with value `x` and default `d`. * * @param o The index into the vtable. * @param x A `long` to put into the buffer, depending on how defaults are handled. If * `force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the * default value, it can be skipped. * @param d A `long` default value to compare against when `force_defaults` is `false`. */ public void addLong (int o, long x, long d) { if(force_defaults || x != d) { addLong (x); slot(o); } } /** * Add a `float` to a table at `o` into its vtable, with value `x` and default `d`. * * @param o The index into the vtable. * @param x A `float` to put into the buffer, depending on how defaults are handled. If * `force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the * default value, it can be skipped. * @param d A `float` default value to compare against when `force_defaults` is `false`. */ public void addFloat (int o, float x, double d) { if(force_defaults || x != d) { addFloat (x); slot(o); } } /** * Add a `double` to a table at `o` into its vtable, with value `x` and default `d`. * * @param o The index into the vtable. * @param x A `double` to put into the buffer, depending on how defaults are handled. If * `force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the * default value, it can be skipped. * @param d A `double` default value to compare against when `force_defaults` is `false`. */ public void addDouble (int o, double x, double d) { if(force_defaults || x != d) { addDouble (x); slot(o); } } /** * Add an `offset` to a table at `o` into its vtable, with value `x` and default `d`. * * @param o The index into the vtable. * @param x An `offset` to put into the buffer, depending on how defaults are handled. If * `force_defaults` is `false`, compare `x` against the default value `d`. If `x` contains the * default value, it can be skipped. * @param d An `offset` default value to compare against when `force_defaults` is `false`. */ public void addOffset (int o, int x, int d) { if(force_defaults || x != d) { addOffset (x); slot(o); } } /** * Add a struct to the table. Structs are stored inline, so nothing additional is being added. * * @param voffset The index into the vtable. * @param x The offset of the created struct. * @param d The default value is always `0`. */ public void addStruct(int voffset, int x, int d) { if(x != d) { Nested(x); slot(voffset); } } /** * Set the current vtable at `voffset` to the current location in the buffer. * * @param voffset The index into the vtable to store the offset relative to the end of the * buffer. */ public void slot(int voffset) { vtable[voffset] = offset(); } /** * Finish off writing the object that is under construction. * * @return The offset to the object inside {@link #dataBuffer()}. * @see #startTable(int) */ public int endTable() { if (vtable == null || !nested) throw new AssertionError("FlatBuffers: endTable called without startTable"); addInt(0); int vtableloc = offset(); // Write out the current vtable. int i = vtable_in_use - 1; // Trim trailing zeroes. for (; i >= 0 && vtable[i] == 0; i--) {} int trimmed_size = i + 1; for (; i >= 0 ; i--) { // Offset relative to the start of the table. short off = (short)(vtable[i] != 0 ? vtableloc - vtable[i] : 0); addShort(off); } final int standard_fields = 2; // The fields below: addShort((short)(vtableloc - object_start)); addShort((short)((trimmed_size + standard_fields) * SIZEOF_SHORT)); // Search for an existing vtable that matches the current one. int existing_vtable = 0; outer_loop: for (i = 0; i < num_vtables; i++) { int vt1 = bb.capacity() - vtables[i]; int vt2 = space; short len = bb.getShort(vt1); if (len == bb.getShort(vt2)) { for (int j = SIZEOF_SHORT; j < len; j += SIZEOF_SHORT) { if (bb.getShort(vt1 + j) != bb.getShort(vt2 + j)) { continue outer_loop; } } existing_vtable = vtables[i]; break outer_loop; } } if (existing_vtable != 0) { // Found a match: // Remove the current vtable. space = bb.capacity() - vtableloc; // Point table to existing vtable. bb.putInt(space, existing_vtable - vtableloc); } else { // No match: // Add the location of the current vtable to the list of vtables. if (num_vtables == vtables.length) vtables = Arrays.copyOf(vtables, num_vtables * 2); vtables[num_vtables++] = offset(); // Point table to current vtable. bb.putInt(bb.capacity() - vtableloc, offset() - vtableloc); } nested = false; return vtableloc; } /** * Checks that a required field has been set in a given table that has * just been constructed. * * @param table The offset to the start of the table from the `ByteBuffer` capacity. * @param field The offset to the field in the vtable. */ public void required(int table, int field) { int table_start = bb.capacity() - table; int vtable_start = table_start - bb.getInt(table_start); boolean ok = bb.getShort(vtable_start + field) != 0; // If this fails, the caller will show what field needs to be set. if (!ok) throw new AssertionError("FlatBuffers: field " + field + " must be set"); } /// @endcond /** * Finalize a buffer, pointing to the given `root_table`. * * @param root_table An offset to be added to the buffer. * @param size_prefix Whether to prefix the size to the buffer. */ protected void finish(int root_table, boolean size_prefix) { prep(minalign, SIZEOF_INT + (size_prefix ? SIZEOF_INT : 0)); addOffset(root_table); if (size_prefix) { addInt(bb.capacity() - space); } bb.position(space); finished = true; } /** * Finalize a buffer, pointing to the given `root_table`. * * @param root_table An offset to be added to the buffer. */ public void finish(int root_table) { finish(root_table, false); } /** * Finalize a buffer, pointing to the given `root_table`, with the size prefixed. * * @param root_table An offset to be added to the buffer. */ public void finishSizePrefixed(int root_table) { finish(root_table, true); } /** * Finalize a buffer, pointing to the given `root_table`. * * @param root_table An offset to be added to the buffer. * @param file_identifier A FlatBuffer file identifier to be added to the buffer before * `root_table`. * @param size_prefix Whether to prefix the size to the buffer. */ protected void finish(int root_table, String file_identifier, boolean size_prefix) { prep(minalign, SIZEOF_INT + FILE_IDENTIFIER_LENGTH + (size_prefix ? SIZEOF_INT : 0)); if (file_identifier.length() != FILE_IDENTIFIER_LENGTH) throw new AssertionError("FlatBuffers: file identifier must be length " + FILE_IDENTIFIER_LENGTH); for (int i = FILE_IDENTIFIER_LENGTH - 1; i >= 0; i--) { addByte((byte)file_identifier.charAt(i)); } finish(root_table, size_prefix); } /** * Finalize a buffer, pointing to the given `root_table`. * * @param root_table An offset to be added to the buffer. * @param file_identifier A FlatBuffer file identifier to be added to the buffer before * `root_table`. */ public void finish(int root_table, String file_identifier) { finish(root_table, file_identifier, false); } /** * Finalize a buffer, pointing to the given `root_table`, with the size prefixed. * * @param root_table An offset to be added to the buffer. * @param file_identifier A FlatBuffer file identifier to be added to the buffer before * `root_table`. */ public void finishSizePrefixed(int root_table, String file_identifier) { finish(root_table, file_identifier, true); } /** * In order to save space, fields that are set to their default value * don't get serialized into the buffer. Forcing defaults provides a * way to manually disable this optimization. * * @param forceDefaults When set to `true`, always serializes default values. * @return Returns `this`. */ public FlatBufferBuilder forceDefaults(boolean forceDefaults){ this.force_defaults = forceDefaults; return this; } /** * Get the ByteBuffer representing the FlatBuffer. Only call this after you've * called `finish()`. The actual data starts at the ByteBuffer's current position, * not necessarily at `0`. * * @return The {@link ByteBuffer} representing the FlatBuffer */ public ByteBuffer dataBuffer() { finished(); return bb; } /** * The FlatBuffer data doesn't start at offset 0 in the {@link ByteBuffer}, but * now the {@code ByteBuffer}'s position is set to that location upon {@link #finish(int)}. * * @return The {@link ByteBuffer#position() position} the data starts in {@link #dataBuffer()} * @deprecated This method should not be needed anymore, but is left * here for the moment to document this API change. It will be removed in the future. */ @Deprecated private int dataStart() { finished(); return space; } /** * A utility function to copy and return the ByteBuffer data from `start` to * `start` + `length` as a `byte[]`. * * @param start Start copying at this offset. * @param length How many bytes to copy. * @return A range copy of the {@link #dataBuffer() data buffer}. * @throws IndexOutOfBoundsException If the range of bytes is ouf of bound. */ public byte[] sizedByteArray(int start, int length){ finished(); byte[] array = new byte[length]; bb.position(start); bb.get(array); return array; } /** * A utility function to copy and return the ByteBuffer data as a `byte[]`. * * @return A full copy of the {@link #dataBuffer() data buffer}. */ public byte[] sizedByteArray() { return sizedByteArray(space, bb.capacity() - space); } /** * A utility function to return an InputStream to the ByteBuffer data * * @return An InputStream that starts at the beginning of the ByteBuffer data * and can read to the end of it. */ public InputStream sizedInputStream() { finished(); ByteBuffer duplicate = bb.duplicate(); duplicate.position(space); duplicate.limit(bb.capacity()); return new ByteBufferBackedInputStream(duplicate); } /** * A class that allows a user to create an InputStream from a ByteBuffer. */ static class ByteBufferBackedInputStream extends InputStream { ByteBuffer buf; public ByteBufferBackedInputStream(ByteBuffer buf) { this.buf = buf; } public int read() throws IOException { try { return buf.get() & 0xFF; } catch(BufferUnderflowException e) { return -1; } } } } /// @}