mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
Implemented the file identifier functionality for Java.
Also fixed flatc not outputting these identifiers for files compiled on the command-line. Bug: 16983987 Change-Id: I8b714cfea3a8e144fa52133f62b2f7eda6eb044a Tested: on Linux
This commit is contained in:
27
java/flatbuffers/Constants.java
Normal file
27
java/flatbuffers/Constants.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2014 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 flatbuffers;
|
||||
|
||||
// Class that holds shared constants.
|
||||
|
||||
public class Constants {
|
||||
// Java doesn't seem to have these.
|
||||
static final int SIZEOF_SHORT = 2;
|
||||
static final int SIZEOF_INT = 4;
|
||||
static final int FILE_IDENTIFIER_LENGTH = 4;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import java.nio.charset.Charset;
|
||||
// Class that helps you build a FlatBuffer.
|
||||
// See the section "Use in Java" in the main FlatBuffers documentation.
|
||||
|
||||
public class FlatBufferBuilder {
|
||||
public class FlatBufferBuilder extends Constants {
|
||||
ByteBuffer bb; // Where we construct the FlatBuffer.
|
||||
int space; // Remaining space in the ByteBuffer.
|
||||
static final Charset utf8charset = Charset.forName("UTF-8");
|
||||
@@ -36,10 +36,6 @@ public class FlatBufferBuilder {
|
||||
int num_vtables = 0; // Number of entries in `vtables` in use.
|
||||
int vector_num_elems = 0; // For the current vector being built.
|
||||
|
||||
// Java doesn't seem to have these.
|
||||
final int SIZEOF_SHORT = 2;
|
||||
final int SIZEOF_INT = 4;
|
||||
|
||||
// Start with a buffer of size `initial_size`, then grow as required.
|
||||
public FlatBufferBuilder(int initial_size) {
|
||||
if (initial_size <= 0) initial_size = 1;
|
||||
@@ -251,6 +247,17 @@ public class FlatBufferBuilder {
|
||||
addOffset(root_table);
|
||||
}
|
||||
|
||||
public void finish(int root_table, String file_identifier) {
|
||||
prep(minalign, SIZEOF_INT + FILE_IDENTIFIER_LENGTH);
|
||||
if (file_identifier.length() != FILE_IDENTIFIER_LENGTH)
|
||||
throw new AssertionError("FlatBuffers: file identifier must be length " +
|
||||
FILE_IDENTIFIER_LENGTH);
|
||||
for (int i = FILE_IDENTIFIER_LENGTH - 1; i >= 0; i--) {
|
||||
addByte((byte)file_identifier.charAt(i));
|
||||
}
|
||||
addOffset(root_table);
|
||||
}
|
||||
|
||||
public ByteBuffer dataBuffer() { return bb; }
|
||||
|
||||
// The FlatBuffer data doesn't start at offset 0 in the ByteBuffer:
|
||||
|
||||
@@ -21,12 +21,10 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
// All tables in the generated code derive from this class, and add their own accessors.
|
||||
public class Table {
|
||||
public class Table extends Constants {
|
||||
protected int bb_pos;
|
||||
protected ByteBuffer bb;
|
||||
|
||||
final int SIZEOF_INT = 4;
|
||||
|
||||
// Look up a field in the vtable, return an offset into the object, or 0 if the field is not
|
||||
// present.
|
||||
protected int __offset(int vtable_offset) {
|
||||
@@ -75,4 +73,14 @@ public class Table {
|
||||
t.bb = bb;
|
||||
return t;
|
||||
}
|
||||
|
||||
protected static boolean __has_identifier(ByteBuffer bb, int offset, String ident) {
|
||||
if (ident.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(offset + SIZEOF_INT + i)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user