10
10
11
11
import com .fasterxml .jackson .core .JsonParser ;
12
12
import com .fasterxml .jackson .core .JsonToken ;
13
+
13
14
import com .fasterxml .jackson .databind .JsonNode ;
14
15
import com .fasterxml .jackson .databind .ObjectMapper ;
15
16
import com .fasterxml .jackson .databind .node .BinaryNode ;
17
+
16
18
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 ;
17
22
import com .fasterxml .jackson .dataformat .smile .testutil .ThrottledInputStream ;
18
23
19
24
public class BinaryReadTest extends BaseTestForSmile
@@ -44,65 +49,95 @@ public Bytes3(byte[] b) {
44
49
}
45
50
}
46
51
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 ();
48
61
49
62
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 );
54
72
}
55
73
56
74
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 );
60
83
}
61
84
62
85
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 );
66
95
}
67
96
68
97
// And then one test just to ensure no state corruption occurs
69
98
public void testMultipleBinaryFields () throws Exception
99
+ {
100
+ _testMultipleBinaryFields (MAPPER_7BITS );
101
+ _testMultipleBinaryFields (MAPPER_RAW );
102
+ }
103
+
104
+ public void _testMultipleBinaryFields (ObjectMapper mapper ) throws Exception
70
105
{
71
106
byte [] inputBytes = new byte [900 ];
72
107
for (int i = 0 ; i < inputBytes .length ; ++i ) {
73
108
inputBytes [i ] = (byte ) i ;
74
109
}
75
110
Bytes3 input = new Bytes3 (inputBytes );
76
- byte [] raw = MAPPER .writeValueAsBytes (input );
111
+ byte [] raw = mapper .writeValueAsBytes (input );
77
112
78
- Bytes3 result = MAPPER .readValue (raw , Bytes3 .class );
113
+ Bytes3 result = mapper .readValue (raw , Bytes3 .class );
79
114
Assert .assertArrayEquals (input .bytes1 , result .bytes1 );
80
115
Assert .assertArrayEquals (input .bytes2 , result .bytes2 );
81
116
Assert .assertArrayEquals (input .bytes3 , result .bytes3 );
82
117
}
83
118
84
- public void _testBinary (int size ) throws Exception
119
+ public void _testBinary (ObjectMapper mapper , int size ) throws Exception
85
120
{
86
121
byte [] input = _bytes (size , 0 );
87
122
88
123
// 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 );
91
126
assertNotNull (b2 );
92
127
Assert .assertArrayEquals (input , b2 );
93
128
94
129
// 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 );
97
132
assertNotNull (bytes );
98
133
assertNotNull (bytes .bytes );
99
134
Assert .assertArrayEquals (input , bytes .bytes );
100
135
101
136
// then using incremental access method
102
- raw = MAPPER .writeValueAsBytes (input );
137
+ raw = mapper .writeValueAsBytes (input );
103
138
104
139
InputStream in = new ThrottledInputStream (raw , 3 );
105
- JsonParser p = MAPPER .createParser (in );
140
+ JsonParser p = mapper .createParser (in );
106
141
assertToken (JsonToken .VALUE_EMBEDDED_OBJECT , p .nextToken ());
107
142
ByteArrayOutputStream bout = new ByteArrayOutputStream (input .length / 3 );
108
143
assertEquals (input .length , p .readBinaryValue (bout ));
@@ -115,14 +150,14 @@ public void _testBinary(int size) throws Exception
115
150
116
151
// and finally streaming but skipping
117
152
in = new ThrottledInputStream (raw , 3 );
118
- p = MAPPER .createParser (in );
153
+ p = mapper .createParser (in );
119
154
assertToken (JsonToken .VALUE_EMBEDDED_OBJECT , p .nextToken ());
120
155
assertNull (p .nextToken ());
121
156
p .close ();
122
157
in .close ();
123
158
124
159
// 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 )));
126
161
assertTrue (n .isObject ());
127
162
assertEquals (1 , n .size ());
128
163
@@ -132,24 +167,24 @@ public void _testBinary(int size) throws Exception
132
167
BinaryNode bn = (BinaryNode ) n ;
133
168
Assert .assertArrayEquals (input , bn .binaryValue ());
134
169
135
- _testBinaryInArray (size );
170
+ _testBinaryInArray (mapper , size );
136
171
}
137
172
138
- private void _testBinaryInArray (int size ) throws Exception
173
+ private void _testBinaryInArray (ObjectMapper mapper , int size ) throws Exception
139
174
{
140
175
byte [] b1 = _bytes (size , 1 );
141
176
byte [] b2 = _bytes (size , 7 );
142
- byte [] doc = MAPPER .writeValueAsBytes (new ByteArrays (b1 , b2 ));
177
+ byte [] doc = mapper .writeValueAsBytes (new ByteArrays (b1 , b2 ));
143
178
@ SuppressWarnings ("resource" )
144
- ByteArrays result = MAPPER .readValue (new ThrottledInputStream (doc , 5 ),
179
+ ByteArrays result = mapper .readValue (new ThrottledInputStream (doc , 5 ),
145
180
ByteArrays .class );
146
181
assertNotNull (result .arrays );
147
182
assertEquals (2 , result .arrays .size ());
148
183
Assert .assertArrayEquals (b1 , result .arrays .get (0 ));
149
184
Assert .assertArrayEquals (b2 , result .arrays .get (1 ));
150
185
151
186
// and once more, now as JsonNode
152
- JsonNode n = MAPPER .readTree (doc );
187
+ JsonNode n = mapper .readTree (doc );
153
188
assertTrue (n .isObject ());
154
189
JsonNode n2 = n .get ("arrays" );
155
190
assertTrue (n2 .isArray ());
0 commit comments