diff --git a/java/com/google/flatbuffers/Constants.java b/java/com/google/flatbuffers/Constants.java index ac0593ae6..c3b98c5e8 100644 --- a/java/com/google/flatbuffers/Constants.java +++ b/java/com/google/flatbuffers/Constants.java @@ -23,10 +23,18 @@ package com.google.flatbuffers; */ public class Constants { // Java doesn't seem to have these. + /** The number of bytes in an `byte`. */ + static final int SIZEOF_BYTE = 1; /** The number of bytes in a `short`. */ static final int SIZEOF_SHORT = 2; /** The number of bytes in an `int`. */ static final int SIZEOF_INT = 4; + /** The number of bytes in an `float`. */ + static final int SIZEOF_FLOAT = 4; + /** The number of bytes in an `long`. */ + static final int SIZEOF_LONG = 8; + /** The number of bytes in an `double`. */ + static final int SIZEOF_DOUBLE = 4; /** The number of bytes in a file identifier. */ static final int FILE_IDENTIFIER_LENGTH = 4; } diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java index fecb213f6..c2186fa7b 100644 --- a/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -188,7 +188,7 @@ public class FlatBufferBuilder { * * @param x A `boolean` to put into the buffer. */ - public void putBoolean(boolean x) { bb.put (space -= 1, (byte)(x ? 1 : 0)); } + public void putBoolean(boolean x) { bb.put (space -= Constants.SIZEOF_BYTE, (byte)(x ? 1 : 0)); } /** * Add a `byte` to the buffer, backwards from the current location. Doesn't align nor @@ -196,7 +196,7 @@ public class FlatBufferBuilder { * * @param x A `byte` to put into the buffer. */ - public void putByte (byte x) { bb.put (space -= 1, x); } + public void putByte (byte x) { bb.put (space -= Constants.SIZEOF_BYTE, x); } /** * Add a `short` to the buffer, backwards from the current location. Doesn't align nor @@ -204,7 +204,7 @@ public class FlatBufferBuilder { * * @param x A `short` to put into the buffer. */ - public void putShort (short x) { bb.putShort (space -= 2, x); } + public void putShort (short x) { bb.putShort (space -= Constants.SIZEOF_SHORT, x); } /** * Add an `int` to the buffer, backwards from the current location. Doesn't align nor @@ -212,7 +212,7 @@ public class FlatBufferBuilder { * * @param x An `int` to put into the buffer. */ - public void putInt (int x) { bb.putInt (space -= 4, x); } + public void putInt (int x) { bb.putInt (space -= Constants.SIZEOF_INT, x); } /** * Add a `long` to the buffer, backwards from the current location. Doesn't align nor @@ -220,7 +220,7 @@ public class FlatBufferBuilder { * * @param x A `long` to put into the buffer. */ - public void putLong (long x) { bb.putLong (space -= 8, x); } + public void putLong (long x) { bb.putLong (space -= Constants.SIZEOF_LONG, x); } /** * Add a `float` to the buffer, backwards from the current location. Doesn't align nor @@ -228,7 +228,7 @@ public class FlatBufferBuilder { * * @param x A `float` to put into the buffer. */ - public void putFloat (float x) { bb.putFloat (space -= 4, x); } + public void putFloat (float x) { bb.putFloat (space -= Constants.SIZEOF_FLOAT, x); } /** * Add a `double` to the buffer, backwards from the current location. Doesn't align nor @@ -236,7 +236,7 @@ public class FlatBufferBuilder { * * @param x A `double` to put into the buffer. */ - public void putDouble (double x) { bb.putDouble(space -= 8, x); } + public void putDouble (double x) { bb.putDouble(space -= Constants.SIZEOF_DOUBLE, x); } /// @endcond /** @@ -244,49 +244,49 @@ public class FlatBufferBuilder { * * @param x A `boolean` to put into the buffer. */ - public void addBoolean(boolean x) { prep(1, 0); putBoolean(x); } + public void addBoolean(boolean x) { prep(Constants.SIZEOF_BYTE, 0); putBoolean(x); } /** * Add a `byte` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x A `byte` to put into the buffer. */ - public void addByte (byte x) { prep(1, 0); putByte (x); } + public void addByte (byte x) { prep(Constants.SIZEOF_BYTE, 0); putByte (x); } /** * Add a `short` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x A `short` to put into the buffer. */ - public void addShort (short x) { prep(2, 0); putShort (x); } + public void addShort (short x) { prep(Constants.SIZEOF_SHORT, 0); putShort (x); } /** * Add an `int` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x An `int` to put into the buffer. */ - public void addInt (int x) { prep(4, 0); putInt (x); } + public void addInt (int x) { prep(Constants.SIZEOF_INT, 0); putInt (x); } /** * Add a `long` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x A `long` to put into the buffer. */ - public void addLong (long x) { prep(8, 0); putLong (x); } + public void addLong (long x) { prep(Constants.SIZEOF_LONG, 0); putLong (x); } /** * Add a `float` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x A `float` to put into the buffer. */ - public void addFloat (float x) { prep(4, 0); putFloat (x); } + public void addFloat (float x) { prep(Constants.SIZEOF_FLOAT, 0); putFloat (x); } /** * Add a `double` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x A `double` to put into the buffer. */ - public void addDouble (double x) { prep(8, 0); putDouble (x); } + public void addDouble (double x) { prep(Constants.SIZEOF_DOUBLE, 0); putDouble (x); } /** * Adds on offset, relative to where it will be written.