@@ -19,18 +19,23 @@ public class ByteAccumulator
19
19
protected final byte [] _prefixBuffer ;
20
20
21
21
/**
22
- * Offset within {@link #_prefixBuffer} where there is room for prefix.
22
+ * Offset within {@link #_prefixBuffer} where there is room for encoding
23
+ * prefix (type, tag, length).
23
24
*/
24
25
protected final int _prefixOffset ;
25
-
26
+
26
27
protected final int _typedTag ;
27
28
28
29
protected Segment _firstSegment , _lastSegment ;
29
30
31
+ /**
32
+ * Total number of bytes contained within buffers, to be used for length prefix.
33
+ */
30
34
protected int _segmentBytes ;
31
35
32
36
/**
33
- * Used to cache start pointer for nested message's parent
37
+ * Pointer to start of contents provided by parent, preceding room
38
+ * for prefix (that is, same as or less than `_prefixOffset`)
34
39
*
35
40
* @since 2.8.8
36
41
*/
@@ -59,24 +64,25 @@ public void append(byte[] buf, int offset, int len) {
59
64
public ByteAccumulator finish (OutputStream out ,
60
65
byte [] input , int offset , int len ) throws IOException
61
66
{
62
- final byte [] prefix = _prefixBuffer ;
63
- int start = _prefixOffset ;
67
+ final byte [] prefixBuf = _prefixBuffer ;
64
68
int ptr ;
65
69
70
+ // First: encode full tag to use, now that we length to calculate prefix from
66
71
if (_typedTag == -1 ) {
67
- ptr = start ;
72
+ ptr = _prefixOffset ;
68
73
} else {
69
- ptr = ProtobufUtil .appendLengthLength (_typedTag , prefix , start );
74
+ ptr = ProtobufUtil .appendLengthLength (_typedTag , prefixBuf , _prefixOffset );
70
75
}
71
76
72
77
int plen = _segmentBytes + len ;
73
- ptr = ProtobufUtil .appendLengthLength (plen , prefix , ptr );
74
-
78
+ ptr = ProtobufUtil .appendLengthLength (plen , prefixBuf , ptr );
79
+
75
80
// root? Just output it all
81
+ final int writeStart = _parentStart ; // same as `_prefixOffset` or less, if buffered content
76
82
if (_parent == null ) {
77
83
// 04-Apr-2017, tatu: We know that parent will have flushed anything it might have,
78
84
// so `_parentStart` is irrelevant here (but not in the other branch)
79
- out .write (prefix , start , ptr -start );
85
+ out .write (prefixBuf , writeStart , ptr -writeStart );
80
86
for (Segment s = _firstSegment ; s != null ; s = s .next ()) {
81
87
s .writeTo (out );
82
88
}
@@ -86,11 +92,7 @@ public ByteAccumulator finish(OutputStream out,
86
92
} else {
87
93
// 04-Apr-2017, tatu: for [dataformats-binary#67], need to flush possible
88
94
// content parent had...
89
- final int flushLen = _prefixOffset - _parentStart ;
90
- if (flushLen > 0 ) {
91
- _parent .append (input , _parentStart , flushLen );
92
- }
93
- _parent .append (prefix , start , ptr -start );
95
+ _parent .append (prefixBuf , writeStart , ptr -writeStart );
94
96
if (_firstSegment != null ) {
95
97
_parent .appendAll (_firstSegment , _lastSegment , _segmentBytes );
96
98
}
@@ -103,32 +105,28 @@ public ByteAccumulator finish(OutputStream out,
103
105
104
106
public ByteAccumulator finish (OutputStream out , byte [] input ) throws IOException
105
107
{
106
- int start = _prefixOffset ;
107
108
int ptr ;
108
- final byte [] prefix = _prefixBuffer ;
109
+ final byte [] prefixBuf = _prefixBuffer ;
109
110
110
111
if (_typedTag == -1 ) {
111
- ptr = start ;
112
+ ptr = _prefixOffset ;
112
113
} else {
113
- ptr = ProtobufUtil .appendLengthLength (_typedTag , prefix , start );
114
+ ptr = ProtobufUtil .appendLengthLength (_typedTag , prefixBuf , _prefixOffset );
114
115
}
115
116
int plen = _segmentBytes ;
116
- ptr = ProtobufUtil .appendLengthLength (plen , prefix , ptr );
117
+ ptr = ProtobufUtil .appendLengthLength (plen , prefixBuf , ptr );
117
118
119
+ final int writeStart = _parentStart ; // same as `_prefixOffset` or less, if buffered content
118
120
// root? Just output it all
119
121
if (_parent == null ) {
120
122
// 04-Apr-2017, tatu: We know that parent will have flushed anything it might have,
121
123
// so `_parentStart` is irrelevant here (but not in the other branch)
122
- out .write (prefix , start , ptr -start );
124
+ out .write (prefixBuf , writeStart , ptr -writeStart );
123
125
for (Segment s = _firstSegment ; s != null ; s = s .next ()) {
124
126
s .writeTo (out );
125
127
}
126
128
} else {
127
- final int flushLen = _prefixOffset - _parentStart ;
128
- if (flushLen > 0 ) {
129
- _parent .append (input , _parentStart , flushLen );
130
- }
131
- _parent .append (prefix , start , ptr -start );
129
+ _parent .append (prefixBuf , writeStart , ptr -writeStart );
132
130
if (_firstSegment != null ) {
133
131
_parent .appendAll (_firstSegment , _lastSegment , _segmentBytes );
134
132
}
0 commit comments