Added force-defaults to Java bindings

Change-Id: I62d10b639112788be3b0f670280bd50ef9fcf094
This commit is contained in:
Florian Enner 2015-01-28 23:06:11 -05:00 committed by Wouter van Oortmerssen
parent 9c169083ad
commit 72b9501e69
1 changed files with 22 additions and 8 deletions

View File

@ -39,6 +39,7 @@ public class FlatBufferBuilder {
int[] vtables = new int[16]; // List of offsets of all vtables. int[] vtables = new int[16]; // List of offsets of all vtables.
int num_vtables = 0; // Number of entries in `vtables` in use. int num_vtables = 0; // Number of entries in `vtables` in use.
int vector_num_elems = 0; // For the current vector being built. 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. * 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` // 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 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(x != d) { addByte (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(x != d) { addShort (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(x != d) { addInt (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(x != d) { addLong (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(x != d) { addFloat (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(x != d) { addDouble (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(x != d) { addOffset (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. // Structs are stored inline, so nothing additional is being added. `d` is always 0.
public void addStruct(int voffset, int x, int d) { public void addStruct(int voffset, int x, int d) {
@ -442,6 +443,19 @@ public class FlatBufferBuilder {
finish(root_table); 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 // Get the ByteBuffer representing the FlatBuffer. Only call this after you've
// called finish(). The actual data starts at the ByteBuffer's current position, // called finish(). The actual data starts at the ByteBuffer's current position,
// not necessarily at 0. // not necessarily at 0.