mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-29 15:22:01 +00:00
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 commit68be4420dd. * Revert "generate size-prefixed monster binary as well" This reverts commit2939516fdf. * 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
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
6b3f057bdc
commit
08cf50c54a
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user