mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-24 02:11:47 +00:00
initial changes to support size prefixed buffers in Java
This commit is contained in:
@@ -816,14 +816,55 @@ public class FlatBufferBuilder {
|
|||||||
* Finalize a buffer, pointing to the given `root_table`.
|
* Finalize a buffer, pointing to the given `root_table`.
|
||||||
*
|
*
|
||||||
* @param root_table An offset to be added to the buffer.
|
* @param root_table An offset to be added to the buffer.
|
||||||
|
* @param size_prefix Whether to prefix the size to the buffer.
|
||||||
*/
|
*/
|
||||||
public void finish(int root_table) {
|
protected void finish(int root_table, boolean size_prefix) {
|
||||||
prep(minalign, SIZEOF_INT);
|
prep(minalign, SIZEOF_INT + (size_prefix ? SIZEOF_INT : 0));
|
||||||
addOffset(root_table);
|
addOffset(root_table);
|
||||||
|
if (size_prefix) {
|
||||||
|
addInt(bb.capacity() - space);
|
||||||
|
}
|
||||||
bb.position(space);
|
bb.position(space);
|
||||||
finished = true;
|
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`.
|
* Finalize a buffer, pointing to the given `root_table`.
|
||||||
*
|
*
|
||||||
@@ -832,14 +873,18 @@ public class FlatBufferBuilder {
|
|||||||
* `root_table`.
|
* `root_table`.
|
||||||
*/
|
*/
|
||||||
public void finish(int root_table, String file_identifier) {
|
public void finish(int root_table, String file_identifier) {
|
||||||
prep(minalign, SIZEOF_INT + FILE_IDENTIFIER_LENGTH);
|
finish(root_table, file_identifier, false);
|
||||||
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--) {
|
* Finalize a buffer, pointing to the given `root_table`, with the size prefixed.
|
||||||
addByte((byte)file_identifier.charAt(i));
|
*
|
||||||
}
|
* @param root_table An offset to be added to the buffer.
|
||||||
finish(root_table);
|
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -828,6 +828,30 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr) {
|
|||||||
code += ") + _bb.";
|
code += ") + _bb.";
|
||||||
code += lang_.get_bb_position;
|
code += lang_.get_bb_position;
|
||||||
code += ", _bb)); }\n";
|
code += ", _bb)); }\n";
|
||||||
|
|
||||||
|
// recreate both methods for the prefixed size version
|
||||||
|
std::string ps_method_name = FunctionStart('G') + "etSizePrefixedRootAs" +
|
||||||
|
struct_def.name;
|
||||||
|
std::string ps_method_signature = " public static " + struct_def.name + " " +
|
||||||
|
ps_method_name;
|
||||||
|
|
||||||
|
// create convenience method that doesn't require an existing object
|
||||||
|
code += ps_method_signature + "(ByteBuffer _psbb) ";
|
||||||
|
code += "{ return " + ps_method_name + "(_psbb, new " + struct_def.name+ "()); }\n";
|
||||||
|
|
||||||
|
// TODO this part needs a C# equivalent
|
||||||
|
// use a slice that skips the size, then proceed as normal
|
||||||
|
code += ps_method_signature + "(ByteBuffer _psbb, " + struct_def.name + " obj) { ";
|
||||||
|
code += "ByteBuffer _bb = _psbb.slice(); ";
|
||||||
|
code += "_bb.position(4); ";
|
||||||
|
code += "return " + method_name + "(_bb, obj); }\n";
|
||||||
|
|
||||||
|
// method that returns the size for a size prefixed buffer
|
||||||
|
code += " public static int " + FunctionStart('G') + "etSizePrefix(ByteBuffer _bb) { ";
|
||||||
|
code += lang_.set_bb_byteorder;
|
||||||
|
code += "return _bb." + FunctionStart('G') + "etInt(_bb.";
|
||||||
|
code += lang_.get_bb_position + "); }\n";
|
||||||
|
|
||||||
if (parser_.root_struct_def_ == &struct_def) {
|
if (parser_.root_struct_def_ == &struct_def) {
|
||||||
if (parser_.file_identifier_.length()) {
|
if (parser_.file_identifier_.length()) {
|
||||||
// Check if a buffer has the identifier.
|
// Check if a buffer has the identifier.
|
||||||
@@ -1305,18 +1329,22 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr) {
|
|||||||
}
|
}
|
||||||
code += " return " + GenOffsetConstruct(struct_def, "o") + ";\n }\n";
|
code += " return " + GenOffsetConstruct(struct_def, "o") + ";\n }\n";
|
||||||
if (parser_.root_struct_def_ == &struct_def) {
|
if (parser_.root_struct_def_ == &struct_def) {
|
||||||
code += " public static void ";
|
std::string size_prefix[] = { "", "SizePrefixed" };
|
||||||
code += FunctionStart('F') + "inish" + struct_def.name;
|
for (int i = 0; i < 2; ++i) {
|
||||||
code += "Buffer(FlatBufferBuilder builder, " + GenOffsetType(struct_def);
|
code += " public static void ";
|
||||||
code += " offset) {";
|
code += FunctionStart('F') + "inish" + size_prefix[i] + struct_def.name;
|
||||||
code += " builder." + FunctionStart('F') + "inish(offset";
|
code += "Buffer(FlatBufferBuilder builder, " + GenOffsetType(struct_def);
|
||||||
if (lang_.language == IDLOptions::kCSharp) {
|
code += " offset) {";
|
||||||
code += ".Value";
|
code += " builder." + FunctionStart('F') + "inish" + size_prefix[i] +
|
||||||
}
|
"(offset";
|
||||||
|
if (lang_.language == IDLOptions::kCSharp) {
|
||||||
|
code += ".Value";
|
||||||
|
}
|
||||||
|
|
||||||
if (parser_.file_identifier_.length())
|
if (parser_.file_identifier_.length())
|
||||||
code += ", \"" + parser_.file_identifier_ + "\"";
|
code += ", \"" + parser_.file_identifier_ + "\"";
|
||||||
code += "); }\n";
|
code += "); }\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Only generate key compare function for table,
|
// Only generate key compare function for table,
|
||||||
|
|||||||
Reference in New Issue
Block a user