Skip to content

Commit 12f3df5

Browse files
committed
minor cleanup
1 parent 2d20a08 commit 12f3df5

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ByteAccumulator.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@ public class ByteAccumulator
1919
protected final byte[] _prefixBuffer;
2020

2121
/**
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).
2324
*/
2425
protected final int _prefixOffset;
25-
26+
2627
protected final int _typedTag;
2728

2829
protected Segment _firstSegment, _lastSegment;
2930

31+
/**
32+
* Total number of bytes contained within buffers, to be used for length prefix.
33+
*/
3034
protected int _segmentBytes;
3135

3236
/**
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`)
3439
*
3540
* @since 2.8.8
3641
*/
@@ -59,24 +64,25 @@ public void append(byte[] buf, int offset, int len) {
5964
public ByteAccumulator finish(OutputStream out,
6065
byte[] input, int offset, int len) throws IOException
6166
{
62-
final byte[] prefix = _prefixBuffer;
63-
int start = _prefixOffset;
67+
final byte[] prefixBuf = _prefixBuffer;
6468
int ptr;
6569

70+
// First: encode full tag to use, now that we length to calculate prefix from
6671
if (_typedTag == -1) {
67-
ptr = start;
72+
ptr = _prefixOffset;
6873
} else {
69-
ptr = ProtobufUtil.appendLengthLength(_typedTag, prefix, start);
74+
ptr = ProtobufUtil.appendLengthLength(_typedTag, prefixBuf, _prefixOffset);
7075
}
7176

7277
int plen = _segmentBytes + len;
73-
ptr = ProtobufUtil.appendLengthLength(plen, prefix, ptr);
74-
78+
ptr = ProtobufUtil.appendLengthLength(plen, prefixBuf, ptr);
79+
7580
// root? Just output it all
81+
final int writeStart = _parentStart; // same as `_prefixOffset` or less, if buffered content
7682
if (_parent == null) {
7783
// 04-Apr-2017, tatu: We know that parent will have flushed anything it might have,
7884
// 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);
8086
for (Segment s = _firstSegment; s != null; s = s.next()) {
8187
s.writeTo(out);
8288
}
@@ -86,11 +92,7 @@ public ByteAccumulator finish(OutputStream out,
8692
} else {
8793
// 04-Apr-2017, tatu: for [dataformats-binary#67], need to flush possible
8894
// 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);
9496
if (_firstSegment != null) {
9597
_parent.appendAll(_firstSegment, _lastSegment, _segmentBytes);
9698
}
@@ -103,32 +105,28 @@ public ByteAccumulator finish(OutputStream out,
103105

104106
public ByteAccumulator finish(OutputStream out, byte[] input) throws IOException
105107
{
106-
int start = _prefixOffset;
107108
int ptr;
108-
final byte[] prefix = _prefixBuffer;
109+
final byte[] prefixBuf = _prefixBuffer;
109110

110111
if (_typedTag == -1) {
111-
ptr = start;
112+
ptr = _prefixOffset;
112113
} else {
113-
ptr = ProtobufUtil.appendLengthLength(_typedTag, prefix, start);
114+
ptr = ProtobufUtil.appendLengthLength(_typedTag, prefixBuf, _prefixOffset);
114115
}
115116
int plen = _segmentBytes;
116-
ptr = ProtobufUtil.appendLengthLength(plen, prefix, ptr);
117+
ptr = ProtobufUtil.appendLengthLength(plen, prefixBuf, ptr);
117118

119+
final int writeStart = _parentStart; // same as `_prefixOffset` or less, if buffered content
118120
// root? Just output it all
119121
if (_parent == null) {
120122
// 04-Apr-2017, tatu: We know that parent will have flushed anything it might have,
121123
// 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);
123125
for (Segment s = _firstSegment; s != null; s = s.next()) {
124126
s.writeTo(out);
125127
}
126128
} 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);
132130
if (_firstSegment != null) {
133131
_parent.appendAll(_firstSegment, _lastSegment, _segmentBytes);
134132
}

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufGenerator.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,17 +1699,15 @@ private final void _startBuffering(int typedTag) throws IOException
16991699
_ensureRoom(20);
17001700
// and leave the gap of 10 bytes
17011701
int ptr = _currPtr;
1702+
int start = _currStart;
17021703

17031704
// root level content to flush first?
17041705
if (_buffered == null) {
1705-
int start = _currStart;
17061706
int len = ptr - start;
17071707
if (len > 0) {
1708-
ptr = 0;
1708+
ptr = start = 0;
17091709
_output.write(_currBuffer, start, len);
17101710
}
1711-
// note: no need to (re)set _currStart since nothing is buffered, so whatever
1712-
// is passed to child accumulator will NOT be used by or for anything
17131711
}
17141712
_buffered = new ByteAccumulator(_buffered, typedTag, _currBuffer, ptr, _currStart);
17151713
ptr += 10;
@@ -1742,7 +1740,7 @@ private final void _startBuffering() throws IOException
17421740
_output.write(_currBuffer, _currStart, len);
17431741
}
17441742
}
1745-
*/
1743+
*/
17461744
_buffered = new ByteAccumulator(_buffered, -1, _currBuffer, ptr, _currStart);
17471745
ptr += 5;
17481746
_currStart = ptr;

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/NestedWrite67Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void testIssue67() throws Exception
5858

5959
byte[] bytes = MAPPER.writer(schema).writeValueAsBytes(level1);
6060

61-
showBytes(bytes);
61+
// showBytes(bytes);
6262

6363
Level1 gotLevel1 = MAPPER.readerFor(Level1.class).with(schema).readValue(bytes);
6464

@@ -75,12 +75,12 @@ public void testIssue67() throws Exception
7575
assertEquals(level3s.length, gotLevel1.level2.level3s.length);
7676
assertEquals(level3a.value3, gotLevel1.level2.level3s[0].value3);
7777
}
78-
78+
/*
7979
private void showBytes(byte[] bytes) {
8080
for (byte b : bytes) {
8181
System.out.print(String.format("%8s", Integer.toHexString(b)).substring(6, 8).replaceAll(" ", "0") + " ");
8282
}
8383
System.out.println();
8484
}
85-
85+
*/
8686
}

0 commit comments

Comments
 (0)