diff options
author | pjulien <pjulien@gmail.com> | 2015-01-31 11:14:59 -0500 |
---|---|---|
committer | Wouter van Oortmerssen <wvo@google.com> | 2015-02-04 13:53:02 -0800 |
commit | 4d3db99283199ffd8d5fdbe84933444d25b980f4 (patch) | |
tree | 4b80d581108d889317abd3a903401849a8e5a40f /java | |
parent | 72b9501e69901abd2ba6fa7980996f3c1e244d93 (diff) | |
download | flatbuffers-4d3db99283199ffd8d5fdbe84933444d25b980f4.tar.gz flatbuffers-4d3db99283199ffd8d5fdbe84933444d25b980f4.tar.bz2 flatbuffers-4d3db99283199ffd8d5fdbe84933444d25b980f4.zip |
Issue #136
The satellite data of the ``ByteBuffer`` cannot be modified in
any way and stay thread safe in the presence of concurrent readers.
This implementation is simple and does introduce an allocation, however
without it multiple readers will quickly and continuously encounter
``IndexOutOfBoundsException`` exceptions.
An alternative, but possibly more controversial, implementation would
be to use ``Unsafe``. Using ``Unsafe``, it's possible to do an
array copy with a provided buffer index.
Change-Id: I851d4034e753b3be2931ee2249ec2c82dde43135
Diffstat (limited to 'java')
-rw-r--r-- | java/com/google/flatbuffers/Table.java | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/java/com/google/flatbuffers/Table.java b/java/com/google/flatbuffers/Table.java index c4263894..3ed99ad8 100644 --- a/java/com/google/flatbuffers/Table.java +++ b/java/com/google/flatbuffers/Table.java @@ -18,6 +18,7 @@ package com.google.flatbuffers; import static com.google.flatbuffers.Constants.*; import java.nio.ByteBuffer; +import java.nio.ByteOrder; // All tables in the generated code derive from this class, and add their own accessors. public class Table { @@ -46,13 +47,13 @@ public class Table { if (bb.hasArray()) { return new String(bb.array(), offset + SIZEOF_INT, bb.getInt(offset), FlatBufferBuilder.utf8charset); } else { - // We can't access .array(), since the ByteBuffer is read-only. + // We can't access .array(), since the ByteBuffer is read-only, + // off-heap or a memory map + ByteBuffer bb = this.bb.duplicate().order(ByteOrder.LITTLE_ENDIAN); // We're forced to make an extra copy: byte[] copy = new byte[bb.getInt(offset)]; - int old_pos = bb.position(); bb.position(offset + SIZEOF_INT); bb.get(copy); - bb.position(old_pos); return new String(copy, 0, copy.length, FlatBufferBuilder.utf8charset); } } @@ -77,12 +78,11 @@ public class Table { protected ByteBuffer __vector_as_bytebuffer(int vector_offset, int elem_size) { int o = __offset(vector_offset); if (o == 0) return null; - int old_pos = bb.position(); - bb.position(__vector(o)); - ByteBuffer nbb = bb.slice(); - bb.position(old_pos); - nbb.limit(__vector_len(o) * elem_size); - return nbb; + ByteBuffer bb = this.bb.duplicate().order(ByteOrder.LITTLE_ENDIAN); + int vectorstart = __vector(o); + bb.position(vectorstart); + bb.limit(vectorstart + __vector_len(o) * elem_size); + return bb; } // Initialize any Table-derived type to point to the union at the given offset. |