Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
7cce55d8
Commit
7cce55d8
authored
Apr 28, 2016
by
Wouter van Oortmerssen
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3854 from pjulien/3853
Fix for #3853
parents
933c195e
9fb87f81
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
12 deletions
+38
-12
Table.java
java/com/google/flatbuffers/Table.java
+38
-12
No files found.
java/com/google/flatbuffers/Table.java
View file @
7cce55d8
...
...
@@ -19,6 +19,11 @@ package com.google.flatbuffers;
import
static
com
.
google
.
flatbuffers
.
Constants
.*;
import
java.nio.ByteBuffer
;
import
java.nio.ByteOrder
;
import
java.nio.CharBuffer
;
import
java.nio.charset.CharacterCodingException
;
import
java.nio.charset.Charset
;
import
java.nio.charset.CharsetDecoder
;
import
java.nio.charset.CoderResult
;
/// @cond FLATBUFFERS_INTERNAL
...
...
@@ -26,6 +31,13 @@ import java.nio.ByteOrder;
* All tables in the generated code derive from this class, and add their own accessors.
*/
public
class
Table
{
private
final
static
ThreadLocal
<
CharsetDecoder
>
UTF8_DECODER
=
new
ThreadLocal
<
CharsetDecoder
>()
{
@Override
protected
CharsetDecoder
initialValue
()
{
return
Charset
.
forName
(
"UTF-8"
).
newDecoder
();
}
};
private
final
static
ThreadLocal
<
CharBuffer
>
CHAR_BUFFER
=
new
ThreadLocal
<
CharBuffer
>();
/** Used to hold the position of the `bb` buffer. */
protected
int
bb_pos
;
/** The underlying ByteBuffer to hold the data of the Table. */
...
...
@@ -71,20 +83,34 @@ public class Table {
* @return Returns a `String` from the data stored inside the FlatBuffer at `offset`.
*/
protected
String
__string
(
int
offset
)
{
CharsetDecoder
decoder
=
UTF8_DECODER
.
get
();
decoder
.
reset
();
offset
+=
bb
.
getInt
(
offset
);
if
(
bb
.
hasArray
())
{
return
new
String
(
bb
.
array
(),
bb
.
arrayOffset
()
+
offset
+
SIZEOF_INT
,
bb
.
getInt
(
offset
),
FlatBufferBuilder
.
utf8charset
);
}
else
{
// 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
)];
bb
.
position
(
offset
+
SIZEOF_INT
);
bb
.
get
(
copy
);
return
new
String
(
copy
,
0
,
copy
.
length
,
FlatBufferBuilder
.
utf8charset
);
ByteBuffer
src
=
bb
.
duplicate
().
order
(
ByteOrder
.
LITTLE_ENDIAN
);
int
length
=
src
.
getInt
(
offset
);
src
.
position
(
offset
+
SIZEOF_INT
);
src
.
limit
(
offset
+
SIZEOF_INT
+
length
);
int
required
=
(
int
)((
float
)
length
*
decoder
.
maxCharsPerByte
());
CharBuffer
dst
=
CHAR_BUFFER
.
get
();
if
(
dst
==
null
||
dst
.
capacity
()
<
required
)
{
dst
=
CharBuffer
.
allocate
(
required
);
CHAR_BUFFER
.
set
(
dst
);
}
dst
.
clear
();
try
{
CoderResult
cr
=
decoder
.
decode
(
src
,
dst
,
true
);
if
(!
cr
.
isUnderflow
())
{
cr
.
throwException
();
}
}
catch
(
CharacterCodingException
x
)
{
throw
new
Error
(
x
);
}
return
dst
.
flip
().
toString
();
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment