diff --git a/java/flatbuffers/FlatBufferBuilder.java b/java/flatbuffers/FlatBufferBuilder.java index ee0401780..a5ec1fb29 100755 --- a/java/flatbuffers/FlatBufferBuilder.java +++ b/java/flatbuffers/FlatBufferBuilder.java @@ -123,10 +123,11 @@ public class FlatBufferBuilder { putInt(off); } - public void startVector(int elem_size, int num_elems) { + 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. } public int endVector() { @@ -137,7 +138,7 @@ public class FlatBufferBuilder { public int createString(String s) { byte[] utf8 = s.getBytes(utf8charset); addByte((byte)0); - startVector(1, utf8.length); + startVector(1, utf8.length, 1); System.arraycopy(utf8, 0, bb.array(), space -= utf8.length, utf8.length); return endVector(); } diff --git a/src/idl_gen_java.cpp b/src/idl_gen_java.cpp index 6e751eb5e..0f114f090 100755 --- a/src/idl_gen_java.cpp +++ b/src/idl_gen_java.cpp @@ -320,8 +320,12 @@ static void GenStruct(StructDef &struct_def, code += " public static void start" + MakeCamel(field.name); code += "Vector(FlatBufferBuilder builder, int numElems) "; code += "{ builder.startVector("; - code += NumToString(InlineSize(field.value.type.VectorType())); - code += ", numElems); }\n"; + auto vector_type = field.value.type.VectorType(); + auto alignment = InlineAlignment(vector_type); + auto elem_size = InlineSize(vector_type); + code += NumToString(elem_size); + code += ", numElems, " + NumToString(alignment); + code += "); }\n"; } } code += " public static int end" + struct_def.name; diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index a8ec2f933..68da9d894 100755 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -33,26 +33,29 @@ public class Monster extends Table { public Monster enemy(Monster obj) { int o = __offset(28); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } public byte testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; } public int testnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; } + public Monster testempty() { return testempty(new Monster()); } + public Monster testempty(Monster obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } - public static void startMonster(FlatBufferBuilder builder) { builder.startObject(14); } + public static void startMonster(FlatBufferBuilder builder) { builder.startObject(15); } public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); } public static void addMana(FlatBufferBuilder builder, short mana) { builder.addShort(1, mana, 150); } public static void addHp(FlatBufferBuilder builder, short hp) { builder.addShort(2, hp, 100); } public static void addName(FlatBufferBuilder builder, int nameOffset) { builder.addOffset(3, nameOffset, 0); } public static void addInventory(FlatBufferBuilder builder, int inventoryOffset) { builder.addOffset(5, inventoryOffset, 0); } - public static void startInventoryVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems); } + public static void startInventoryVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); } public static void addColor(FlatBufferBuilder builder, byte color) { builder.addByte(6, color, 8); } public static void addTestType(FlatBufferBuilder builder, byte testType) { builder.addByte(7, testType, 0); } public static void addTest(FlatBufferBuilder builder, int testOffset) { builder.addOffset(8, testOffset, 0); } public static void addTest4(FlatBufferBuilder builder, int test4Offset) { builder.addOffset(9, test4Offset, 0); } - public static void startTest4Vector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems); } + public static void startTest4Vector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 2); } public static void addTestarrayofstring(FlatBufferBuilder builder, int testarrayofstringOffset) { builder.addOffset(10, testarrayofstringOffset, 0); } - public static void startTestarrayofstringVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems); } + public static void startTestarrayofstringVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); } public static void addTestarrayoftables(FlatBufferBuilder builder, int testarrayoftablesOffset) { builder.addOffset(11, testarrayoftablesOffset, 0); } - public static void startTestarrayoftablesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems); } + public static void startTestarrayoftablesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); } public static void addEnemy(FlatBufferBuilder builder, int enemyOffset) { builder.addOffset(12, enemyOffset, 0); } public static void addTestnestedflatbuffer(FlatBufferBuilder builder, int testnestedflatbufferOffset) { builder.addOffset(13, testnestedflatbufferOffset, 0); } - public static void startTestnestedflatbufferVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems); } + public static void startTestnestedflatbufferVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); } + public static void addTestempty(FlatBufferBuilder builder, int testemptyOffset) { builder.addOffset(14, testemptyOffset, 0); } public static int endMonster(FlatBufferBuilder builder) { return builder.endObject(); } };