1
1
package com .fasterxml .jackson .core .io ;
2
2
3
3
import java .lang .ref .SoftReference ;
4
+ import java .util .Arrays ;
4
5
5
6
import com .fasterxml .jackson .core .util .ByteArrayBuilder ;
6
7
import com .fasterxml .jackson .core .util .TextBuffer ;
@@ -52,12 +53,6 @@ public final class JsonStringEncoder
52
53
/**********************************************************************
53
54
*/
54
55
55
- /**
56
- * Lazily constructed text buffer used to produce JSON encoded Strings
57
- * as characters (without UTF-8 encoding)
58
- */
59
- protected TextBuffer _text ;
60
-
61
56
/**
62
57
* Lazily-constructed builder used for UTF-8 encoding of text values
63
58
* (quoted and unquoted)
@@ -109,12 +104,8 @@ public static JsonStringEncoder getInstance() {
109
104
*/
110
105
public char [] quoteAsString (String input )
111
106
{
112
- TextBuffer textBuffer = _text ;
113
- if (textBuffer == null ) {
114
- // no allocator; can add if we must, shouldn't need to
115
- _text = textBuffer = new TextBuffer (null );
116
- }
117
- char [] outputBuffer = textBuffer .emptyAndGetCurrentSegment ();
107
+ TextBuffer textBuffer = null ;
108
+ char [] outputBuffer = new char [100 ];
118
109
final int [] escCodes = CharTypes .get7BitOutputEscapes ();
119
110
final int escCodeCount = escCodes .length ;
120
111
int inPtr = 0 ;
@@ -130,6 +121,9 @@ public char[] quoteAsString(String input)
130
121
break tight_loop ;
131
122
}
132
123
if (outPtr >= outputBuffer .length ) {
124
+ if (textBuffer == null ) {
125
+ textBuffer = TextBuffer .fromInitial (outputBuffer );
126
+ }
133
127
outputBuffer = textBuffer .finishCurrentSegment ();
134
128
outPtr = 0 ;
135
129
}
@@ -150,6 +144,9 @@ public char[] quoteAsString(String input)
150
144
if (first > 0 ) {
151
145
System .arraycopy (_qbuf , 0 , outputBuffer , outPtr , first );
152
146
}
147
+ if (textBuffer == null ) {
148
+ textBuffer = TextBuffer .fromInitial (outputBuffer );
149
+ }
153
150
outputBuffer = textBuffer .finishCurrentSegment ();
154
151
int second = length - first ;
155
152
System .arraycopy (_qbuf , first , outputBuffer , 0 , second );
@@ -159,6 +156,10 @@ public char[] quoteAsString(String input)
159
156
outPtr += length ;
160
157
}
161
158
}
159
+
160
+ if (textBuffer == null ) {
161
+ return Arrays .copyOfRange (outputBuffer , 0 , outPtr );
162
+ }
162
163
textBuffer .setCurrentLength (outPtr );
163
164
return textBuffer .contentsAsArray ();
164
165
}
@@ -175,11 +176,9 @@ public char[] quoteAsString(CharSequence input)
175
176
return quoteAsString ((String ) input );
176
177
}
177
178
178
- TextBuffer textBuffer = _text ;
179
- if (textBuffer == null ) {
180
- _text = textBuffer = new TextBuffer (null );
181
- }
182
- char [] outputBuffer = textBuffer .emptyAndGetCurrentSegment ();
179
+ TextBuffer textBuffer = null ;
180
+
181
+ char [] outputBuffer = new char [100 ];
183
182
final int [] escCodes = CharTypes .get7BitOutputEscapes ();
184
183
final int escCodeCount = escCodes .length ;
185
184
int inPtr = 0 ;
@@ -195,6 +194,9 @@ public char[] quoteAsString(CharSequence input)
195
194
break tight_loop ;
196
195
}
197
196
if (outPtr >= outputBuffer .length ) {
197
+ if (textBuffer == null ) {
198
+ textBuffer = TextBuffer .fromInitial (outputBuffer );
199
+ }
198
200
outputBuffer = textBuffer .finishCurrentSegment ();
199
201
outPtr = 0 ;
200
202
}
@@ -203,6 +205,7 @@ public char[] quoteAsString(CharSequence input)
203
205
break outer ;
204
206
}
205
207
}
208
+ // something to escape; 2 or 6-char variant?
206
209
char d = input .charAt (inPtr ++);
207
210
int escCode = escCodes [d ];
208
211
int length = (escCode < 0 )
@@ -214,6 +217,9 @@ public char[] quoteAsString(CharSequence input)
214
217
if (first > 0 ) {
215
218
System .arraycopy (_qbuf , 0 , outputBuffer , outPtr , first );
216
219
}
220
+ if (textBuffer == null ) {
221
+ textBuffer = TextBuffer .fromInitial (outputBuffer );
222
+ }
217
223
outputBuffer = textBuffer .finishCurrentSegment ();
218
224
int second = length - first ;
219
225
System .arraycopy (_qbuf , first , outputBuffer , 0 , second );
@@ -223,6 +229,10 @@ public char[] quoteAsString(CharSequence input)
223
229
outPtr += length ;
224
230
}
225
231
}
232
+
233
+ if (textBuffer == null ) {
234
+ return Arrays .copyOfRange (outputBuffer , 0 , outPtr );
235
+ }
226
236
textBuffer .setCurrentLength (outPtr );
227
237
return textBuffer .contentsAsArray ();
228
238
}
0 commit comments