Skip to content

Commit 03daa88

Browse files
committed
Backport Smile-binary-write-then-read test, fix a problem found
1 parent 7229150 commit 03daa88

File tree

3 files changed

+69
-41
lines changed

3 files changed

+69
-41
lines changed

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/seq/SequenceWriterTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fasterxml.jackson.dataformat.cbor.seq;
22

33
import java.io.ByteArrayOutputStream;
4-
import java.io.StringWriter;
54

65
import com.fasterxml.jackson.databind.JsonNode;
76
import com.fasterxml.jackson.databind.MappingIterator;

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileParser.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,26 +2487,20 @@ private final byte[] _finishBinaryRaw() throws IOException
24872487
final int expLen = byteLen;
24882488
final byte[] b = new byte[byteLen];
24892489

2490-
if (_inputPtr >= _inputEnd) {
2491-
if (!_loadMore()) {
2492-
_reportIncompleteBinaryReadRaw(expLen, 0);
2493-
}
2494-
_loadMoreGuaranteed();
2495-
}
24962490
int ptr = 0;
2497-
while (true) {
2491+
while (byteLen > 0) {
2492+
if (_inputPtr >= _inputEnd) {
2493+
if (!_loadMore()) {
2494+
_reportIncompleteBinaryReadRaw(expLen, ptr);
2495+
}
2496+
}
24982497
int toAdd = Math.min(byteLen, _inputEnd - _inputPtr);
24992498
System.arraycopy(_inputBuffer, _inputPtr, b, ptr, toAdd);
25002499
_inputPtr += toAdd;
25012500
ptr += toAdd;
25022501
byteLen -= toAdd;
2503-
if (byteLen <= 0) {
2504-
return b;
2505-
}
2506-
if (!_loadMore()) {
2507-
_reportIncompleteBinaryReadRaw(expLen, ptr);
2508-
}
25092502
}
2503+
return b;
25102504
}
25112505

25122506
// @since 2.12.3

smile/src/test/java/com/fasterxml/jackson/dataformat/smile/mapper/BinaryReadTest.java

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010

1111
import com.fasterxml.jackson.core.JsonParser;
1212
import com.fasterxml.jackson.core.JsonToken;
13+
1314
import com.fasterxml.jackson.databind.JsonNode;
1415
import com.fasterxml.jackson.databind.ObjectMapper;
1516
import com.fasterxml.jackson.databind.node.BinaryNode;
17+
1618
import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile;
19+
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
20+
import com.fasterxml.jackson.dataformat.smile.SmileGenerator;
21+
import com.fasterxml.jackson.dataformat.smile.databind.SmileMapper;
1722
import com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream;
1823

1924
public class BinaryReadTest extends BaseTestForSmile
@@ -44,65 +49,95 @@ public Bytes3(byte[] b) {
4449
}
4550
}
4651

47-
private final ObjectMapper MAPPER = smileMapper();
52+
// Default mapper with default 7-bit escaped binary:
53+
private final ObjectMapper MAPPER_7BITS = smileMapper();
54+
55+
// but also one with "raw" regular binary:
56+
private final ObjectMapper MAPPER_RAW = SmileMapper.builder(
57+
SmileFactory.builder()
58+
.disable(SmileGenerator.Feature.ENCODE_BINARY_AS_7BIT)
59+
.build()
60+
).build();
4861

4962
public void testSmallBinaryValues() throws Exception {
50-
_testBinary(0);
51-
_testBinary(1);
52-
_testBinary(20);
53-
_testBinary(100);
63+
_testSmallBinaryValues(MAPPER_7BITS);
64+
_testSmallBinaryValues(MAPPER_RAW);
65+
}
66+
67+
private void _testSmallBinaryValues(ObjectMapper mapper) throws Exception {
68+
_testBinary(mapper, 0);
69+
_testBinary(mapper, 1);
70+
_testBinary(mapper, 20);
71+
_testBinary(mapper, 100);
5472
}
5573

5674
public void testMediumBinaryValues() throws Exception {
57-
_testBinary(500);
58-
_testBinary(1500);
59-
_testBinary(8900);
75+
_testMediumBinaryValues(MAPPER_7BITS);
76+
_testMediumBinaryValues(MAPPER_RAW);
77+
}
78+
79+
private void _testMediumBinaryValues(ObjectMapper mapper) throws Exception {
80+
_testBinary(mapper, 500);
81+
_testBinary(mapper, 1500);
82+
_testBinary(mapper, 8900);
6083
}
6184

6285
public void testLargeBinaryValues() throws Exception {
63-
_testBinary(99000);
64-
_testBinary(299000);
65-
_testBinary(740000);
86+
_testLargeBinaryValues(MAPPER_7BITS);
87+
_testLargeBinaryValues(MAPPER_RAW);
88+
}
89+
90+
private void _testLargeBinaryValues(ObjectMapper mapper) throws Exception {
91+
_testBinary(mapper, 99000);
92+
_testBinary(mapper, 133001);
93+
_testBinary(mapper, 299003);
94+
_testBinary(mapper, 740000);
6695
}
6796

6897
// And then one test just to ensure no state corruption occurs
6998
public void testMultipleBinaryFields() throws Exception
99+
{
100+
_testMultipleBinaryFields(MAPPER_7BITS);
101+
_testMultipleBinaryFields(MAPPER_RAW);
102+
}
103+
104+
public void _testMultipleBinaryFields(ObjectMapper mapper) throws Exception
70105
{
71106
byte[] inputBytes = new byte[900];
72107
for (int i = 0; i < inputBytes.length; ++i) {
73108
inputBytes[i] = (byte) i;
74109
}
75110
Bytes3 input = new Bytes3(inputBytes);
76-
byte[] raw = MAPPER.writeValueAsBytes(input);
111+
byte[] raw = mapper.writeValueAsBytes(input);
77112

78-
Bytes3 result = MAPPER.readValue(raw, Bytes3.class);
113+
Bytes3 result = mapper.readValue(raw, Bytes3.class);
79114
Assert.assertArrayEquals(input.bytes1, result.bytes1);
80115
Assert.assertArrayEquals(input.bytes2, result.bytes2);
81116
Assert.assertArrayEquals(input.bytes3, result.bytes3);
82117
}
83118

84-
public void _testBinary(int size) throws Exception
119+
public void _testBinary(ObjectMapper mapper, int size) throws Exception
85120
{
86121
byte[] input = _bytes(size, 0);
87122

88123
// First, read/write as individual value
89-
byte[] raw = MAPPER.writeValueAsBytes(input);
90-
byte[] b2 = MAPPER.readValue(raw, byte[].class);
124+
byte[] raw = mapper.writeValueAsBytes(input);
125+
byte[] b2 = mapper.readValue(raw, byte[].class);
91126
assertNotNull(b2);
92127
Assert.assertArrayEquals(input, b2);
93128

94129
// then as POJO member
95-
raw = MAPPER.writeValueAsBytes(new Bytes(input));
96-
Bytes bytes = MAPPER.readValue(raw, Bytes.class);
130+
raw = mapper.writeValueAsBytes(new Bytes(input));
131+
Bytes bytes = mapper.readValue(raw, Bytes.class);
97132
assertNotNull(bytes);
98133
assertNotNull(bytes.bytes);
99134
Assert.assertArrayEquals(input, bytes.bytes);
100135

101136
// then using incremental access method
102-
raw = MAPPER.writeValueAsBytes(input);
137+
raw = mapper.writeValueAsBytes(input);
103138

104139
InputStream in = new ThrottledInputStream(raw, 3);
105-
JsonParser p = MAPPER.createParser(in);
140+
JsonParser p = mapper.createParser(in);
106141
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
107142
ByteArrayOutputStream bout = new ByteArrayOutputStream(input.length / 3);
108143
assertEquals(input.length, p.readBinaryValue(bout));
@@ -115,14 +150,14 @@ public void _testBinary(int size) throws Exception
115150

116151
// and finally streaming but skipping
117152
in = new ThrottledInputStream(raw, 3);
118-
p = MAPPER.createParser(in);
153+
p = mapper.createParser(in);
119154
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
120155
assertNull(p.nextToken());
121156
p.close();
122157
in.close();
123158

124159
// And once more! Read as tree
125-
JsonNode n = MAPPER.readTree(MAPPER.writeValueAsBytes(new Bytes(input)));
160+
JsonNode n = mapper.readTree(mapper.writeValueAsBytes(new Bytes(input)));
126161
assertTrue(n.isObject());
127162
assertEquals(1, n.size());
128163

@@ -132,24 +167,24 @@ public void _testBinary(int size) throws Exception
132167
BinaryNode bn = (BinaryNode) n;
133168
Assert.assertArrayEquals(input, bn.binaryValue());
134169

135-
_testBinaryInArray(size);
170+
_testBinaryInArray(mapper, size);
136171
}
137172

138-
private void _testBinaryInArray(int size) throws Exception
173+
private void _testBinaryInArray(ObjectMapper mapper, int size) throws Exception
139174
{
140175
byte[] b1 = _bytes(size, 1);
141176
byte[] b2 = _bytes(size, 7);
142-
byte[] doc = MAPPER.writeValueAsBytes(new ByteArrays(b1, b2));
177+
byte[] doc = mapper.writeValueAsBytes(new ByteArrays(b1, b2));
143178
@SuppressWarnings("resource")
144-
ByteArrays result = MAPPER.readValue(new ThrottledInputStream(doc, 5),
179+
ByteArrays result = mapper.readValue(new ThrottledInputStream(doc, 5),
145180
ByteArrays.class);
146181
assertNotNull(result.arrays);
147182
assertEquals(2, result.arrays.size());
148183
Assert.assertArrayEquals(b1, result.arrays.get(0));
149184
Assert.assertArrayEquals(b2, result.arrays.get(1));
150185

151186
// and once more, now as JsonNode
152-
JsonNode n = MAPPER.readTree(doc);
187+
JsonNode n = mapper.readTree(doc);
153188
assertTrue(n.isObject());
154189
JsonNode n2 = n.get("arrays");
155190
assertTrue(n2.isArray());

0 commit comments

Comments
 (0)