7
7
import com .fasterxml .jackson .core .io .NumberInput ;
8
8
import com .fasterxml .jackson .core .util .ByteArrayBuilder ;
9
9
import com .fasterxml .jackson .databind .SerializerProvider ;
10
+ import com .fasterxml .jackson .databind .exc .InvalidFormatException ;
11
+ import com .fasterxml .jackson .databind .util .ClassUtil ;
10
12
11
13
/**
12
14
* Value node that contains a text value.
@@ -60,93 +62,16 @@ public String textValue() {
60
62
@ SuppressWarnings ("resource" )
61
63
public byte [] getBinaryValue (Base64Variant b64variant ) throws IOException
62
64
{
63
- ByteArrayBuilder builder = new ByteArrayBuilder (100 );
64
- final String str = _value ;
65
- int ptr = 0 ;
66
- int len = str .length ();
67
-
68
- main_loop :
69
- while (ptr < len ) {
70
- // first, we'll skip preceding white space, if any
71
- char ch ;
72
- do {
73
- ch = str .charAt (ptr ++);
74
- if (ptr >= len ) {
75
- break main_loop ;
76
- }
77
- } while (ch <= ' ' );
78
- int bits = b64variant .decodeBase64Char (ch );
79
- if (bits < 0 ) {
80
- _reportInvalidBase64 (b64variant , ch , 0 );
81
- }
82
- int decodedData = bits ;
83
- // then second base64 char; can't get padding yet, nor ws
84
- if (ptr >= len ) {
85
- _reportBase64EOF ();
86
- }
87
- ch = str .charAt (ptr ++);
88
- bits = b64variant .decodeBase64Char (ch );
89
- if (bits < 0 ) {
90
- _reportInvalidBase64 (b64variant , ch , 1 );
91
- }
92
- decodedData = (decodedData << 6 ) | bits ;
93
- // third base64 char; can be padding, but not ws
94
- if (ptr >= len ) {
95
- // but as per [JACKSON-631] can be end-of-input, iff not using padding
96
- if (!b64variant .usesPadding ()) {
97
- // Got 12 bits, only need 8, need to shift
98
- decodedData >>= 4 ;
99
- builder .append (decodedData );
100
- break ;
101
- }
102
- _reportBase64EOF ();
103
- }
104
- ch = str .charAt (ptr ++);
105
- bits = b64variant .decodeBase64Char (ch );
106
-
107
- // First branch: can get padding (-> 1 byte)
108
- if (bits < 0 ) {
109
- if (bits != Base64Variant .BASE64_VALUE_PADDING ) {
110
- _reportInvalidBase64 (b64variant , ch , 2 );
111
- }
112
- // Ok, must get padding
113
- if (ptr >= len ) {
114
- _reportBase64EOF ();
115
- }
116
- ch = str .charAt (ptr ++);
117
- if (!b64variant .usesPaddingChar (ch )) {
118
- _reportInvalidBase64 (b64variant , ch , 3 , "expected padding character '" +b64variant .getPaddingChar ()+"'" );
119
- }
120
- // Got 12 bits, only need 8, need to shift
121
- decodedData >>= 4 ;
122
- builder .append (decodedData );
123
- continue ;
124
- }
125
- // Nope, 2 or 3 bytes
126
- decodedData = (decodedData << 6 ) | bits ;
127
- // fourth and last base64 char; can be padding, but not ws
128
- if (ptr >= len ) {
129
- // but as per [JACKSON-631] can be end-of-input, iff not using padding
130
- if (!b64variant .usesPadding ()) {
131
- decodedData >>= 2 ;
132
- builder .appendTwoBytes (decodedData );
133
- break ;
134
- }
135
- _reportBase64EOF ();
136
- }
137
- ch = str .charAt (ptr ++);
138
- bits = b64variant .decodeBase64Char (ch );
139
- if (bits < 0 ) {
140
- if (bits != Base64Variant .BASE64_VALUE_PADDING ) {
141
- _reportInvalidBase64 (b64variant , ch , 3 );
142
- }
143
- decodedData >>= 2 ;
144
- builder .appendTwoBytes (decodedData );
145
- } else {
146
- // otherwise, our triple is now complete
147
- decodedData = (decodedData << 6 ) | bits ;
148
- builder .appendThreeBytes (decodedData );
149
- }
65
+ final String str = _value .trim ();
66
+ ByteArrayBuilder builder = new ByteArrayBuilder (4 + ((str .length () * 3 ) << 2 ));
67
+ try {
68
+ b64variant .decode (str , builder );
69
+ } catch (IllegalArgumentException e ) {
70
+ throw InvalidFormatException .from (null ,
71
+ String .format (
72
+ "Can not access contents of TextNode as binary due to broken Base64 encoding: %s" ,
73
+ e .getMessage ()),
74
+ str , byte [].class );
150
75
}
151
76
return builder .toByteArray ();
152
77
}
@@ -155,7 +80,7 @@ public byte[] getBinaryValue(Base64Variant b64variant) throws IOException
155
80
public byte [] binaryValue () throws IOException {
156
81
return getBinaryValue (Base64Variants .getDefaultVariant ());
157
82
}
158
-
83
+
159
84
/*
160
85
/**********************************************************
161
86
/* General type coercions
@@ -210,12 +135,12 @@ public double asDouble(double defaultValue) {
210
135
*/
211
136
212
137
@ Override
213
- public final void serialize (JsonGenerator jg , SerializerProvider provider ) throws IOException
138
+ public final void serialize (JsonGenerator g , SerializerProvider provider ) throws IOException
214
139
{
215
140
if (_value == null ) {
216
- jg .writeNull ();
141
+ g .writeNull ();
217
142
} else {
218
- jg .writeString (_value );
143
+ g .writeString (_value );
219
144
}
220
145
}
221
146
@@ -258,44 +183,4 @@ protected static void appendQuoted(StringBuilder sb, String content)
258
183
CharTypes .appendQuoted (sb , content );
259
184
sb .append ('"' );
260
185
}
261
-
262
- /*
263
- /**********************************************************
264
- /* Helper methods
265
- /**********************************************************
266
- */
267
-
268
- protected void _reportInvalidBase64 (Base64Variant b64variant , char ch , int bindex )
269
- throws JsonParseException
270
- {
271
- _reportInvalidBase64 (b64variant , ch , bindex , null );
272
- }
273
-
274
- /**
275
- * @param bindex Relative index within base64 character unit; between 0
276
- * and 3 (as unit has exactly 4 characters)
277
- */
278
- protected void _reportInvalidBase64 (Base64Variant b64variant , char ch , int bindex , String msg )
279
- throws JsonParseException
280
- {
281
- String base ;
282
- if (ch <= ' ' ) {
283
- base = "Illegal white space character (code 0x" +Integer .toHexString (ch )+") as character #" +(bindex +1 )+" of 4-char base64 unit: can only used between units" ;
284
- } else if (b64variant .usesPaddingChar (ch )) {
285
- base = "Unexpected padding character ('" +b64variant .getPaddingChar ()+"') as character #" +(bindex +1 )+" of 4-char base64 unit: padding only legal as 3rd or 4th character" ;
286
- } else if (!Character .isDefined (ch ) || Character .isISOControl (ch )) {
287
- // Not sure if we can really get here... ? (most illegal xml chars are caught at lower level)
288
- base = "Illegal character (code 0x" +Integer .toHexString (ch )+") in base64 content" ;
289
- } else {
290
- base = "Illegal character '" +ch +"' (code 0x" +Integer .toHexString (ch )+") in base64 content" ;
291
- }
292
- if (msg != null ) {
293
- base = base + ": " + msg ;
294
- }
295
- throw new JsonParseException (null , base , JsonLocation .NA );
296
- }
297
-
298
- protected void _reportBase64EOF () throws JsonParseException {
299
- throw new JsonParseException (null , "Unexpected end-of-String when base64 content" );
300
- }
301
186
}
0 commit comments