diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java index 44ef1d9d1..5b1425a2d 100644 --- a/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -39,6 +39,7 @@ public class FlatBufferBuilder { int[] vtables = new int[16]; // List of offsets of all vtables. int num_vtables = 0; // Number of entries in `vtables` in use. int vector_num_elems = 0; // For the current vector being built. + boolean force_defaults = false; // False omits default values from the serialized data /** * Start with a buffer of size {@code initial_size}, then grow as required. @@ -333,14 +334,14 @@ public class FlatBufferBuilder { } // Add a scalar to a table at `o` into its vtable, with value `x` and default `d` - public void addBoolean(int o, boolean x, boolean d) { if(x != d) { addBoolean(x); slot(o); } } - public void addByte (int o, byte x, int d) { if(x != d) { addByte (x); slot(o); } } - public void addShort (int o, short x, int d) { if(x != d) { addShort (x); slot(o); } } - public void addInt (int o, int x, int d) { if(x != d) { addInt (x); slot(o); } } - public void addLong (int o, long x, long d) { if(x != d) { addLong (x); slot(o); } } - public void addFloat (int o, float x, double d) { if(x != d) { addFloat (x); slot(o); } } - public void addDouble (int o, double x, double d) { if(x != d) { addDouble (x); slot(o); } } - public void addOffset (int o, int x, int d) { if(x != d) { addOffset (x); slot(o); } } + public void addBoolean(int o, boolean x, boolean d) { if(force_defaults || x != d) { addBoolean(x); slot(o); } } + public void addByte (int o, byte x, int d) { if(force_defaults || x != d) { addByte (x); slot(o); } } + public void addShort (int o, short x, int d) { if(force_defaults || x != d) { addShort (x); slot(o); } } + public void addInt (int o, int x, int d) { if(force_defaults || x != d) { addInt (x); slot(o); } } + public void addLong (int o, long x, long d) { if(force_defaults || x != d) { addLong (x); slot(o); } } + public void addFloat (int o, float x, double d) { if(force_defaults || x != d) { addFloat (x); slot(o); } } + public void addDouble (int o, double x, double d) { if(force_defaults || x != d) { addDouble (x); slot(o); } } + public void addOffset (int o, int x, int d) { if(force_defaults || x != d) { addOffset (x); slot(o); } } // Structs are stored inline, so nothing additional is being added. `d` is always 0. public void addStruct(int voffset, int x, int d) { @@ -442,6 +443,19 @@ public class FlatBufferBuilder { finish(root_table); } + /** + * 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 true always serializes default values + * @return 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.