mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 20:15:34 +00:00
bulk code format fix (#8707)
This commit is contained in:
@@ -3,12 +3,10 @@ package com.google.flatbuffers;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Implements {@code ReadBuf} using an array of bytes
|
||||
* as a backing storage. Using array of bytes are
|
||||
* Implements {@code ReadBuf} using an array of bytes as a backing storage. Using array of bytes are
|
||||
* usually faster than {@code ByteBuffer}.
|
||||
*
|
||||
* This class is not thread-safe, meaning that
|
||||
* it must operate on a single thread. Operating from
|
||||
* <p>This class is not thread-safe, meaning that it must operate on a single thread. Operating from
|
||||
* multiple thread leads into a undefined behavior
|
||||
*/
|
||||
public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
@@ -51,27 +49,27 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
|
||||
@Override
|
||||
public short getShort(int index) {
|
||||
return (short) ((buffer[index+ 1] << 8) | (buffer[index] & 0xff));
|
||||
return (short) ((buffer[index + 1] << 8) | (buffer[index] & 0xff));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(int index) {
|
||||
return (((buffer[index + 3]) << 24) |
|
||||
((buffer[index + 2] & 0xff) << 16) |
|
||||
((buffer[index + 1] & 0xff) << 8) |
|
||||
((buffer[index] & 0xff)));
|
||||
return (((buffer[index + 3]) << 24)
|
||||
| ((buffer[index + 2] & 0xff) << 16)
|
||||
| ((buffer[index + 1] & 0xff) << 8)
|
||||
| ((buffer[index] & 0xff)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLong(int index) {
|
||||
return ((((long) buffer[index++] & 0xff)) |
|
||||
(((long) buffer[index++] & 0xff) << 8) |
|
||||
(((long) buffer[index++] & 0xff) << 16) |
|
||||
(((long) buffer[index++] & 0xff) << 24) |
|
||||
(((long) buffer[index++] & 0xff) << 32) |
|
||||
(((long) buffer[index++] & 0xff) << 40) |
|
||||
(((long) buffer[index++] & 0xff) << 48) |
|
||||
(((long) buffer[index]) << 56));
|
||||
return ((((long) buffer[index++] & 0xff))
|
||||
| (((long) buffer[index++] & 0xff) << 8)
|
||||
| (((long) buffer[index++] & 0xff) << 16)
|
||||
| (((long) buffer[index++] & 0xff) << 24)
|
||||
| (((long) buffer[index++] & 0xff) << 32)
|
||||
| (((long) buffer[index++] & 0xff) << 40)
|
||||
| (((long) buffer[index++] & 0xff) << 48)
|
||||
| (((long) buffer[index]) << 56));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,17 +92,16 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void putBoolean(boolean value) {
|
||||
setBoolean(writePos, value);
|
||||
writePos++;
|
||||
setBoolean(writePos, value);
|
||||
writePos++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(byte[] value, int start, int length) {
|
||||
set(writePos, value, start, length);
|
||||
writePos+=length;
|
||||
writePos += length;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -116,36 +113,36 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
@Override
|
||||
public void putShort(short value) {
|
||||
setShort(writePos, value);
|
||||
writePos +=2;
|
||||
writePos += 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putInt(int value) {
|
||||
setInt(writePos, value);
|
||||
writePos +=4;
|
||||
writePos += 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putLong(long value) {
|
||||
setLong(writePos, value);
|
||||
writePos +=8;
|
||||
writePos += 8;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putFloat(float value) {
|
||||
setFloat(writePos, value);
|
||||
writePos +=4;
|
||||
writePos += 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putDouble(double value) {
|
||||
setDouble(writePos, value);
|
||||
writePos +=8;
|
||||
writePos += 8;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBoolean(int index, boolean value) {
|
||||
set(index, value ? (byte)1 : (byte)0);
|
||||
set(index, value ? (byte) 1 : (byte) 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -165,7 +162,7 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
requestCapacity(index + 2);
|
||||
|
||||
buffer[index++] = (byte) ((value) & 0xff);
|
||||
buffer[index ] = (byte) ((value >> 8) & 0xff);
|
||||
buffer[index] = (byte) ((value >> 8) & 0xff);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -173,9 +170,9 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
requestCapacity(index + 4);
|
||||
|
||||
buffer[index++] = (byte) ((value) & 0xff);
|
||||
buffer[index++] = (byte) ((value >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((value >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((value >> 16) & 0xff);
|
||||
buffer[index ] = (byte) ((value >> 24) & 0xff);
|
||||
buffer[index] = (byte) ((value >> 24) & 0xff);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -184,14 +181,14 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
|
||||
int i = (int) value;
|
||||
buffer[index++] = (byte) ((i) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 16) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 24) & 0xff);
|
||||
i = (int) (value >> 32);
|
||||
buffer[index++] = (byte) ((i) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 16) & 0xff);
|
||||
buffer[index ] = (byte) ((i >> 24) & 0xff);
|
||||
buffer[index] = (byte) ((i >> 24) & 0xff);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -200,9 +197,9 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
|
||||
int iValue = Float.floatToRawIntBits(value);
|
||||
buffer[index++] = (byte) ((iValue) & 0xff);
|
||||
buffer[index++] = (byte) ((iValue >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((iValue >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((iValue >> 16) & 0xff);
|
||||
buffer[index ] = (byte) ((iValue >> 24) & 0xff);
|
||||
buffer[index] = (byte) ((iValue >> 24) & 0xff);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -212,14 +209,14 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
long lValue = Double.doubleToRawLongBits(value);
|
||||
int i = (int) lValue;
|
||||
buffer[index++] = (byte) ((i) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 16) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 24) & 0xff);
|
||||
i = (int) (lValue >> 32);
|
||||
buffer[index++] = (byte) ((i) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 8) & 0xff);
|
||||
buffer[index++] = (byte) ((i >> 16) & 0xff);
|
||||
buffer[index ] = (byte) ((i >> 24) & 0xff);
|
||||
buffer[index] = (byte) ((i >> 24) & 0xff);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -235,7 +232,8 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
@Override
|
||||
public boolean requestCapacity(int capacity) {
|
||||
if (capacity < 0) {
|
||||
throw new IllegalArgumentException("Capacity may not be negative (likely a previous int overflow)");
|
||||
throw new IllegalArgumentException(
|
||||
"Capacity may not be negative (likely a previous int overflow)");
|
||||
}
|
||||
if (buffer.length >= capacity) {
|
||||
return true;
|
||||
@@ -243,7 +241,7 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
// implemented in the same growing fashion as ArrayList
|
||||
int oldCapacity = buffer.length;
|
||||
int newCapacity = oldCapacity + (oldCapacity >> 1);
|
||||
if (newCapacity < capacity) { // Note: this also catches newCapacity int overflow
|
||||
if (newCapacity < capacity) { // Note: this also catches newCapacity int overflow
|
||||
newCapacity = capacity;
|
||||
}
|
||||
buffer = Arrays.copyOf(buffer, newCapacity);
|
||||
|
||||
@@ -20,16 +20,17 @@ import java.nio.ByteBuffer;
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
|
||||
/**
|
||||
* All vector access objects derive from this class, and add their own accessors.
|
||||
*/
|
||||
/** All vector access objects derive from this class, and add their own accessors. */
|
||||
public class BaseVector {
|
||||
/** Used to hold the vector data position. */
|
||||
private int vector;
|
||||
|
||||
/** Used to hold the vector size. */
|
||||
private int length;
|
||||
|
||||
/** Used to hold the vector element size in table. */
|
||||
private int element_size;
|
||||
|
||||
/** The underlying ByteBuffer to hold the data of the vector. */
|
||||
protected ByteBuffer bb;
|
||||
|
||||
@@ -56,10 +57,10 @@ public class BaseVector {
|
||||
* Re-init the internal state with an external buffer {@code ByteBuffer}, an offset within and
|
||||
* element size.
|
||||
*
|
||||
* This method exists primarily to allow recycling vector instances without risking memory leaks
|
||||
* due to {@code ByteBuffer} references.
|
||||
* <p>This method exists primarily to allow recycling vector instances without risking memory
|
||||
* leaks due to {@code ByteBuffer} references.
|
||||
*/
|
||||
protected void __reset(int _vector, int _element_size, ByteBuffer _bb) {
|
||||
protected void __reset(int _vector, int _element_size, ByteBuffer _bb) {
|
||||
bb = _bb;
|
||||
if (bb != null) {
|
||||
vector = _vector;
|
||||
@@ -75,8 +76,8 @@ public class BaseVector {
|
||||
/**
|
||||
* Resets the internal state with a null {@code ByteBuffer} and a zero position.
|
||||
*
|
||||
* This method exists primarily to allow recycling vector instances without risking memory leaks
|
||||
* due to {@code ByteBuffer} references. The instance will be unusable until it is assigned
|
||||
* <p>This method exists primarily to allow recycling vector instances without risking memory
|
||||
* leaks due to {@code ByteBuffer} references. The instance will be unusable until it is assigned
|
||||
* again to a {@code ByteBuffer}.
|
||||
*/
|
||||
public void reset() {
|
||||
|
||||
@@ -17,13 +17,10 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Helper type for accessing vector of booleans.
|
||||
*/
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/** Helper type for accessing vector of booleans. */
|
||||
public final class BooleanVector extends BaseVector {
|
||||
/**
|
||||
* Assigns vector access object to vector data.
|
||||
@@ -31,10 +28,11 @@ public final class BooleanVector extends BaseVector {
|
||||
* @param _vector Start data of a vector.
|
||||
* @param _bb Table's ByteBuffer.
|
||||
* @return Returns current vector access object assigned to vector data whose offset is stored at
|
||||
* `vector`.
|
||||
* `vector`.
|
||||
*/
|
||||
public BooleanVector __assign(int _vector, ByteBuffer _bb) {
|
||||
__reset(_vector, Constants.SIZEOF_BYTE, _bb); return this;
|
||||
__reset(_vector, Constants.SIZEOF_BYTE, _bb);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,7 +41,7 @@ public final class BooleanVector extends BaseVector {
|
||||
* @param j The index from which the boolean will be read.
|
||||
* @return the boolean value at the given index.
|
||||
*/
|
||||
public boolean get(int j) {
|
||||
public boolean get(int j) {
|
||||
return 0 != bb.get(__element(j));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ public class ByteBufferReadWriteBuf implements ReadWriteBuf {
|
||||
|
||||
@Override
|
||||
public void putBoolean(boolean value) {
|
||||
buffer.put(value ? (byte)1 : (byte)0);
|
||||
buffer.put(value ? (byte) 1 : (byte) 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -104,7 +104,7 @@ public class ByteBufferReadWriteBuf implements ReadWriteBuf {
|
||||
|
||||
@Override
|
||||
public void setBoolean(int index, boolean value) {
|
||||
set(index, value ? (byte)1 : (byte)0);
|
||||
set(index, value ? (byte) 1 : (byte) 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -166,5 +166,4 @@ public class ByteBufferReadWriteBuf implements ReadWriteBuf {
|
||||
public boolean requestCapacity(int capacity) {
|
||||
return capacity <= buffer.limit();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,35 +24,31 @@ import java.nio.ByteBuffer;
|
||||
/// @addtogroup flatbuffers_java_api
|
||||
/// @{
|
||||
|
||||
/**
|
||||
* Class that collects utility functions around `ByteBuffer`.
|
||||
*/
|
||||
/** Class that collects utility functions around `ByteBuffer`. */
|
||||
public class ByteBufferUtil {
|
||||
|
||||
/**
|
||||
* Extract the size prefix from a `ByteBuffer`.
|
||||
*
|
||||
* @param bb a size-prefixed buffer
|
||||
* @return the size prefix
|
||||
*/
|
||||
public static int getSizePrefix(ByteBuffer bb) {
|
||||
return bb.getInt(bb.position());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a duplicate of a size-prefixed `ByteBuffer` that has its position
|
||||
* advanced just past the size prefix.
|
||||
*
|
||||
* @param bb a size-prefixed buffer
|
||||
* @return a new buffer on the same underlying data that has skipped the
|
||||
* size prefix
|
||||
*/
|
||||
public static ByteBuffer removeSizePrefix(ByteBuffer bb) {
|
||||
ByteBuffer s = bb.duplicate();
|
||||
s.position(s.position() + SIZE_PREFIX_LENGTH);
|
||||
return s;
|
||||
}
|
||||
/**
|
||||
* Extract the size prefix from a `ByteBuffer`.
|
||||
*
|
||||
* @param bb a size-prefixed buffer
|
||||
* @return the size prefix
|
||||
*/
|
||||
public static int getSizePrefix(ByteBuffer bb) {
|
||||
return bb.getInt(bb.position());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a duplicate of a size-prefixed `ByteBuffer` that has its position advanced just past the
|
||||
* size prefix.
|
||||
*
|
||||
* @param bb a size-prefixed buffer
|
||||
* @return a new buffer on the same underlying data that has skipped the size prefix
|
||||
*/
|
||||
public static ByteBuffer removeSizePrefix(ByteBuffer bb) {
|
||||
ByteBuffer s = bb.duplicate();
|
||||
s.position(s.position() + SIZE_PREFIX_LENGTH);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
/// @}
|
||||
|
||||
@@ -17,13 +17,10 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Helper type for accessing vector of signed or unsigned 8-bit values.
|
||||
*/
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/** Helper type for accessing vector of signed or unsigned 8-bit values. */
|
||||
public final class ByteVector extends BaseVector {
|
||||
/**
|
||||
* Assigns vector access object to vector data.
|
||||
@@ -31,10 +28,11 @@ public final class ByteVector extends BaseVector {
|
||||
* @param vector Start data of a vector.
|
||||
* @param bb Table's ByteBuffer.
|
||||
* @return Returns current vector access object assigned to vector data whose offset is stored at
|
||||
* `vector`.
|
||||
* `vector`.
|
||||
*/
|
||||
public ByteVector __assign(int vector, ByteBuffer bb) {
|
||||
__reset(vector, Constants.SIZEOF_BYTE, bb); return this;
|
||||
public ByteVector __assign(int vector, ByteBuffer bb) {
|
||||
__reset(vector, Constants.SIZEOF_BYTE, bb);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,12 +42,12 @@ public final class ByteVector extends BaseVector {
|
||||
* @return the 8-bit value at the given index.
|
||||
*/
|
||||
public byte get(int j) {
|
||||
return bb.get(__element(j));
|
||||
return bb.get(__element(j));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the byte at the given index, zero-extends it to type int, and returns the result,
|
||||
* which is therefore in the range 0 through 255.
|
||||
* Reads the byte at the given index, zero-extends it to type int, and returns the result, which
|
||||
* is therefore in the range 0 through 255.
|
||||
*
|
||||
* @param j The index from which the byte will be read.
|
||||
* @return the unsigned 8-bit at the given index.
|
||||
|
||||
@@ -18,35 +18,40 @@ package com.google.flatbuffers;
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
|
||||
/**
|
||||
* Class that holds shared constants
|
||||
*/
|
||||
/** Class that holds shared constants */
|
||||
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 = 8;
|
||||
/** The number of bytes in a file identifier. */
|
||||
static final int FILE_IDENTIFIER_LENGTH = 4;
|
||||
/** The number of bytes in a size prefix. */
|
||||
public static final int SIZE_PREFIX_LENGTH = 4;
|
||||
/** A version identifier to force a compile error if someone
|
||||
accidentally tries to build generated code with a runtime of
|
||||
two mismatched version. Versions need to always match, as
|
||||
the runtime and generated code are modified in sync.
|
||||
Changes to the Java implementation need to be sure to change
|
||||
the version here and in the code generator on every possible
|
||||
incompatible change */
|
||||
public static void FLATBUFFERS_25_2_10() {}
|
||||
// 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 = 8;
|
||||
|
||||
/** The number of bytes in a file identifier. */
|
||||
static final int FILE_IDENTIFIER_LENGTH = 4;
|
||||
|
||||
/** The number of bytes in a size prefix. */
|
||||
public static final int SIZE_PREFIX_LENGTH = 4;
|
||||
|
||||
/**
|
||||
* A version identifier to force a compile error if someone accidentally tries to build generated
|
||||
* code with a runtime of two mismatched version. Versions need to always match, as the runtime
|
||||
* and generated code are modified in sync. Changes to the Java implementation need to be sure to
|
||||
* change the version here and in the code generator on every possible incompatible change
|
||||
*/
|
||||
public static void FLATBUFFERS_25_2_10() {}
|
||||
}
|
||||
|
||||
/// @endcond
|
||||
|
||||
@@ -17,13 +17,10 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Helper type for accessing vector of double values.
|
||||
*/
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/** Helper type for accessing vector of double values. */
|
||||
public final class DoubleVector extends BaseVector {
|
||||
/**
|
||||
* Assigns vector access object to vector data.
|
||||
@@ -31,10 +28,11 @@ public final class DoubleVector extends BaseVector {
|
||||
* @param _vector Start data of a vector.
|
||||
* @param _bb Table's ByteBuffer.
|
||||
* @return Returns current vector access object assigned to vector data whose offset is stored at
|
||||
* `vector`.
|
||||
* `vector`.
|
||||
*/
|
||||
public DoubleVector __assign(int _vector, ByteBuffer _bb) {
|
||||
__reset(_vector, Constants.SIZEOF_DOUBLE, _bb); return this;
|
||||
__reset(_vector, Constants.SIZEOF_DOUBLE, _bb);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -17,13 +17,10 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Helper type for accessing vector of float values.
|
||||
*/
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/** Helper type for accessing vector of float values. */
|
||||
public final class FloatVector extends BaseVector {
|
||||
/**
|
||||
* Assigns vector access object to vector data.
|
||||
@@ -31,10 +28,11 @@ public final class FloatVector extends BaseVector {
|
||||
* @param _vector Start data of a vector.
|
||||
* @param _bb Table's ByteBuffer.
|
||||
* @return Returns current vector access object assigned to vector data whose offset is stored at
|
||||
* `vector`.
|
||||
* `vector`.
|
||||
*/
|
||||
public FloatVector __assign(int _vector, ByteBuffer _bb) {
|
||||
__reset(_vector, Constants.SIZEOF_FLOAT, _bb); return this;
|
||||
__reset(_vector, Constants.SIZEOF_FLOAT, _bb);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,7 +41,7 @@ public final class FloatVector extends BaseVector {
|
||||
* @param j The index from which the float value will be read.
|
||||
* @return the float value at the given index.
|
||||
*/
|
||||
public float get(int j) {
|
||||
public float get(int j) {
|
||||
return bb.getFloat(__element(j));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,13 +17,10 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Helper type for accessing vector of signed or unsigned 32-bit values.
|
||||
*/
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/** Helper type for accessing vector of signed or unsigned 32-bit values. */
|
||||
public final class IntVector extends BaseVector {
|
||||
/**
|
||||
* Assigns vector access object to vector data.
|
||||
@@ -31,10 +28,11 @@ public final class IntVector extends BaseVector {
|
||||
* @param _vector Start data of a vector.
|
||||
* @param _bb Table's ByteBuffer.
|
||||
* @return Returns current vector access object assigned to vector data whose offset is stored at
|
||||
* `vector`.
|
||||
* `vector`.
|
||||
*/
|
||||
public IntVector __assign(int _vector, ByteBuffer _bb) {
|
||||
__reset(_vector, Constants.SIZEOF_INT, _bb); return this;
|
||||
__reset(_vector, Constants.SIZEOF_INT, _bb);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,13 +17,10 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Helper type for accessing vector of long values.
|
||||
*/
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/** Helper type for accessing vector of long values. */
|
||||
public final class LongVector extends BaseVector {
|
||||
/**
|
||||
* Assigns vector access object to vector data.
|
||||
@@ -31,10 +28,11 @@ public final class LongVector extends BaseVector {
|
||||
* @param _vector Start data of a vector.
|
||||
* @param _bb Table's ByteBuffer.
|
||||
* @return Returns current vector access object assigned to vector data whose offset is stored at
|
||||
* `vector`.
|
||||
* `vector`.
|
||||
*/
|
||||
public LongVector __assign(int _vector, ByteBuffer _bb) {
|
||||
__reset(_vector, Constants.SIZEOF_LONG, _bb); return this;
|
||||
__reset(_vector, Constants.SIZEOF_LONG, _bb);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
/**
|
||||
* Represent a chunk of data, where FlexBuffers will read from.
|
||||
*/
|
||||
/** Represent a chunk of data, where FlexBuffers will read from. */
|
||||
public interface ReadBuf {
|
||||
|
||||
/**
|
||||
* Read boolean from data. Booleans as stored as single byte
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return boolean element
|
||||
*/
|
||||
@@ -14,6 +13,7 @@ public interface ReadBuf {
|
||||
|
||||
/**
|
||||
* Read a byte from data.
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a byte
|
||||
*/
|
||||
@@ -21,6 +21,7 @@ public interface ReadBuf {
|
||||
|
||||
/**
|
||||
* Read a short from data.
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a short
|
||||
*/
|
||||
@@ -28,6 +29,7 @@ public interface ReadBuf {
|
||||
|
||||
/**
|
||||
* Read a 32-bit int from data.
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return an int
|
||||
*/
|
||||
@@ -35,6 +37,7 @@ public interface ReadBuf {
|
||||
|
||||
/**
|
||||
* Read a 64-bit long from data.
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a long
|
||||
*/
|
||||
@@ -42,6 +45,7 @@ public interface ReadBuf {
|
||||
|
||||
/**
|
||||
* Read a 32-bit float from data.
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a float
|
||||
*/
|
||||
@@ -49,6 +53,7 @@ public interface ReadBuf {
|
||||
|
||||
/**
|
||||
* Read a 64-bit float from data.
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a double
|
||||
*/
|
||||
@@ -56,6 +61,7 @@ public interface ReadBuf {
|
||||
|
||||
/**
|
||||
* Read an UTF-8 string from data.
|
||||
*
|
||||
* @param start initial element of the string
|
||||
* @param size size of the string in bytes.
|
||||
* @return a {@code String}
|
||||
@@ -63,19 +69,19 @@ public interface ReadBuf {
|
||||
String getString(int start, int size);
|
||||
|
||||
/**
|
||||
* Expose ReadBuf as an array of bytes.
|
||||
* This method is meant to be as efficient as possible, so for a array-backed ReadBuf, it should
|
||||
* return its own internal data. In case access to internal data is not possible,
|
||||
* a copy of the data into an array of bytes might occur.
|
||||
* Expose ReadBuf as an array of bytes. This method is meant to be as efficient as possible, so
|
||||
* for a array-backed ReadBuf, it should return its own internal data. In case access to internal
|
||||
* data is not possible, a copy of the data into an array of bytes might occur.
|
||||
*
|
||||
* @return ReadBuf as an array of bytes
|
||||
*/
|
||||
byte[] data();
|
||||
|
||||
/**
|
||||
* Defines the size of the message in the buffer. It also determines last position that buffer
|
||||
* can be read. Last byte to be accessed is in position {@code limit() -1}.
|
||||
* Defines the size of the message in the buffer. It also determines last position that buffer can
|
||||
* be read. Last byte to be accessed is in position {@code limit() -1}.
|
||||
*
|
||||
* @return indicate last position
|
||||
*/
|
||||
int limit();
|
||||
|
||||
}
|
||||
|
||||
@@ -6,137 +6,144 @@ package com.google.flatbuffers;
|
||||
*/
|
||||
public interface ReadWriteBuf extends ReadBuf {
|
||||
|
||||
/**
|
||||
* Clears (resets) the buffer so that it can be reused. Write position will be set to the
|
||||
* start.
|
||||
*/
|
||||
void clear();
|
||||
/**
|
||||
* Clears (resets) the buffer so that it can be reused. Write position will be set to the start.
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* Put a boolean into the buffer at {@code writePosition()} . Booleans as stored as single
|
||||
* byte. Write position will be incremented.
|
||||
* @return boolean element
|
||||
*/
|
||||
void putBoolean(boolean value);
|
||||
/**
|
||||
* Put a boolean into the buffer at {@code writePosition()} . Booleans as stored as single byte.
|
||||
* Write position will be incremented.
|
||||
*
|
||||
* @return boolean element
|
||||
*/
|
||||
void putBoolean(boolean value);
|
||||
|
||||
/**
|
||||
* Put an array of bytes into the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
* @param value the data to be copied
|
||||
* @param start initial position on value to be copied
|
||||
* @param length amount of bytes to be copied
|
||||
*/
|
||||
void put (byte[] value, int start, int length);
|
||||
/**
|
||||
* Put an array of bytes into the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*
|
||||
* @param value the data to be copied
|
||||
* @param start initial position on value to be copied
|
||||
* @param length amount of bytes to be copied
|
||||
*/
|
||||
void put(byte[] value, int start, int length);
|
||||
|
||||
/**
|
||||
* Write a byte into the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*/
|
||||
void put(byte value);
|
||||
/**
|
||||
* Write a byte into the buffer at {@code writePosition()}. Write position will be incremented.
|
||||
*/
|
||||
void put(byte value);
|
||||
|
||||
/**
|
||||
* Write a 16-bit into in the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*/
|
||||
void putShort(short value);
|
||||
/**
|
||||
* Write a 16-bit into in the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*/
|
||||
void putShort(short value);
|
||||
|
||||
/**
|
||||
* Write a 32-bit into in the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*/
|
||||
void putInt(int value);
|
||||
/**
|
||||
* Write a 32-bit into in the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*/
|
||||
void putInt(int value);
|
||||
|
||||
/**
|
||||
* Write a 64-bit into in the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*/
|
||||
void putLong(long value);
|
||||
/**
|
||||
* Write a 64-bit into in the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*/
|
||||
void putLong(long value);
|
||||
|
||||
/**
|
||||
* Write a 32-bit float into the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*/
|
||||
void putFloat(float value);
|
||||
/**
|
||||
* Write a 32-bit float into the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*/
|
||||
void putFloat(float value);
|
||||
|
||||
/**
|
||||
* Write a 64-bit float into the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*/
|
||||
void putDouble(double value);
|
||||
/**
|
||||
* Write a 64-bit float into the buffer at {@code writePosition()}. Write position will be
|
||||
* incremented.
|
||||
*/
|
||||
void putDouble(double value);
|
||||
|
||||
/**
|
||||
* Write boolean into a given position on the buffer. Booleans as stored as single byte.
|
||||
* @param index position of the element in buffer
|
||||
*/
|
||||
void setBoolean(int index, boolean value);
|
||||
/**
|
||||
* Write boolean into a given position on the buffer. Booleans as stored as single byte.
|
||||
*
|
||||
* @param index position of the element in buffer
|
||||
*/
|
||||
void setBoolean(int index, boolean value);
|
||||
|
||||
/**
|
||||
* Read a byte from data.
|
||||
* @param index position of the element in the buffer
|
||||
* @return a byte
|
||||
*/
|
||||
void set(int index, byte value);
|
||||
/**
|
||||
* Read a byte from data.
|
||||
*
|
||||
* @param index position of the element in the buffer
|
||||
* @return a byte
|
||||
*/
|
||||
void set(int index, byte value);
|
||||
|
||||
/**
|
||||
* Write an array of bytes into the buffer.
|
||||
* @param index initial position of the buffer to be written
|
||||
* @param value the data to be copied
|
||||
* @param start initial position on value to be copied
|
||||
* @param length amount of bytes to be copied
|
||||
*/
|
||||
void set(int index, byte[] value, int start, int length);
|
||||
/**
|
||||
* Write an array of bytes into the buffer.
|
||||
*
|
||||
* @param index initial position of the buffer to be written
|
||||
* @param value the data to be copied
|
||||
* @param start initial position on value to be copied
|
||||
* @param length amount of bytes to be copied
|
||||
*/
|
||||
void set(int index, byte[] value, int start, int length);
|
||||
|
||||
/**
|
||||
* Read a short from data.
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a short
|
||||
*/
|
||||
void setShort(int index, short value);
|
||||
/**
|
||||
* Read a short from data.
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a short
|
||||
*/
|
||||
void setShort(int index, short value);
|
||||
|
||||
/**
|
||||
* Read a 32-bit int from data.
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return an int
|
||||
*/
|
||||
void setInt(int index, int value);
|
||||
/**
|
||||
* Read a 32-bit int from data.
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return an int
|
||||
*/
|
||||
void setInt(int index, int value);
|
||||
|
||||
/**
|
||||
* Read a 64-bit long from data.
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a long
|
||||
*/
|
||||
void setLong(int index, long value);
|
||||
/**
|
||||
* Read a 64-bit long from data.
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a long
|
||||
*/
|
||||
void setLong(int index, long value);
|
||||
|
||||
/**
|
||||
* Read a 32-bit float from data.
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a float
|
||||
*/
|
||||
void setFloat(int index, float value);
|
||||
/**
|
||||
* Read a 32-bit float from data.
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a float
|
||||
*/
|
||||
void setFloat(int index, float value);
|
||||
|
||||
/**
|
||||
* Read a 64-bit float from data.
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a double
|
||||
*/
|
||||
void setDouble(int index, double value);
|
||||
/**
|
||||
* Read a 64-bit float from data.
|
||||
*
|
||||
* @param index position of the element in ReadBuf
|
||||
* @return a double
|
||||
*/
|
||||
void setDouble(int index, double value);
|
||||
|
||||
int writePosition();
|
||||
|
||||
int writePosition();
|
||||
/**
|
||||
* Defines the size of the message in the buffer. It also determines last position that buffer
|
||||
* can be read or write. Last byte to be accessed is in position {@code limit() -1}.
|
||||
* @return indicate last position
|
||||
*/
|
||||
int limit();
|
||||
/**
|
||||
* Defines the size of the message in the buffer. It also determines last position that buffer can
|
||||
* be read or write. Last byte to be accessed is in position {@code limit() -1}.
|
||||
*
|
||||
* @return indicate last position
|
||||
*/
|
||||
int limit();
|
||||
|
||||
/**
|
||||
* Request capacity of the buffer. In case buffer is already larger
|
||||
* than the requested, this method will just return true. Otherwise
|
||||
* It might try to resize the buffer.
|
||||
*
|
||||
* @return true if buffer is able to offer
|
||||
* the requested capacity
|
||||
*/
|
||||
boolean requestCapacity(int capacity);
|
||||
/**
|
||||
* Request capacity of the buffer. In case buffer is already larger than the requested, this
|
||||
* method will just return true. Otherwise It might try to resize the buffer.
|
||||
*
|
||||
* @return true if buffer is able to offer the requested capacity
|
||||
*/
|
||||
boolean requestCapacity(int capacity);
|
||||
}
|
||||
|
||||
@@ -17,13 +17,10 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Helper type for accessing vector of signed or unsigned 16-bit values.
|
||||
*/
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/** Helper type for accessing vector of signed or unsigned 16-bit values. */
|
||||
public final class ShortVector extends BaseVector {
|
||||
/**
|
||||
* Assigns vector access object to vector data.
|
||||
@@ -31,10 +28,11 @@ public final class ShortVector extends BaseVector {
|
||||
* @param _vector Start data of a vector.
|
||||
* @param _bb Table's ByteBuffer.
|
||||
* @return Returns current vector access object assigned to vector data whose offset is stored at
|
||||
* `vector`.
|
||||
* `vector`.
|
||||
*/
|
||||
public ShortVector __assign(int _vector, ByteBuffer _bb) {
|
||||
__reset(_vector, Constants.SIZEOF_SHORT, _bb); return this;
|
||||
__reset(_vector, Constants.SIZEOF_SHORT, _bb);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,8 +46,8 @@ public final class ShortVector extends BaseVector {
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the short at the given index, zero-extends it to type int, and returns the result,
|
||||
* which is therefore in the range 0 through 65535.
|
||||
* Reads the short at the given index, zero-extends it to type int, and returns the result, which
|
||||
* is therefore in the range 0 through 65535.
|
||||
*
|
||||
* @param j The index from which the short value will be read.
|
||||
* @return the unsigned 16-bit at the given index.
|
||||
|
||||
@@ -17,13 +17,10 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Helper type for accessing vector of String.
|
||||
*/
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/** Helper type for accessing vector of String. */
|
||||
public final class StringVector extends BaseVector {
|
||||
private Utf8 utf8 = Utf8.getDefault();
|
||||
|
||||
@@ -34,10 +31,11 @@ public final class StringVector extends BaseVector {
|
||||
* @param _element_size Size of a vector element.
|
||||
* @param _bb Table's ByteBuffer.
|
||||
* @return Returns current vector access object assigned to vector data whose offset is stored at
|
||||
* `vector`.
|
||||
* `vector`.
|
||||
*/
|
||||
public StringVector __assign(int _vector, int _element_size, ByteBuffer _bb) {
|
||||
__reset(_vector, _element_size, _bb); return this;
|
||||
__reset(_vector, _element_size, _bb);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,22 +20,21 @@ import java.nio.ByteBuffer;
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
|
||||
/**
|
||||
* All structs in the generated code derive from this class, and add their own accessors.
|
||||
*/
|
||||
/** All structs in the generated code derive from this class, and add their own accessors. */
|
||||
public class Struct {
|
||||
/** Used to hold the position of the `bb` buffer. */
|
||||
protected int bb_pos;
|
||||
|
||||
/** The underlying ByteBuffer to hold the data of the Struct. */
|
||||
protected ByteBuffer bb;
|
||||
|
||||
/**
|
||||
* Re-init the internal state with an external buffer {@code ByteBuffer} and an offset within.
|
||||
*
|
||||
* This method exists primarily to allow recycling Table instances without risking memory leaks
|
||||
* <p>This method exists primarily to allow recycling Table instances without risking memory leaks
|
||||
* due to {@code ByteBuffer} references.
|
||||
*/
|
||||
protected void __reset(int _i, ByteBuffer _bb) {
|
||||
protected void __reset(int _i, ByteBuffer _bb) {
|
||||
bb = _bb;
|
||||
if (bb != null) {
|
||||
bb_pos = _i;
|
||||
@@ -47,8 +46,8 @@ public class Struct {
|
||||
/**
|
||||
* Resets internal state with a null {@code ByteBuffer} and a zero position.
|
||||
*
|
||||
* This method exists primarily to allow recycling Struct instances without risking memory leaks
|
||||
* due to {@code ByteBuffer} references. The instance will be unusable until it is assigned
|
||||
* <p>This method exists primarily to allow recycling Struct instances without risking memory
|
||||
* leaks due to {@code ByteBuffer} references. The instance will be unusable until it is assigned
|
||||
* again to a {@code ByteBuffer}.
|
||||
*
|
||||
* @param struct the instance to reset to initial state
|
||||
|
||||
@@ -17,23 +17,26 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
|
||||
/**
|
||||
* All tables in the generated code derive from this class, and add their own accessors.
|
||||
*/
|
||||
/** All tables in the generated code derive from this class, and add their own accessors. */
|
||||
public class Table {
|
||||
/** Used to hold the position of the `bb` buffer. */
|
||||
protected int bb_pos;
|
||||
|
||||
/** The underlying ByteBuffer to hold the data of the Table. */
|
||||
protected ByteBuffer bb;
|
||||
|
||||
/** Used to hold the vtable position. */
|
||||
private int vtable_start;
|
||||
|
||||
/** Used to hold the vtable size. */
|
||||
private int vtable_size;
|
||||
|
||||
Utf8 utf8 = Utf8.getDefault();
|
||||
|
||||
/**
|
||||
@@ -41,7 +44,9 @@ public class Table {
|
||||
*
|
||||
* @return Returns the Table's ByteBuffer.
|
||||
*/
|
||||
public ByteBuffer getByteBuffer() { return bb; }
|
||||
public ByteBuffer getByteBuffer() {
|
||||
return bb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up a field in the vtable.
|
||||
@@ -82,10 +87,10 @@ public class Table {
|
||||
/**
|
||||
* Create a Java `String` from UTF-8 data stored inside the FlatBuffer.
|
||||
*
|
||||
* This allocates a new string and converts to wide chars upon each access,
|
||||
* which is not very efficient. Instead, each FlatBuffer string also comes with an
|
||||
* accessor based on __vector_as_bytebuffer below, which is much more efficient,
|
||||
* assuming your Java program can handle UTF-8 data directly.
|
||||
* <p>This allocates a new string and converts to wide chars upon each access, which is not very
|
||||
* efficient. Instead, each FlatBuffer string also comes with an accessor based on
|
||||
* __vector_as_bytebuffer below, which is much more efficient, assuming your Java program can
|
||||
* handle UTF-8 data directly.
|
||||
*
|
||||
* @param offset An `int` index into the Table's ByteBuffer.
|
||||
* @return Returns a `String` from the data stored inside the FlatBuffer at `offset`.
|
||||
@@ -97,10 +102,10 @@ public class Table {
|
||||
/**
|
||||
* Create a Java `String` from UTF-8 data stored inside the FlatBuffer.
|
||||
*
|
||||
* This allocates a new string and converts to wide chars upon each access,
|
||||
* which is not very efficient. Instead, each FlatBuffer string also comes with an
|
||||
* accessor based on __vector_as_bytebuffer below, which is much more efficient,
|
||||
* assuming your Java program can handle UTF-8 data directly.
|
||||
* <p>This allocates a new string and converts to wide chars upon each access, which is not very
|
||||
* efficient. Instead, each FlatBuffer string also comes with an accessor based on
|
||||
* __vector_as_bytebuffer below, which is much more efficient, assuming your Java program can
|
||||
* handle UTF-8 data directly.
|
||||
*
|
||||
* @param offset An `int` index into the Table's ByteBuffer.
|
||||
* @param bb Table ByteBuffer used to read a string at given offset.
|
||||
@@ -133,15 +138,15 @@ public class Table {
|
||||
*/
|
||||
protected int __vector(int offset) {
|
||||
offset += bb_pos;
|
||||
return offset + bb.getInt(offset) + SIZEOF_INT; // data starts after the length
|
||||
return offset + bb.getInt(offset) + SIZEOF_INT; // data starts after the length
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a whole vector as a ByteBuffer.
|
||||
*
|
||||
* This is efficient, since it only allocates a new {@link ByteBuffer} object,
|
||||
* but does not actually copy the data, it still refers to the same bytes
|
||||
* as the original ByteBuffer. Also useful with nested FlatBuffers, etc.
|
||||
* <p>This is efficient, since it only allocates a new {@link ByteBuffer} object, but does not
|
||||
* actually copy the data, it still refers to the same bytes as the original ByteBuffer. Also
|
||||
* useful with nested FlatBuffers, etc.
|
||||
*
|
||||
* @param vector_offset The position of the vector in the byte buffer
|
||||
* @param elem_size The size of each element in the array
|
||||
@@ -160,8 +165,8 @@ public class Table {
|
||||
/**
|
||||
* Initialize vector as a ByteBuffer.
|
||||
*
|
||||
* This is more efficient than using duplicate, since it doesn't copy the data
|
||||
* nor allocattes a new {@link ByteBuffer}, creating no garbage to be collected.
|
||||
* <p>This is more efficient than using duplicate, since it doesn't copy the data nor allocattes a
|
||||
* new {@link ByteBuffer}, creating no garbage to be collected.
|
||||
*
|
||||
* @param bb The {@link ByteBuffer} for the array
|
||||
* @param vector_offset The position of the vector in the byte buffer
|
||||
@@ -205,17 +210,16 @@ public class Table {
|
||||
/**
|
||||
* Check if a {@link ByteBuffer} contains a file identifier.
|
||||
*
|
||||
* @param bb A {@code ByteBuffer} to check if it contains the identifier
|
||||
* `ident`.
|
||||
* @param bb A {@code ByteBuffer} to check if it contains the identifier `ident`.
|
||||
* @param ident A `String` identifier of the FlatBuffer file.
|
||||
* @return True if the buffer contains the file identifier
|
||||
*/
|
||||
protected static boolean __has_identifier(ByteBuffer bb, String ident) {
|
||||
if (ident.length() != FILE_IDENTIFIER_LENGTH)
|
||||
throw new AssertionError("FlatBuffers: file identifier must be length " +
|
||||
FILE_IDENTIFIER_LENGTH);
|
||||
throw new AssertionError(
|
||||
"FlatBuffers: file identifier must be length " + FILE_IDENTIFIER_LENGTH);
|
||||
for (int i = 0; i < FILE_IDENTIFIER_LENGTH; i++) {
|
||||
if (ident.charAt(i) != (char)bb.get(bb.position() + SIZEOF_INT + i)) return false;
|
||||
if (ident.charAt(i) != (char) bb.get(bb.position() + SIZEOF_INT + i)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -229,11 +233,13 @@ public class Table {
|
||||
protected void sortTables(int[] offsets, final ByteBuffer bb) {
|
||||
Integer[] off = new Integer[offsets.length];
|
||||
for (int i = 0; i < offsets.length; i++) off[i] = offsets[i];
|
||||
java.util.Arrays.sort(off, new java.util.Comparator<Integer>() {
|
||||
public int compare(Integer o1, Integer o2) {
|
||||
return keysCompare(o1, o2, bb);
|
||||
}
|
||||
});
|
||||
java.util.Arrays.sort(
|
||||
off,
|
||||
new java.util.Comparator<Integer>() {
|
||||
public int compare(Integer o1, Integer o2) {
|
||||
return keysCompare(o1, o2, bb);
|
||||
}
|
||||
});
|
||||
for (int i = 0; i < offsets.length; i++) offsets[i] = off[i];
|
||||
}
|
||||
|
||||
@@ -244,7 +250,9 @@ public class Table {
|
||||
* @param o2 An 'Integer' index of the second key into the bb.
|
||||
* @param bb A {@code ByteBuffer} to get the keys.
|
||||
*/
|
||||
protected int keysCompare(Integer o1, Integer o2, ByteBuffer bb) { return 0; }
|
||||
protected int keysCompare(Integer o1, Integer o2, ByteBuffer bb) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two strings in the buffer.
|
||||
@@ -261,7 +269,7 @@ public class Table {
|
||||
int startPos_1 = offset_1 + SIZEOF_INT;
|
||||
int startPos_2 = offset_2 + SIZEOF_INT;
|
||||
int len = Math.min(len_1, len_2);
|
||||
for(int i = 0; i < len; i++) {
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (bb.get(i + startPos_1) != bb.get(i + startPos_2))
|
||||
return bb.get(i + startPos_1) - bb.get(i + startPos_2);
|
||||
}
|
||||
@@ -282,8 +290,7 @@ public class Table {
|
||||
int startPos_1 = offset_1 + Constants.SIZEOF_INT;
|
||||
int len = Math.min(len_1, len_2);
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (bb.get(i + startPos_1) != key[i])
|
||||
return bb.get(i + startPos_1) - key[i];
|
||||
if (bb.get(i + startPos_1) != key[i]) return bb.get(i + startPos_1) - key[i];
|
||||
}
|
||||
return len_1 - len_2;
|
||||
}
|
||||
@@ -291,10 +298,10 @@ public class Table {
|
||||
/**
|
||||
* Re-init the internal state with an external buffer {@code ByteBuffer} and an offset within.
|
||||
*
|
||||
* This method exists primarily to allow recycling Table instances without risking memory leaks
|
||||
* <p>This method exists primarily to allow recycling Table instances without risking memory leaks
|
||||
* due to {@code ByteBuffer} references.
|
||||
*/
|
||||
protected void __reset(int _i, ByteBuffer _bb) {
|
||||
protected void __reset(int _i, ByteBuffer _bb) {
|
||||
bb = _bb;
|
||||
if (bb != null) {
|
||||
bb_pos = _i;
|
||||
@@ -310,9 +317,9 @@ public class Table {
|
||||
/**
|
||||
* Resets the internal state with a null {@code ByteBuffer} and a zero position.
|
||||
*
|
||||
* This method exists primarily to allow recycling Table instances without risking memory leaks
|
||||
* due to {@code ByteBuffer} references. The instance will be unusable until it is assigned
|
||||
* again to a {@code ByteBuffer}.
|
||||
* <p>This method exists primarily to allow recycling Table instances without risking memory leaks
|
||||
* due to {@code ByteBuffer} references. The instance will be unusable until it is assigned again
|
||||
* to a {@code ByteBuffer}.
|
||||
*/
|
||||
public void __reset() {
|
||||
__reset(0, null);
|
||||
|
||||
@@ -17,13 +17,10 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Helper type for accessing vector of unions.
|
||||
*/
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/** Helper type for accessing vector of unions. */
|
||||
public final class UnionVector extends BaseVector {
|
||||
/**
|
||||
* Assigns vector access object to vector data.
|
||||
@@ -32,13 +29,13 @@ public final class UnionVector extends BaseVector {
|
||||
* @param _element_size Size of a vector element.
|
||||
* @param _bb Table's ByteBuffer.
|
||||
* @return Returns current vector access object assigned to vector data whose offset is stored at
|
||||
* `vector`.
|
||||
* `vector`.
|
||||
*/
|
||||
public UnionVector __assign(int _vector, int _element_size, ByteBuffer _bb) {
|
||||
__reset(_vector, _element_size, _bb); return this;
|
||||
__reset(_vector, _element_size, _bb);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initialize any Table-derived type to point to the union at the given `index`.
|
||||
*
|
||||
|
||||
@@ -16,22 +16,22 @@
|
||||
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import static java.lang.Character.MAX_SURROGATE;
|
||||
import static java.lang.Character.MIN_SURROGATE;
|
||||
import static java.lang.Character.MIN_HIGH_SURROGATE;
|
||||
import static java.lang.Character.MIN_LOW_SURROGATE;
|
||||
import static java.lang.Character.MIN_SUPPLEMENTARY_CODE_POINT;
|
||||
import static java.lang.Character.MIN_SURROGATE;
|
||||
import static java.lang.Character.isSurrogatePair;
|
||||
import static java.lang.Character.toCodePoint;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public abstract class Utf8 {
|
||||
|
||||
/**
|
||||
* Returns the number of bytes in the UTF-8-encoded form of {@code sequence}. For a string,
|
||||
* this method is equivalent to {@code string.getBytes(UTF_8).length}, but is more efficient in
|
||||
* both time and space.
|
||||
* Returns the number of bytes in the UTF-8-encoded form of {@code sequence}. For a string, this
|
||||
* method is equivalent to {@code string.getBytes(UTF_8).length}, but is more efficient in both
|
||||
* time and space.
|
||||
*
|
||||
* @throws IllegalArgumentException if {@code sequence} contains ill-formed UTF-16 (unpaired
|
||||
* surrogates)
|
||||
@@ -60,6 +60,7 @@ public abstract class Utf8 {
|
||||
|
||||
/**
|
||||
* Get the default UTF-8 processor.
|
||||
*
|
||||
* @return the default processor
|
||||
*/
|
||||
public static Utf8 getDefault() {
|
||||
@@ -71,6 +72,7 @@ public abstract class Utf8 {
|
||||
|
||||
/**
|
||||
* Set the default instance of the UTF-8 processor.
|
||||
*
|
||||
* @param instance the new instance to use
|
||||
*/
|
||||
public static void setDefault(Utf8 instance) {
|
||||
@@ -79,6 +81,7 @@ public abstract class Utf8 {
|
||||
|
||||
/**
|
||||
* Encode a Java's CharSequence UTF8 codepoint into a byte array.
|
||||
*
|
||||
* @param in CharSequence to be encoded
|
||||
* @param start start position of the first char in the codepoint
|
||||
* @param out byte array of 4 bytes to be filled
|
||||
@@ -94,11 +97,11 @@ public abstract class Utf8 {
|
||||
}
|
||||
|
||||
char c = in.charAt(start);
|
||||
if (c < 0x80) {
|
||||
// One byte (0xxx xxxx)
|
||||
out[0] = (byte) c;
|
||||
return 1;
|
||||
} else if (c < 0x800) {
|
||||
if (c < 0x80) {
|
||||
// One byte (0xxx xxxx)
|
||||
out[0] = (byte) c;
|
||||
return 1;
|
||||
} else if (c < 0x800) {
|
||||
// Two bytes (110x xxxx 10xx xxxx)
|
||||
out[0] = (byte) (0xC0 | (c >>> 6));
|
||||
out[1] = (byte) (0x80 | (0x3F & c));
|
||||
@@ -107,7 +110,7 @@ public abstract class Utf8 {
|
||||
// Three bytes (1110 xxxx 10xx xxxx 10xx xxxx)
|
||||
// Maximum single-char code point is 0xFFFF, 16 bits.
|
||||
out[0] = (byte) (0xE0 | (c >>> 12));
|
||||
out[1] =(byte) (0x80 | (0x3F & (c >>> 6)));
|
||||
out[1] = (byte) (0x80 | (0x3F & (c >>> 6)));
|
||||
out[2] = (byte) (0x80 | (0x3F & c));
|
||||
return 3;
|
||||
} else {
|
||||
@@ -115,7 +118,7 @@ public abstract class Utf8 {
|
||||
// Minimum code point represented by a surrogate pair is 0x10000, 17 bits, four UTF-8
|
||||
// bytes
|
||||
final char low;
|
||||
if (start + 1 == inLength || !isSurrogatePair(c, (low = in.charAt(start+1)))) {
|
||||
if (start + 1 == inLength || !isSurrogatePair(c, (low = in.charAt(start + 1)))) {
|
||||
throw new UnpairedSurrogateException(start, inLength);
|
||||
}
|
||||
int codePoint = toCodePoint(c, low);
|
||||
@@ -134,23 +137,17 @@ public abstract class Utf8 {
|
||||
*/
|
||||
static class DecodeUtil {
|
||||
|
||||
/**
|
||||
* Returns whether this is a single-byte codepoint (i.e., ASCII) with the form '0XXXXXXX'.
|
||||
*/
|
||||
/** Returns whether this is a single-byte codepoint (i.e., ASCII) with the form '0XXXXXXX'. */
|
||||
static boolean isOneByte(byte b) {
|
||||
return b >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this is a two-byte codepoint with the form '10XXXXXX'.
|
||||
*/
|
||||
/** Returns whether this is a two-byte codepoint with the form '10XXXXXX'. */
|
||||
static boolean isTwoBytes(byte b) {
|
||||
return b < (byte) 0xE0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this is a three-byte codepoint with the form '110XXXXX'.
|
||||
*/
|
||||
/** Returns whether this is a three-byte codepoint with the form '110XXXXX'. */
|
||||
static boolean isThreeBytes(byte b) {
|
||||
return b < (byte) 0xF0;
|
||||
}
|
||||
@@ -159,8 +156,7 @@ public abstract class Utf8 {
|
||||
resultArr[resultPos] = (char) byte1;
|
||||
}
|
||||
|
||||
static void handleTwoBytes(
|
||||
byte byte1, byte byte2, char[] resultArr, int resultPos)
|
||||
static void handleTwoBytes(byte byte1, byte byte2, char[] resultArr, int resultPos)
|
||||
throws IllegalArgumentException {
|
||||
// Simultaneously checks for illegal trailing-byte in leading position (<= '11000000') and
|
||||
// overlong 2-byte, '11000001'.
|
||||
@@ -177,58 +173,56 @@ public abstract class Utf8 {
|
||||
byte byte1, byte byte2, byte byte3, char[] resultArr, int resultPos)
|
||||
throws IllegalArgumentException {
|
||||
if (isNotTrailingByte(byte2)
|
||||
// overlong? 5 most significant bits must not all be zero
|
||||
|| (byte1 == (byte) 0xE0 && byte2 < (byte) 0xA0)
|
||||
// check for illegal surrogate codepoints
|
||||
|| (byte1 == (byte) 0xED && byte2 >= (byte) 0xA0)
|
||||
|| isNotTrailingByte(byte3)) {
|
||||
// overlong? 5 most significant bits must not all be zero
|
||||
|| (byte1 == (byte) 0xE0 && byte2 < (byte) 0xA0)
|
||||
// check for illegal surrogate codepoints
|
||||
|| (byte1 == (byte) 0xED && byte2 >= (byte) 0xA0)
|
||||
|| isNotTrailingByte(byte3)) {
|
||||
throw new IllegalArgumentException("Invalid UTF-8");
|
||||
}
|
||||
resultArr[resultPos] = (char)
|
||||
(((byte1 & 0x0F) << 12) | (trailingByteValue(byte2) << 6) | trailingByteValue(byte3));
|
||||
resultArr[resultPos] =
|
||||
(char)
|
||||
(((byte1 & 0x0F) << 12) | (trailingByteValue(byte2) << 6) | trailingByteValue(byte3));
|
||||
}
|
||||
|
||||
static void handleFourBytes(
|
||||
byte byte1, byte byte2, byte byte3, byte byte4, char[] resultArr, int resultPos)
|
||||
throws IllegalArgumentException{
|
||||
throws IllegalArgumentException {
|
||||
if (isNotTrailingByte(byte2)
|
||||
// Check that 1 <= plane <= 16. Tricky optimized form of:
|
||||
// valid 4-byte leading byte?
|
||||
// if (byte1 > (byte) 0xF4 ||
|
||||
// overlong? 4 most significant bits must not all be zero
|
||||
// byte1 == (byte) 0xF0 && byte2 < (byte) 0x90 ||
|
||||
// codepoint larger than the highest code point (U+10FFFF)?
|
||||
// byte1 == (byte) 0xF4 && byte2 > (byte) 0x8F)
|
||||
|| (((byte1 << 28) + (byte2 - (byte) 0x90)) >> 30) != 0
|
||||
|| isNotTrailingByte(byte3)
|
||||
|| isNotTrailingByte(byte4)) {
|
||||
// Check that 1 <= plane <= 16. Tricky optimized form of:
|
||||
// valid 4-byte leading byte?
|
||||
// if (byte1 > (byte) 0xF4 ||
|
||||
// overlong? 4 most significant bits must not all be zero
|
||||
// byte1 == (byte) 0xF0 && byte2 < (byte) 0x90 ||
|
||||
// codepoint larger than the highest code point (U+10FFFF)?
|
||||
// byte1 == (byte) 0xF4 && byte2 > (byte) 0x8F)
|
||||
|| (((byte1 << 28) + (byte2 - (byte) 0x90)) >> 30) != 0
|
||||
|| isNotTrailingByte(byte3)
|
||||
|| isNotTrailingByte(byte4)) {
|
||||
throw new IllegalArgumentException("Invalid UTF-8");
|
||||
}
|
||||
int codepoint = ((byte1 & 0x07) << 18)
|
||||
| (trailingByteValue(byte2) << 12)
|
||||
| (trailingByteValue(byte3) << 6)
|
||||
| trailingByteValue(byte4);
|
||||
int codepoint =
|
||||
((byte1 & 0x07) << 18)
|
||||
| (trailingByteValue(byte2) << 12)
|
||||
| (trailingByteValue(byte3) << 6)
|
||||
| trailingByteValue(byte4);
|
||||
resultArr[resultPos] = DecodeUtil.highSurrogate(codepoint);
|
||||
resultArr[resultPos + 1] = DecodeUtil.lowSurrogate(codepoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the byte is not a valid continuation of the form '10XXXXXX'.
|
||||
*/
|
||||
/** Returns whether the byte is not a valid continuation of the form '10XXXXXX'. */
|
||||
private static boolean isNotTrailingByte(byte b) {
|
||||
return b > (byte) 0xBF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the actual value of the trailing byte (removes the prefix '10') for composition.
|
||||
*/
|
||||
/** Returns the actual value of the trailing byte (removes the prefix '10') for composition. */
|
||||
private static int trailingByteValue(byte b) {
|
||||
return b & 0x3F;
|
||||
}
|
||||
|
||||
private static char highSurrogate(int codePoint) {
|
||||
return (char) ((MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT >>> 10))
|
||||
+ (codePoint >>> 10));
|
||||
return (char)
|
||||
((MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT >>> 10)) + (codePoint >>> 10));
|
||||
}
|
||||
|
||||
private static char lowSurrogate(int codePoint) {
|
||||
@@ -236,7 +230,8 @@ public abstract class Utf8 {
|
||||
}
|
||||
}
|
||||
|
||||
// These UTF-8 handling methods are copied from Guava's Utf8Unsafe class with a modification to throw
|
||||
// These UTF-8 handling methods are copied from Guava's Utf8Unsafe class with a modification to
|
||||
// throw
|
||||
// a protocol buffer local exception. This exception is then caught in CodedOutputStream so it can
|
||||
// fallback to more lenient behavior.
|
||||
static class UnpairedSurrogateException extends IllegalArgumentException {
|
||||
|
||||
@@ -25,8 +25,8 @@ import java.nio.charset.CoderResult;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* This class implements the Utf8 API using the Java Utf8 encoder. Use
|
||||
* Utf8.setDefault(new Utf8Old()); to use it.
|
||||
* This class implements the Utf8 API using the Java Utf8 encoder. Use Utf8.setDefault(new
|
||||
* Utf8Old()); to use it.
|
||||
*/
|
||||
public class Utf8Old extends Utf8 {
|
||||
|
||||
@@ -64,8 +64,7 @@ public class Utf8Old extends Utf8 {
|
||||
}
|
||||
cache.lastOutput.clear();
|
||||
cache.lastInput = in;
|
||||
CharBuffer wrap = (in instanceof CharBuffer) ?
|
||||
(CharBuffer) in : CharBuffer.wrap(in);
|
||||
CharBuffer wrap = (in instanceof CharBuffer) ? (CharBuffer) in : CharBuffer.wrap(in);
|
||||
CoderResult result = cache.encoder.encode(wrap, cache.lastOutput, true);
|
||||
if (result.isError()) {
|
||||
try {
|
||||
|
||||
@@ -1,41 +1,39 @@
|
||||
package com.google.flatbuffers;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import static java.lang.Character.MAX_SURROGATE;
|
||||
import static java.lang.Character.MIN_SUPPLEMENTARY_CODE_POINT;
|
||||
import static java.lang.Character.MIN_SURROGATE;
|
||||
import static java.lang.Character.isSurrogatePair;
|
||||
import static java.lang.Character.toCodePoint;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* A set of low-level, high-performance static utility methods related
|
||||
* to the UTF-8 character encoding. This class has no dependencies
|
||||
* outside of the core JDK libraries.
|
||||
* A set of low-level, high-performance static utility methods related to the UTF-8 character
|
||||
* encoding. This class has no dependencies outside of the core JDK libraries.
|
||||
*
|
||||
* <p>There are several variants of UTF-8. The one implemented by
|
||||
* this class is the restricted definition of UTF-8 introduced in
|
||||
* Unicode 3.1, which mandates the rejection of "overlong" byte
|
||||
* sequences as well as rejection of 3-byte surrogate codepoint byte
|
||||
* sequences. Note that the UTF-8 decoder included in Oracle's JDK
|
||||
* has been modified to also reject "overlong" byte sequences, but (as
|
||||
* of 2011) still accepts 3-byte surrogate codepoint byte sequences.
|
||||
* <p>There are several variants of UTF-8. The one implemented by this class is the restricted
|
||||
* definition of UTF-8 introduced in Unicode 3.1, which mandates the rejection of "overlong" byte
|
||||
* sequences as well as rejection of 3-byte surrogate codepoint byte sequences. Note that the UTF-8
|
||||
* decoder included in Oracle's JDK has been modified to also reject "overlong" byte sequences, but
|
||||
* (as of 2011) still accepts 3-byte surrogate codepoint byte sequences.
|
||||
*
|
||||
* <p>The byte sequences considered valid by this class are exactly
|
||||
* those that can be roundtrip converted to Strings and back to bytes
|
||||
* using the UTF-8 charset, without loss: <pre> {@code
|
||||
* <p>The byte sequences considered valid by this class are exactly those that can be roundtrip
|
||||
* converted to Strings and back to bytes using the UTF-8 charset, without loss:
|
||||
*
|
||||
* <pre>{@code
|
||||
* Arrays.equals(bytes, new String(bytes, Internal.UTF_8).getBytes(Internal.UTF_8))
|
||||
* }</pre>
|
||||
*
|
||||
* <p>See the Unicode Standard,</br>
|
||||
* Table 3-6. <em>UTF-8 Bit Distribution</em>,</br>
|
||||
* Table 3-7. <em>Well Formed UTF-8 Byte Sequences</em>.
|
||||
* <p>See the Unicode Standard,</br> Table 3-6. <em>UTF-8 Bit Distribution</em>,</br> Table 3-7.
|
||||
* <em>Well Formed UTF-8 Byte Sequences</em>.
|
||||
*/
|
||||
final public class Utf8Safe extends Utf8 {
|
||||
public final class Utf8Safe extends Utf8 {
|
||||
|
||||
/**
|
||||
* Returns the number of bytes in the UTF-8-encoded form of {@code sequence}. For a string,
|
||||
* this method is equivalent to {@code string.getBytes(UTF_8).length}, but is more efficient in
|
||||
* both time and space.
|
||||
* Returns the number of bytes in the UTF-8-encoded form of {@code sequence}. For a string, this
|
||||
* method is equivalent to {@code string.getBytes(UTF_8).length}, but is more efficient in both
|
||||
* time and space.
|
||||
*
|
||||
* @throws IllegalArgumentException if {@code sequence} contains ill-formed UTF-16 (unpaired
|
||||
* surrogates)
|
||||
@@ -55,7 +53,7 @@ final public class Utf8Safe extends Utf8 {
|
||||
for (; i < utf16Length; i++) {
|
||||
char c = sequence.charAt(i);
|
||||
if (c < 0x800) {
|
||||
utf8Length += ((0x7f - c) >>> 31); // branch free!
|
||||
utf8Length += ((0x7f - c) >>> 31); // branch free!
|
||||
} else {
|
||||
utf8Length += encodedLengthGeneral(sequence, i);
|
||||
break;
|
||||
@@ -64,8 +62,8 @@ final public class Utf8Safe extends Utf8 {
|
||||
|
||||
if (utf8Length < utf16Length) {
|
||||
// Necessary and sufficient condition for overflow because of maximum 3x expansion
|
||||
throw new IllegalArgumentException("UTF-8 length does not fit in int: "
|
||||
+ (utf8Length + (1L << 32)));
|
||||
throw new IllegalArgumentException(
|
||||
"UTF-8 length does not fit in int: " + (utf8Length + (1L << 32)));
|
||||
}
|
||||
return utf8Length;
|
||||
}
|
||||
@@ -167,13 +165,11 @@ final public class Utf8Safe extends Utf8 {
|
||||
return new String(resultArr, 0, resultPos);
|
||||
}
|
||||
|
||||
public static String decodeUtf8Buffer(ByteBuffer buffer, int offset,
|
||||
int length) {
|
||||
public static String decodeUtf8Buffer(ByteBuffer buffer, int offset, int length) {
|
||||
// Bitwise OR combines the sign bits so any negative value fails the check.
|
||||
if ((offset | length | buffer.limit() - offset - length) < 0) {
|
||||
throw new ArrayIndexOutOfBoundsException(
|
||||
String.format("buffer limit=%d, index=%d, limit=%d", buffer.limit(),
|
||||
offset, length));
|
||||
String.format("buffer limit=%d, index=%d, limit=%d", buffer.limit(), offset, length));
|
||||
}
|
||||
|
||||
final int limit = offset + length;
|
||||
@@ -212,8 +208,7 @@ final public class Utf8Safe extends Utf8 {
|
||||
if (offset >= limit) {
|
||||
throw new IllegalArgumentException("Invalid UTF-8");
|
||||
}
|
||||
DecodeUtil.handleTwoBytes(
|
||||
byte1, /* byte2 */ buffer.get(offset++), resultArr, resultPos++);
|
||||
DecodeUtil.handleTwoBytes(byte1, /* byte2 */ buffer.get(offset++), resultArr, resultPos++);
|
||||
} else if (DecodeUtil.isThreeBytes(byte1)) {
|
||||
if (offset >= limit - 1) {
|
||||
throw new IllegalArgumentException("Invalid UTF-8");
|
||||
@@ -263,7 +258,6 @@ final public class Utf8Safe extends Utf8 {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void encodeUtf8Buffer(CharSequence in, ByteBuffer out) {
|
||||
final int inLength = in.length();
|
||||
int outIx = out.position();
|
||||
@@ -335,8 +329,7 @@ final public class Utf8Safe extends Utf8 {
|
||||
}
|
||||
}
|
||||
|
||||
private static int encodeUtf8Array(CharSequence in, byte[] out,
|
||||
int offset, int length) {
|
||||
private static int encodeUtf8Array(CharSequence in, byte[] out, int offset, int length) {
|
||||
int utf16Length = in.length();
|
||||
int j = offset;
|
||||
int i = 0;
|
||||
@@ -366,8 +359,7 @@ final public class Utf8Safe extends Utf8 {
|
||||
// Minimum code point represented by a surrogate pair is 0x10000, 17 bits,
|
||||
// four UTF-8 bytes
|
||||
final char low;
|
||||
if (i + 1 == in.length()
|
||||
|| !Character.isSurrogatePair(c, (low = in.charAt(++i)))) {
|
||||
if (i + 1 == in.length() || !Character.isSurrogatePair(c, (low = in.charAt(++i)))) {
|
||||
throw new UnpairedSurrogateException((i - 1), utf16Length);
|
||||
}
|
||||
int codePoint = Character.toCodePoint(c, low);
|
||||
@@ -379,8 +371,7 @@ final public class Utf8Safe extends Utf8 {
|
||||
// If we are surrogates and we're not a surrogate pair, always throw an
|
||||
// UnpairedSurrogateException instead of an ArrayOutOfBoundsException.
|
||||
if ((Character.MIN_SURROGATE <= c && c <= Character.MAX_SURROGATE)
|
||||
&& (i + 1 == in.length()
|
||||
|| !Character.isSurrogatePair(c, in.charAt(i + 1)))) {
|
||||
&& (i + 1 == in.length() || !Character.isSurrogatePair(c, in.charAt(i + 1)))) {
|
||||
throw new UnpairedSurrogateException(i, utf16Length);
|
||||
}
|
||||
throw new ArrayIndexOutOfBoundsException("Failed writing " + c + " at index " + j);
|
||||
@@ -402,8 +393,7 @@ final public class Utf8Safe extends Utf8 {
|
||||
public void encodeUtf8(CharSequence in, ByteBuffer out) {
|
||||
if (out.hasArray()) {
|
||||
int start = out.arrayOffset();
|
||||
int end = encodeUtf8Array(in, out.array(), start + out.position(),
|
||||
out.remaining());
|
||||
int end = encodeUtf8Array(in, out.array(), start + out.position(), out.remaining());
|
||||
out.position(end - start);
|
||||
} else {
|
||||
encodeUtf8Buffer(in, out);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user