Java: Added access object for vector of struct and vector of tables. (#5233)

* Java: Added access object for vector of struct and vector of tables.

* Java: Workarounds removed when accessing the union vector.
This commit is contained in:
Kulikov Alexey
2019-09-23 23:22:43 +07:00
committed by Wouter van Oortmerssen
parent 97f3aa9174
commit e365c502ff
40 changed files with 989 additions and 21 deletions

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.flatbuffers;
import java.nio.ByteBuffer;
/// @cond FLATBUFFERS_INTERNAL
/**
* 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;
/**
* Get the start data of a vector.
*
* @return Returns the start of the vector data.
*/
protected int __vector() {
return vector;
}
/**
* Gets the element position in vector's ByteBuffer.
*
* @param j An `int` index of element into a vector.
* @return Returns the position of the vector element in a ByteBuffer.
*/
protected int __element(int j) {
return vector + j * element_size;
}
/**
* 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.
*/
protected void __reset(int _vector, int _element_size, ByteBuffer _bb) {
bb = _bb;
if (bb != null) {
vector = _vector;
length = bb.getInt(_vector - Constants.SIZEOF_INT);
element_size = _element_size;
} else {
vector = 0;
length = 0;
element_size = 0;
}
}
/**
* 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
* again to a {@code ByteBuffer}.
*/
public void reset() {
__reset(0, 0, null);
}
/**
* Get the length of a vector.
*
* @return Returns the length of the vector.
*/
public int length() {
return length;
}
}
/// @endcond

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.
*/
public final class BooleanVector extends BaseVector {
/**
* Assigns vector access object to vector data.
*
* @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`.
*/
public BooleanVector __assign(int _vector, ByteBuffer _bb) {
__reset(_vector, Constants.SIZEOF_BYTE, _bb); return this;
}
/**
* Reads the boolean at the given index.
*
* @param j The index from which the boolean will be read.
* @return the boolean value at the given index.
*/
public boolean get(int j) {
return 0 != bb.get(__element(j));
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.
*/
public final class ByteVector extends BaseVector {
/**
* Assigns vector access object to vector data.
*
* @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`.
*/
public ByteVector __assign(int vector, ByteBuffer bb) {
__reset(vector, Constants.SIZEOF_BYTE, bb); return this;
}
/**
* Reads the byte at the given index.
*
* @param j The index from which the byte will be read.
* @return the 8-bit value at the given index.
*/
public byte get(int 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.
*
* @param j The index from which the byte will be read.
* @return the unsigned 8-bit at the given index.
*/
public int getAsUnsigned(int j) {
return (int) get(j) & 0xFF;
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.
*/
public final class DoubleVector extends BaseVector {
/**
* Assigns vector access object to vector data.
*
* @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`.
*/
public DoubleVector __assign(int _vector, ByteBuffer _bb) {
__reset(_vector, Constants.SIZEOF_DOUBLE, _bb); return this;
}
/**
* Reads the double value at the given index.
*
* @param j The index from which the double value will be read.
* @return the double value at the given index.
*/
public double get(int j) {
return bb.getDouble(__element(j));
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.
*/
public final class FloatVector extends BaseVector {
/**
* Assigns vector access object to vector data.
*
* @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`.
*/
public FloatVector __assign(int _vector, ByteBuffer _bb) {
__reset(_vector, Constants.SIZEOF_FLOAT, _bb); return this;
}
/**
* Reads the float value at the given index.
*
* @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) {
return bb.getFloat(__element(j));
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.
*/
public final class IntVector extends BaseVector {
/**
* Assigns vector access object to vector data.
*
* @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`.
*/
public IntVector __assign(int _vector, ByteBuffer _bb) {
__reset(_vector, Constants.SIZEOF_INT, _bb); return this;
}
/**
* Reads the integer at the given index.
*
* @param j The index from which the integer will be read.
* @return the 32-bit value at the given index.
*/
public int get(int j) {
return bb.getInt(__element(j));
}
/**
* Reads the integer at the given index, zero-extends it to type long, and returns the result,
* which is therefore in the range 0 through 4294967295.
*
* @param j The index from which the integer will be read.
* @return the unsigned 32-bit at the given index.
*/
public long getAsUnsigned(int j) {
return (long) get(j) & 0xFFFFFFFFL;
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.
*/
public final class LongVector extends BaseVector {
/**
* Assigns vector access object to vector data.
*
* @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`.
*/
public LongVector __assign(int _vector, ByteBuffer _bb) {
__reset(_vector, Constants.SIZEOF_LONG, _bb); return this;
}
/**
* Reads the long value at the given index.
*
* @param j The index from which the long value will be read.
* @return the signed 64-bit value at the given index.
*/
public long get(int j) {
return bb.getLong(__element(j));
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.
*/
public final class ShortVector extends BaseVector {
/**
* Assigns vector access object to vector data.
*
* @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`.
*/
public ShortVector __assign(int _vector, ByteBuffer _bb) {
__reset(_vector, Constants.SIZEOF_SHORT, _bb); return this;
}
/**
* Reads the short value at the given index.
*
* @param j The index from which the short value will be read.
* @return the 16-bit value at the given index.
*/
public short get(int j) {
return bb.getShort(__element(j));
}
/**
* 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.
*/
public int getAsUnsigned(int j) {
return (int) get(j) & 0xFFFF;
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.
*/
public final class StringVector extends BaseVector {
private Utf8 utf8 = Utf8.getDefault();
/**
* Assigns vector access object to vector data.
*
* @param _vector Start data of a vector.
* @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`.
*/
public StringVector __assign(int _vector, int _element_size, ByteBuffer _bb) {
__reset(_vector, _element_size, _bb); return this;
}
/**
* Reads the String at the given index.
*
* @param j The index from which the String value will be read.
* @return the String at the given index.
*/
public String get(int j) {
return Table.__string(__element(j), bb, utf8);
}
}

View File

@@ -75,6 +75,13 @@ public class Table {
return offset + bb.getInt(offset);
}
/**
* Retrieve a relative offset.
*
* @param offset An `int` index into a ByteBuffer containing the relative offset.
* @param bb from which the relative offset will be retrieved.
* @return Returns the relative offset stored at `offset`.
*/
protected static int __indirect(int offset, ByteBuffer bb) {
return offset + bb.getInt(offset);
}
@@ -91,6 +98,23 @@ public class Table {
* @return Returns a `String` from the data stored inside the FlatBuffer at `offset`.
*/
protected String __string(int offset) {
return __string(offset, bb, utf8);
}
/**
* 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.
*
* @param offset An `int` index into the Table's ByteBuffer.
* @param bb Table ByteBuffer used to read a string at given offset.
* @param utf8 decoder that creates a Java `String` from UTF-8 characters.
* @return Returns a `String` from the data stored inside the FlatBuffer at `offset`.
*/
protected static String __string(int offset, ByteBuffer bb, Utf8 utf8) {
offset += bb.getInt(offset);
int length = bb.getInt(offset);
return utf8.decodeUtf8(bb, offset + SIZEOF_INT, length);
@@ -169,11 +193,19 @@ public class Table {
* @return Returns the Table that points to the union at `offset`.
*/
protected Table __union(Table t, int offset) {
offset += bb_pos;
t.bb_pos = offset + bb.getInt(offset);
t.bb = bb;
t.vtable_start = t.bb_pos - bb.getInt(t.bb_pos);
t.vtable_size = bb.getShort(t.vtable_start);
return __union(t, offset, bb);
}
/**
* Initialize any Table-derived type to point to the union at the given `offset`.
*
* @param t A `Table`-derived type that should point to the union at `offset`.
* @param offset An `int` index into the Table's ByteBuffer.
* @param bb Table ByteBuffer used to initialize the object Table-derived type.
* @return Returns the Table that points to the union at `offset`.
*/
protected static Table __union(Table t, int offset, ByteBuffer bb) {
t.__reset(__indirect(offset, bb), bb);
return t;
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.
*/
public final class UnionVector extends BaseVector {
/**
* Assigns vector access object to vector data.
*
* @param _vector Start data of a vector.
* @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`.
*/
public UnionVector __assign(int _vector, int _element_size, ByteBuffer _bb) {
__reset(_vector, _element_size, _bb); return this;
}
/**
* Initialize any Table-derived type to point to the union at the given `index`.
*
* @param obj A `Table`-derived type that should point to the union at `index`.
* @param j An `int` index into the union vector.
* @return Returns the Table that points to the union at `index`.
*/
public Table get(Table obj, int j) {
return Table.__union(obj, __element(j), bb);
}
}