Commit 08cf50c5 authored by Robert Schmidtke's avatar Robert Schmidtke Committed by Wouter van Oortmerssen

Java/C#/Python prefixed size support (#4445)

* initial changes to support size prefixed buffers in Java

* add slice equivalent to CSharp ByteBuffer

* resolve TODO for slicing in CSharp code generation

* add newly generated Java and CSharp test sources

* fix typo in comment

* add FinishSizePrefixed methods to CSharp FlatBufferBuilder as well

* add option to allow writing the prefix as well

* generate size-prefixed monster binary as well

* extend JavaTest to test the size prefixed binary as well

* use constants for size prefix length

* fuse common code for getRootAs and getSizePrefixedRootAs

* pulled file identifier out of if

* add FinishSizePrefixed, GetSizePrefixedRootAs support for Python

* Revert "extend JavaTest to test the size prefixed binary as well"

This reverts commit 68be4420dda47e8d0600bb19691f03be71503a68.

* Revert "generate size-prefixed monster binary as well"

This reverts commit 2939516fdf78df4f061c627221e232b312301417.

* fix ByteBuffer.cs Slice() method; add proper CSharp and Java tests

* fix unused parameter

* increment version number

* pulled out generated methods into separate utility class

* pulled out generated methods into separate utility class for Python

* fix indentation

* remove unnecessary comment

* fix newline and copyright

* add ByteBufferUtil to csproj compilation

* hide ByteBuffer's internal data; track offset into parent's array

* test unsafe versions as well; compile and run in debug mode

* clarify help text for size prefix

* move ByteBuffer slicing behavior to subclass

* fix protection levels

* add size prefix support for text generation

* add ByteBufferSlice to csproj compilation

* revert size prefix handling for nested buffers

* use duplicate instead of slice for removing size prefix

* remove slice subclass and use duplicate for removing size prefix

* remove slice specific tests

* remove superfluous command line option
parent 6b3f057b
......@@ -47,6 +47,7 @@ snapshot.sh
tags
tests/go_gen
tests/monsterdata_java_wire.mon
tests/monsterdata_java_wire_sp.mon
tests/monsterdata_go_wire.mon
tests/monsterdata_javascript_wire.mon
tests/unicode_test.mon
......
/*
* Copyright 2017 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;
/// @file
/// @addtogroup flatbuffers_java_api
/// @{
/**
* 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;
}
}
/// @}
......@@ -37,6 +37,8 @@ public class Constants {
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;
}
/// @endcond
......@@ -816,30 +816,75 @@ public class FlatBufferBuilder {
* Finalize a buffer, pointing to the given `root_table`.
*
* @param root_table An offset to be added to the buffer.
* @param size_prefix Whether to prefix the size to the buffer.
*/
public void finish(int root_table) {
prep(minalign, SIZEOF_INT);
protected void finish(int root_table, boolean size_prefix) {
prep(minalign, SIZEOF_INT + (size_prefix ? SIZEOF_INT : 0));
addOffset(root_table);
if (size_prefix) {
addInt(bb.capacity() - space);
}
bb.position(space);
finished = true;
}
/**
* Finalize a buffer, pointing to the given `root_table`.
*
* @param root_table An offset to be added to the buffer.
*/
public void finish(int root_table) {
finish(root_table, false);
}
/**
* Finalize a buffer, pointing to the given `root_table`, with the size prefixed.
*
* @param root_table An offset to be added to the buffer.
*/
public void finishSizePrefixed(int root_table) {
finish(root_table, true);
}
/**
* Finalize a buffer, pointing to the given `root_table`.
*
* @param root_table An offset to be added to the buffer.
* @param file_identifier A FlatBuffer file identifier to be added to the buffer before
* `root_table`.
* @param size_prefix Whether to prefix the size to the buffer.
*/
public void finish(int root_table, String file_identifier) {
prep(minalign, SIZEOF_INT + FILE_IDENTIFIER_LENGTH);
protected void finish(int root_table, String file_identifier, boolean size_prefix) {
prep(minalign, SIZEOF_INT + FILE_IDENTIFIER_LENGTH + (size_prefix ? SIZEOF_INT : 0));
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));
}
finish(root_table);
finish(root_table, size_prefix);
}
/**
* Finalize a buffer, pointing to the given `root_table`.
*
* @param root_table An offset to be added to the buffer.
* @param file_identifier A FlatBuffer file identifier to be added to the buffer before
* `root_table`.
*/
public void finish(int root_table, String file_identifier) {
finish(root_table, file_identifier, false);
}
/**
* Finalize a buffer, pointing to the given `root_table`, with the size prefixed.
*
* @param root_table An offset to be added to the buffer.
* @param file_identifier A FlatBuffer file identifier to be added to the buffer before
* `root_table`.
*/
public void finishSizePrefixed(int root_table, String file_identifier) {
finish(root_table, file_identifier, true);
}
/**
......
......@@ -30,6 +30,8 @@
//
using System;
using System.IO;
using System.Text;
namespace FlatBuffers
{
......@@ -38,12 +40,12 @@ namespace FlatBuffers
/// </summary>
public class ByteBuffer
{
private readonly byte[] _buffer;
protected byte[] _buffer;
private int _pos; // Must track start of the buffer.
public int Length { get { return _buffer.Length; } }
public byte[] Data { get { return _buffer; } }
public ByteBuffer(int size) : this(new byte[size]) { }
public ByteBuffer(byte[] buffer) : this(buffer, 0) { }
......@@ -63,11 +65,64 @@ namespace FlatBuffers
_pos = 0;
}
// Create a new ByteBuffer on the same underlying data.
// The new ByteBuffer's position will be same as this buffer's.
public ByteBuffer Duplicate()
{
return new ByteBuffer(_buffer, Position);
}
// Increases the size of the ByteBuffer, and copies the old data towards
// the end of the new buffer.
public void GrowFront(int newSize)
{
if ((Length & 0xC0000000) != 0)
throw new Exception(
"ByteBuffer: cannot grow buffer beyond 2 gigabytes.");
if (newSize < Length)
throw new Exception("ByteBuffer: cannot truncate buffer.");
byte[] newBuffer = new byte[newSize];
Buffer.BlockCopy(_buffer, 0, newBuffer, newSize - Length,
Length);
_buffer = newBuffer;
}
public byte[] ToArray(int pos, int len)
{
byte[] arr = new byte[len];
Buffer.BlockCopy(_buffer, pos, arr, 0, len);
return arr;
}
public byte[] ToSizedArray()
{
return ToArray(Position, Length - Position);
}
public byte[] ToFullArray()
{
return ToArray(0, Length);
}
public ArraySegment<byte> ToArraySegment(int pos, int len)
{
return new ArraySegment<byte>(_buffer, pos, len);
}
public MemoryStream ToMemoryStream(int pos, int len)
{
return new MemoryStream(_buffer, pos, len);
}
#if !UNSAFE_BYTEBUFFER
// Pre-allocated helper arrays for convertion.
private float[] floathelper = new[] { 0.0f };
private int[] inthelper = new[] { 0 };
private double[] doublehelper = new[] { 0.0 };
private ulong[] ulonghelper = new[] { 0UL };
#endif // !UNSAFE_BYTEBUFFER
// Helper functions for the unsafe version.
static public ushort ReverseBytes(ushort input)
......@@ -136,7 +191,6 @@ namespace FlatBuffers
}
#endif // !UNSAFE_BYTEBUFFER
private void AssertOffsetAndLength(int offset, int length)
{
#if !BYTEBUFFER_NO_BOUNDS_CHECK
......@@ -171,6 +225,13 @@ namespace FlatBuffers
PutByte(offset, value);
}
public void PutStringUTF8(int offset, string value)
{
AssertOffsetAndLength(offset, value.Length);
Encoding.UTF8.GetBytes(value, 0, value.Length,
_buffer, offset);
}
#if UNSAFE_BYTEBUFFER
// Unsafe but more efficient versions of Put*.
public void PutShort(int offset, short value)
......@@ -321,6 +382,11 @@ namespace FlatBuffers
return _buffer[index];
}
public string GetStringUTF8(int startPos, int len)
{
return Encoding.UTF8.GetString(_buffer, startPos, len);
}
#if UNSAFE_BYTEBUFFER
// Unsafe but more efficient versions of Get*.
public short GetShort(int offset)
......
/*
* Copyright 2017 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.
*/
using System;
namespace FlatBuffers
{
/// <summary>
/// Class that collects utility functions around `ByteBuffer`.
/// </summary>
public class ByteBufferUtil
{
// Extract the size prefix from a `ByteBuffer`.
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.
public static ByteBuffer RemoveSizePrefix(ByteBuffer bb) {
ByteBuffer s = bb.Duplicate();
s.Position += FlatBufferConstants.SizePrefixLength;
return s;
}
}
}
......@@ -59,7 +59,7 @@ namespace FlatBuffers
throw new ArgumentOutOfRangeException("initialSize",
initialSize, "Must be greater than zero");
_space = initialSize;
_bb = new ByteBuffer(new byte[initialSize]);
_bb = new ByteBuffer(initialSize);
}
/// <summary>
......@@ -99,18 +99,7 @@ namespace FlatBuffers
// the end of the new buffer (since we build the buffer backwards).
void GrowBuffer()
{
var oldBuf = _bb.Data;
var oldBufSize = oldBuf.Length;
if ((oldBufSize & 0xC0000000) != 0)
throw new Exception(
"FlatBuffers: cannot grow buffer beyond 2 gigabytes.");
var newBufSize = oldBufSize << 1;
var newBuf = new byte[newBufSize];
Buffer.BlockCopy(oldBuf, 0, newBuf, newBufSize - oldBufSize,
oldBufSize);
_bb = new ByteBuffer(newBuf, newBufSize);
_bb.GrowFront(_bb.Length << 1);
}
// Prepare to write an element of `size` after `additional_bytes`
......@@ -475,7 +464,7 @@ namespace FlatBuffers
AddByte(0);
var utf8StringLen = Encoding.UTF8.GetByteCount(s);
StartVector(1, utf8StringLen, 1);
Encoding.UTF8.GetBytes(s, 0, s.Length, _bb.Data, _space -= utf8StringLen);
_bb.PutStringUTF8(_space -= utf8StringLen, s);
return new StringOffset(EndVector().Value);
}
......@@ -586,13 +575,41 @@ namespace FlatBuffers
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
public void Finish(int rootTable)
/// <param name="sizePrefix">
/// Whether to prefix the size to the buffer.
/// </param>
protected void Finish(int rootTable, bool sizePrefix)
{
Prep(_minAlign, sizeof(int));
Prep(_minAlign, sizeof(int) + (sizePrefix ? sizeof(int) : 0));
AddOffset(rootTable);
if (sizePrefix) {
AddInt(_bb.Length - _space);
}
_bb.Position = _space;
}
/// <summary>
/// Finalize a buffer, pointing to the given `root_table`.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
public void Finish(int rootTable)
{
Finish(rootTable, false);
}
/// <summary>
/// Finalize a buffer, pointing to the given `root_table`, with the size prefixed.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
public void FinishSizePrefixed(int rootTable)
{
Finish(rootTable, true);
}
/// <summary>
/// Get the ByteBuffer representing the FlatBuffer.
/// </summary>
......@@ -615,41 +632,69 @@ namespace FlatBuffers
/// </returns>
public byte[] SizedByteArray()
{
var newArray = new byte[_bb.Data.Length - _bb.Position];
Buffer.BlockCopy(_bb.Data, _bb.Position, newArray, 0,
_bb.Data.Length - _bb.Position);
return newArray;
}
/// <summary>
/// Finalize a buffer, pointing to the given `rootTable`.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
/// <param name="fileIdentifier">
/// A FlatBuffer file identifier to be added to the buffer before
/// `root_table`.
/// </param>
public void Finish(int rootTable, string fileIdentifier)
{
Prep(_minAlign, sizeof(int) +
FlatBufferConstants.FileIdentifierLength);
if (fileIdentifier.Length !=
FlatBufferConstants.FileIdentifierLength)
throw new ArgumentException(
"FlatBuffers: file identifier must be length " +
FlatBufferConstants.FileIdentifierLength,
"fileIdentifier");
for (int i = FlatBufferConstants.FileIdentifierLength - 1; i >= 0;
i--)
{
AddByte((byte)fileIdentifier[i]);
}
Finish(rootTable);
return _bb.ToSizedArray();
}
/// <summary>
/// Finalize a buffer, pointing to the given `rootTable`.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
/// <param name="fileIdentifier">
/// A FlatBuffer file identifier to be added to the buffer before
/// `root_table`.
/// </param>
/// <param name="sizePrefix">
/// Whether to prefix the size to the buffer.
/// </param>
protected void Finish(int rootTable, string fileIdentifier, bool sizePrefix)
{
Prep(_minAlign, sizeof(int) + (sizePrefix ? sizeof(int) : 0) +
FlatBufferConstants.FileIdentifierLength);
if (fileIdentifier.Length !=
FlatBufferConstants.FileIdentifierLength)
throw new ArgumentException(
"FlatBuffers: file identifier must be length " +
FlatBufferConstants.FileIdentifierLength,
"fileIdentifier");
for (int i = FlatBufferConstants.FileIdentifierLength - 1; i >= 0;
i--)
{
AddByte((byte)fileIdentifier[i]);
}
Finish(rootTable, sizePrefix);
}
/// <summary>
/// Finalize a buffer, pointing to the given `rootTable`.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
/// <param name="fileIdentifier">
/// A FlatBuffer file identifier to be added to the buffer before
/// `root_table`.
/// </param>
public void Finish(int rootTable, string fileIdentifier)
{
Finish(rootTable, fileIdentifier, false);
}
/// <summary>
/// Finalize a buffer, pointing to the given `rootTable`, with the size prefixed.
/// </summary>
/// <param name="rootTable">
/// An offset to be added to the buffer.
/// </param>
/// <param name="fileIdentifier">
/// A FlatBuffer file identifier to be added to the buffer before
/// `root_table`.
/// </param>
public void FinishSizePrefixed(int rootTable, string fileIdentifier)
{
Finish(rootTable, fileIdentifier, true);
}
}
}
......
......@@ -24,5 +24,6 @@ namespace FlatBuffers
public static class FlatBufferConstants
{
public const int FileIdentifierLength = 4;
public const int SizePrefixLength = 4;
}
}
......@@ -60,7 +60,7 @@ namespace FlatBuffers
offset += bb.GetInt(offset);
var len = bb.GetInt(offset);
var startPos = offset + sizeof(int);
return Encoding.UTF8.GetString(bb.Data, startPos , len);
return bb.GetStringUTF8(startPos, len);
}
// Get the length of a vector whose offset is stored at "offset" in this object.
......@@ -91,7 +91,7 @@ namespace FlatBuffers
var pos = this.__vector(o);
var len = this.__vector_len(o);
return new ArraySegment<byte>(this.bb.Data, pos, len);
return bb.ToArraySegment(pos, len);
}
// Initialize any Table-derived type to point to the union at the given offset.
......@@ -126,10 +126,11 @@ namespace FlatBuffers
var startPos_1 = offset_1 + sizeof(int);
var startPos_2 = offset_2 + sizeof(int);
var len = Math.Min(len_1, len_2);
byte[] bbArray = bb.Data;
for(int i = 0; i < len; i++) {
if (bbArray[i + startPos_1] != bbArray[i + startPos_2])
return bbArray[i + startPos_1] - bbArray[i + startPos_2];
byte b1 = bb.Get(i + startPos_1);
byte b2 = bb.Get(i + startPos_2);
if (b1 != b2)
return b1 - b2;
}
return len_1 - len_2;
}
......@@ -142,10 +143,10 @@ namespace FlatBuffers
var len_2 = key.Length;
var startPos_1 = offset_1 + sizeof(int);
var len = Math.Min(len_1, len_2);
byte[] bbArray = bb.Data;
for (int i = 0; i < len; i++) {
if (bbArray[i + startPos_1] != key[i])
return bbArray[i + startPos_1] - key[i];
byte b = bb.Get(i + startPos_1);
if (b != key[i])
return b - key[i];
}
return len_1 - len_2;
}
......
......@@ -483,14 +483,32 @@ class Builder(object):
self.current_vtable[slotnum] = self.Offset()
## @endcond
def Finish(self, rootTable):
def __Finish(self, rootTable, sizePrefix):
"""Finish finalizes a buffer, pointing to the given `rootTable`."""
N.enforce_number(rootTable, N.UOffsetTFlags)
self.Prep(self.minalign, N.UOffsetTFlags.bytewidth)
prepSize = N.UOffsetTFlags.bytewidth
if sizePrefix:
prepSize += N.Int32Flags.bytewidth
self.Prep(self.minalign, prepSize)
self.PrependUOffsetTRelative(rootTable)
if sizePrefix:
size = len(self.Bytes) - self.Head()
N.enforce_number(size, N.Int32Flags)
self.PrependInt32(size)
self.finished = True
return self.Head()
def Finish(self, rootTable):
"""Finish finalizes a buffer, pointing to the given `rootTable`."""
return self.__Finish(rootTable, False)
def FinishSizePrefixed(self, rootTable):
"""
Finish finalizes a buffer, pointing to the given `rootTable`,
with the size prefixed.
"""
return self.__Finish(rootTable, True)
## @cond FLATBUFFERS_INTERNAL
def Prepend(self, flags, off):
self.Prep(flags.bytewidth, 0)
......
# Copyright 2017 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.
from . import encode
from . import number_types
from . import packer
def GetSizePrefix(buf, offset):
"""Extract the size prefix from a buffer."""
return encode.Get(packer.int32, buf, offset)
def RemoveSizePrefix(buf, offset):
"""
Create a slice of a size-prefixed buffer that has
its position advanced just past the size prefix.
"""
return buf, offset + number_types.Int32Flags.bytewidth
......@@ -1331,17 +1331,22 @@ class GeneralGenerator : public BaseGenerator {
}
code += " return " + GenOffsetConstruct(struct_def, "o") + ";\n }\n";
if (parser_.root_struct_def_ == &struct_def) {
code += " public static void ";
code += FunctionStart('F') + "inish" + struct_def.name;
code +=
"Buffer(FlatBufferBuilder builder, " + GenOffsetType(struct_def);
code += " offset) {";
code += " builder." + FunctionStart('F') + "inish(offset";
if (lang_.language == IDLOptions::kCSharp) { code += ".Value"; }
if (parser_.file_identifier_.length())
code += ", \"" + parser_.file_identifier_ + "\"";
code += "); }\n";
std::string size_prefix[] = { "", "SizePrefixed" };
for (int i = 0; i < 2; ++i) {
code += " public static void ";
code += FunctionStart('F') + "inish" + size_prefix[i] +
struct_def.name;
code += "Buffer(FlatBufferBuilder builder, " +
GenOffsetType(struct_def);
code += " offset) {";
code += " builder." + FunctionStart('F') + "inish" + size_prefix[i] +
"(offset";
if (lang_.language == IDLOptions::kCSharp) { code += ".Value"; }
if (parser_.file_identifier_.length())
code += ", \"" + parser_.file_identifier_ + "\"";
code += "); }\n";
}
}
}
// Only generate key compare function for table,
......
......@@ -44,8 +44,7 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_PutByteCannotPutAtOffsetPastLength()
{
var buffer = new byte[1];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutByte(1, 99));
}
#endif
......@@ -66,8 +65,7 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortCannotPutAtOffsetPastLength()
{
var buffer = new byte[2];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(2, 99));
}
#endif
......@@ -76,16 +74,14 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortChecksLength()
{
var buffer = new byte[1];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(0, 99));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortChecksLengthAndOffset()
{
var buffer = new byte[2];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(1, 99));
}
#endif
......@@ -108,24 +104,21 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntCannotPutAtOffsetPastLength()
{
var buffer = new byte[4];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(4);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntChecksLength()
{
var buffer = new byte[1];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(0, 0x0A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntChecksLengthAndOffset()
{
var buffer = new byte[4];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(4);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D));
}
#endif
......@@ -152,24 +145,21 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongCannotPutAtOffsetPastLength()
{
var buffer = new byte[8];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(8);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongChecksLength()
{
var buffer = new byte[1];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(0, 0x010203040A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongChecksLengthAndOffset()
{
var buffer = new byte[8];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(8);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D));
}
#endif
......@@ -187,8 +177,7 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_GetByteChecksOffset()
{
var buffer = new byte[1];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(()=>uut.Get(1));
}
#endif
......@@ -207,16 +196,14 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_GetShortChecksOffset()
{
var buffer = new byte[2];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(2));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetShortChecksLength()
{
var buffer = new byte[2];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(1));
}
#endif
......@@ -237,16 +224,14 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_GetIntChecksOffset()
{
var buffer = new byte[4];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(4);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(4));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetIntChecksLength()
{
var buffer = new byte[2];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(0));
}
#endif
......@@ -271,16 +256,14 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void ByteBuffer_GetLongChecksOffset()
{
var buffer = new byte[8];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(8);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(8));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetLongChecksLength()
{
var buffer = new byte[7];
var uut = new ByteBuffer(buffer);
var uut = new ByteBuffer(7);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(0));
}
#endif
......@@ -317,5 +300,49 @@ namespace FlatBuffers.Test
var rereverse = ByteBuffer.ReverseBytes(reverse);
Assert.AreEqual(original, rereverse);
}
[FlatBuffersTestMethod]
public void ByteBuffer_ToFullArray_MatchesBuffer()
{
var buffer = new byte[4];
buffer[0] = 0x0D;
buffer[1] = 0x0C;
buffer[2] = 0x0B;
buffer[3] = 0x0A;
var uut = new ByteBuffer(buffer);
Assert.ArrayEqual(buffer, uut.ToFullArray());
}
[FlatBuffersTestMethod]
public void ByteBuffer_ToSizedArray_MatchesBuffer()
{
var buffer = new byte[4];
buffer[0] = 0x0D;
buffer[1] = 0x0C;
buffer[2] = 0x0B;
buffer[3] = 0x0A;
var uut = new ByteBuffer(buffer);
Assert.ArrayEqual(buffer, uut.ToFullArray());
}
[FlatBuffersTestMethod]
public void ByteBuffer_Duplicate_MatchesBuffer()
{
var buffer = new byte[4];
buffer[0] = 0x0D;
buffer[1] = 0x0C;
buffer[2] = 0x0B;
buffer[3] = 0x0A;
var uut = new ByteBuffer(buffer);
Assert.AreEqual(0x0A0B0C0D, uut.GetInt(0));
// Advance by two bytes
uut.Position = 2; uut = uut.Duplicate();
Assert.AreEqual(0x0A0B, uut.GetShort(2));
// Advance by one more byte
uut.Position = 1; uut = uut.Duplicate();
Assert.AreEqual(0x0A, uut.Get(3));
}
}
}
......@@ -41,6 +41,9 @@
<Compile Include="..\..\net\FlatBuffers\ByteBuffer.cs">
<Link>FlatBuffers\ByteBuffer.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\ByteBufferUtil.cs">
<Link>FlatBuffers\ByteBufferUtil.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\IFlatbufferObject.cs">
<Link>FlatBuffers\IFlatbufferObject.cs</Link>
</Compile>
......
......@@ -32,6 +32,12 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void CanCreateNewFlatBufferFromScratch()
{
CanCreateNewFlatBufferFromScratch(true);
CanCreateNewFlatBufferFromScratch(false);
}
private void CanCreateNewFlatBufferFromScratch(bool sizePrefix)
{
// Second, let's create a FlatBuffer from scratch in C#, and test it also.
// We use an initial size of 1 to exercise the reallocation algorithm,
......@@ -95,22 +101,40 @@ namespace FlatBuffers.Test
Monster.AddTestarrayoftables(fbb, sortMons);
var mon = Monster.EndMonster(fbb);
Monster.FinishMonsterBuffer(fbb, mon);
if (sizePrefix)
{
Monster.FinishSizePrefixedMonsterBuffer(fbb, mon);
}
else
{
Monster.FinishMonsterBuffer(fbb, mon);
}
// Dump to output directory so we can inspect later, if needed
using (var ms = new MemoryStream(fbb.DataBuffer.Data, fbb.DataBuffer.Position, fbb.Offset))
using (var ms = fbb.DataBuffer.ToMemoryStream(fbb.DataBuffer.Position, fbb.Offset))
{
var data = ms.ToArray();
File.WriteAllBytes(@"Resources/monsterdata_cstest.mon",data);
string filename = @"Resources/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon";
File.WriteAllBytes(filename, data);
}
// Remove the size prefix if necessary for further testing
ByteBuffer dataBuffer = fbb.DataBuffer;
if (sizePrefix)
{
Assert.AreEqual(ByteBufferUtil.GetSizePrefix(dataBuffer) + FlatBufferConstants.SizePrefixLength,
dataBuffer.Length - dataBuffer.Position);
dataBuffer = ByteBufferUtil.RemoveSizePrefix(dataBuffer);
}
// Now assert the buffer
TestBuffer(fbb.DataBuffer);
TestBuffer(dataBuffer);
//Attempt to mutate Monster fields and check whether the buffer has been mutated properly
// revert to original values after testing
Monster monster = Monster.GetRootAsMonster(fbb.DataBuffer);
Monster monster = Monster.GetRootAsMonster(dataBuffer);
// mana is optional and does not exist in the buffer so the mutation should fail
// the mana field should retain its default value
......@@ -161,12 +185,12 @@ namespace FlatBuffers.Test
pos.MutateX(1.0f);
Assert.AreEqual(pos.X, 1.0f);
TestBuffer(fbb.DataBuffer);
TestBuffer(dataBuffer);
}
private void TestBuffer(ByteBuffer bb)
{
var monster = Monster.GetRootAsMonster(bb);
Monster monster = Monster.GetRootAsMonster(bb);
Assert.AreEqual(80, monster.Hp);
Assert.AreEqual(150, monster.Mana);
......
......@@ -2,8 +2,22 @@
# Testing C# on Linux using Mono.
mcs -out:fbnettest.exe ../../net/FlatBuffers/*.cs ../MyGame/Example/*.cs FlatBuffersTestClassAttribute.cs FlatBuffersTestMethodAttribute.cs Assert.cs FlatBuffersExampleTests.cs Program.cs ByteBufferTests.cs FlatBufferBuilderTests.cs FlatBuffersFuzzTests.cs FuzzTestData.cs Lcg.cs TestTable.cs
./fbnettest.exe
mcs -debug -out:./fbnettest.exe \
../../net/FlatBuffers/*.cs ../MyGame/Example/*.cs ../MyGame/*.cs \
FlatBuffersTestClassAttribute.cs FlatBuffersTestMethodAttribute.cs Assert.cs FlatBuffersExampleTests.cs Program.cs ByteBufferTests.cs FlatBufferBuilderTests.cs FlatBuffersFuzzTests.cs FuzzTestData.cs Lcg.cs TestTable.cs
mono --debug ./fbnettest.exe
rm fbnettest.exe
rm Resources/monsterdata_cstest.mon
rm Resources/monsterdata_cstest_sp.mon
# Repeat with unsafe versions
mcs -debug -out:./fbnettest.exe \
-unsafe -d:UNSAFE_BYTEBUFFER \
../../net/FlatBuffers/*.cs ../MyGame/Example/*.cs ../MyGame/*.cs \
FlatBuffersTestClassAttribute.cs FlatBuffersTestMethodAttribute.cs Assert.cs FlatBuffersExampleTests.cs Program.cs ByteBufferTests.cs FlatBufferBuilderTests.cs FlatBuffersFuzzTests.cs FuzzTestData.cs Lcg.cs TestTable.cs
mono --debug ./fbnettest.exe
rm fbnettest.exe
rm Resources/monsterdata_cstest.mon
rm Resources/monsterdata_cstest_sp.mon
......@@ -21,6 +21,8 @@ import java.nio.channels.FileChannel;
import MyGame.Example.*;
import NamespaceA.*;
import NamespaceA.NamespaceB.*;
import com.google.flatbuffers.ByteBufferUtil;
import static com.google.flatbuffers.Constants.*;
import com.google.flatbuffers.FlatBufferBuilder;
class JavaTest {
......@@ -53,7 +55,8 @@ class JavaTest {
// better for performance.
FlatBufferBuilder fbb = new FlatBufferBuilder(1);
TestBuilderBasics(fbb);
TestBuilderBasics(fbb, true);
TestBuilderBasics(fbb, false);
TestExtendedBuffer(fbb.dataBuffer().asReadOnlyBuffer());
......@@ -244,14 +247,14 @@ class JavaTest {
FlatBufferBuilder fbb = new FlatBufferBuilder(1, new MappedByteBufferFactory());
TestBuilderBasics(fbb);
TestBuilderBasics(fbb, false);
}
static void TestSizedInputStream() {
// Test on default FlatBufferBuilder that uses HeapByteBuffer
FlatBufferBuilder fbb = new FlatBufferBuilder(1);
TestBuilderBasics(fbb);
TestBuilderBasics(fbb, false);
InputStream in = fbb.sizedInputStream();
byte[] array = fbb.sizedByteArray();
......@@ -271,7 +274,7 @@ class JavaTest {
TestEq(count, array.length);
}
static void TestBuilderBasics(FlatBufferBuilder fbb) {
static void TestBuilderBasics(FlatBufferBuilder fbb, boolean sizePrefix) {
int[] names = {fbb.createString("Frodo"), fbb.createString("Barney"), fbb.createString("Wilma")};
int[] off = new int[3];
Monster.startMonster(fbb);
......@@ -321,7 +324,11 @@ class JavaTest {
Monster.addTestarrayoftables(fbb, sortMons);
int mon = Monster.endMonster(fbb);
Monster.finishMonsterBuffer(fbb, mon);
if (sizePrefix) {
Monster.finishSizePrefixedMonsterBuffer(fbb, mon);
} else {
Monster.finishMonsterBuffer(fbb, mon);
}
// Write the result to a file for debugging purposes:
// Note that the binaries are not necessarily identical, since the JSON
......@@ -329,7 +336,8 @@ class JavaTest {
// Java code. They are functionally equivalent though.
try {
FileChannel fc = new FileOutputStream("monsterdata_java_wire.mon").getChannel();
String filename = "monsterdata_java_wire" + (sizePrefix ? "_sp" : "") + ".mon";
FileChannel fc = new FileOutputStream(filename).getChannel();
fc.write(fbb.dataBuffer().duplicate());
fc.close();
} catch(java.io.IOException e) {
......@@ -338,18 +346,24 @@ class JavaTest {
}
// Test it:
TestExtendedBuffer(fbb.dataBuffer());
ByteBuffer dataBuffer = fbb.dataBuffer();
if (sizePrefix) {
TestEq(ByteBufferUtil.getSizePrefix(dataBuffer) + SIZE_PREFIX_LENGTH,
dataBuffer.remaining());
dataBuffer = ByteBufferUtil.removeSizePrefix(dataBuffer);
}
TestExtendedBuffer(dataBuffer);
// Make sure it also works with read only ByteBuffers. This is slower,
// since creating strings incurs an additional copy
// (see Table.__string).
TestExtendedBuffer(fbb.dataBuffer().asReadOnlyBuffer());
TestExtendedBuffer(dataBuffer.asReadOnlyBuffer());
TestEnums();
//Attempt to mutate Monster fields and check whether the buffer has been mutated properly
// revert to original values after testing
Monster monster = Monster.getRootAsMonster(fbb.dataBuffer());
Monster monster = Monster.getRootAsMonster(dataBuffer);
// mana is optional and does not exist in the buffer so the mutation should fail
// the mana field should retain its default value
......
......@@ -203,6 +203,7 @@ public struct Monster : IFlatbufferObject
return new Offset<Monster>(o);
}
public static void FinishMonsterBuffer(FlatBufferBuilder builder, Offset<Monster> offset) { builder.Finish(offset.Value, "MONS"); }
public static void FinishSizePrefixedMonsterBuffer(FlatBufferBuilder builder, Offset<Monster> offset) { builder.FinishSizePrefixed(offset.Value, "MONS"); }
public static VectorOffset CreateSortedVectorOfMonster(FlatBufferBuilder builder, Offset<Monster>[] offsets) {
Array.Sort(offsets, (Offset<Monster> o1, Offset<Monster> o2) => Table.CompareStrings(Table.__offset(10, o1.Value, builder.DataBuffer), Table.__offset(10, o2.Value, builder.DataBuffer), builder.DataBuffer));
......
......@@ -228,6 +228,7 @@ public final class Monster extends Table {
return o;
}
public static void finishMonsterBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset, "MONS"); }
public static void finishSizePrefixedMonsterBuffer(FlatBufferBuilder builder, int offset) { builder.finishSizePrefixed(offset, "MONS"); }
@Override
protected int keysCompare(Integer o1, Integer o2, ByteBuffer _bb) { return compareStrings(__offset(10, o1, _bb), __offset(10, o2, _bb), _bb); }
......
No preview for this file type
// automatically generated by the FlatBuffers compiler, do not modify
import * as NS39599748 from "./namespace_test1_generated";
import * as NS4989953370203581498 from "./namespace_test1_generated";
/**
* @constructor
*/
......@@ -39,24 +39,24 @@ static getRootAsTableInFirstNS(bb:flatbuffers.ByteBuffer, obj?:TableInFirstNS):T
* @param {NamespaceA.NamespaceB.TableInNestedNS=} obj
* @returns {NamespaceA.NamespaceB.TableInNestedNS|null}
*/
fooTable(obj?:NS39599748.NamespaceA.NamespaceB.TableInNestedNS):NS39599748.NamespaceA.NamespaceB.TableInNestedNS|null {
fooTable(obj?:NS4989953370203581498.NamespaceA.NamespaceB.TableInNestedNS):NS4989953370203581498.NamespaceA.NamespaceB.TableInNestedNS|null {
var offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? (obj || new NS39599748.NamespaceA.NamespaceB.TableInNestedNS).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
return offset ? (obj || new NS4989953370203581498.NamespaceA.NamespaceB.TableInNestedNS).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
};
/**
* @returns {NamespaceA.NamespaceB.EnumInNestedNS}
*/
fooEnum():NS39599748.NamespaceA.NamespaceB.EnumInNestedNS {
fooEnum():NS4989953370203581498.NamespaceA.NamespaceB.EnumInNestedNS {
var offset = this.bb!.__offset(this.bb_pos, 6);
return offset ? /** @type {NamespaceA.NamespaceB.EnumInNestedNS} */ (this.bb!.readInt8(this.bb_pos + offset)) : NS39599748.NamespaceA.NamespaceB.EnumInNestedNS.A;
return offset ? /** @type {NamespaceA.NamespaceB.EnumInNestedNS} */ (this.bb!.readInt8(this.bb_pos + offset)) : NS4989953370203581498.NamespaceA.NamespaceB.EnumInNestedNS.A;
};
/**
* @param {NamespaceA.NamespaceB.EnumInNestedNS} value
* @returns {boolean}
*/
mutate_foo_enum(value:NS39599748.NamespaceA.NamespaceB.EnumInNestedNS):boolean {
mutate_foo_enum(value:NS4989953370203581498.NamespaceA.NamespaceB.EnumInNestedNS):boolean {
var offset = this.bb!.__offset(this.bb_pos, 6);
if (offset === 0) {
......@@ -71,9 +71,9 @@ mutate_foo_enum(value:NS39599748.NamespaceA.NamespaceB.EnumInNestedNS):boolean {
* @param {NamespaceA.NamespaceB.StructInNestedNS=} obj
* @returns {NamespaceA.NamespaceB.StructInNestedNS|null}
*/
fooStruct(obj?:NS39599748.NamespaceA.NamespaceB.StructInNestedNS):NS39599748.NamespaceA.NamespaceB.StructInNestedNS|null {
fooStruct(obj?:NS4989953370203581498.NamespaceA.NamespaceB.StructInNestedNS):NS4989953370203581498.NamespaceA.NamespaceB.StructInNestedNS|null {
var offset = this.bb!.__offset(this.bb_pos, 8);
return offset ? (obj || new NS39599748.NamespaceA.NamespaceB.StructInNestedNS).__init(this.bb_pos + offset, this.bb!) : null;
return offset ? (obj || new NS4989953370203581498.NamespaceA.NamespaceB.StructInNestedNS).__init(this.bb_pos + offset, this.bb!) : null;
};
/**
......@@ -95,8 +95,8 @@ static addFooTable(builder:flatbuffers.Builder, fooTableOffset:flatbuffers.Offse
* @param {flatbuffers.Builder} builder
* @param {NamespaceA.NamespaceB.EnumInNestedNS} fooEnum
*/
static addFooEnum(builder:flatbuffers.Builder, fooEnum:NS39599748.NamespaceA.NamespaceB.EnumInNestedNS) {
builder.addFieldInt8(1, fooEnum, NS39599748.NamespaceA.NamespaceB.EnumInNestedNS.A);
static addFooEnum(builder:flatbuffers.Builder, fooEnum:NS4989953370203581498.NamespaceA.NamespaceB.EnumInNestedNS) {
builder.addFieldInt8(1, fooEnum, NS4989953370203581498.NamespaceA.NamespaceB.EnumInNestedNS.A);
};
/**
......
......@@ -25,6 +25,7 @@ import unittest
from flatbuffers import compat
from flatbuffers import util
from flatbuffers.compat import range_func as compat_range
from flatbuffers.compat import NumpyRequiredForThisFeature
......@@ -56,9 +57,11 @@ def assertRaises(test_case, fn, exception_class):
class TestWireFormat(unittest.TestCase):
def test_wire_format(self):
# Verify that using the generated Python code builds a buffer without
# returning errors, and is interpreted correctly:
gen_buf, gen_off = make_monster_from_generated_code()
CheckReadBuffer(gen_buf, gen_off)
# returning errors, and is interpreted correctly, for size prefixed
# representation and regular:
for sizePrefix in [True, False]:
gen_buf, gen_off = make_monster_from_generated_code(sizePrefix = sizePrefix)
CheckReadBuffer(gen_buf, gen_off, sizePrefix = sizePrefix)
# Verify that the canonical flatbuffer file is readable by the
# generated Python code. Note that context managers are not part of
......@@ -74,7 +77,7 @@ class TestWireFormat(unittest.TestCase):
f.close()
def CheckReadBuffer(buf, offset):
def CheckReadBuffer(buf, offset, sizePrefix = False):
''' CheckReadBuffer checks that the given buffer is evaluated correctly
as the example Monster. '''
......@@ -83,6 +86,11 @@ def CheckReadBuffer(buf, offset):
if not stmt:
raise AssertionError('CheckReadBuffer case failed')
if sizePrefix:
size = util.GetSizePrefix(buf, offset)
# taken from the size of monsterdata_python_wire.mon, minus 4
asserter(size == 348)
buf, offset = util.RemoveSizePrefix(buf, offset)
monster = MyGame.Example.Monster.Monster.GetRootAsMonster(buf, offset)
asserter(monster.Hp() == 80)
......@@ -810,7 +818,7 @@ class TestByteLayout(unittest.TestCase):
])
def make_monster_from_generated_code():
def make_monster_from_generated_code(sizePrefix = False):
''' Use generated code to build the example Monster. '''
b = flatbuffers.Builder(0)
......@@ -871,7 +879,10 @@ def make_monster_from_generated_code():
MyGame.Example.Monster.MonsterAddVectorOfDoubles(b, VectorOfDoubles)
mon = MyGame.Example.Monster.MonsterEnd(b)
b.Finish(mon)
if sizePrefix:
b.FinishSizePrefixed(mon)
else:
b.Finish(mon)
return b.Bytes, b.Head()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment