1
1
package com .fasterxml .jackson .core .json ;
2
2
3
+ import java .io .*;
4
+ import java .math .BigDecimal ;
5
+ import java .math .BigInteger ;
6
+
3
7
import com .fasterxml .jackson .core .*;
4
8
import com .fasterxml .jackson .core .io .CharTypes ;
5
9
import com .fasterxml .jackson .core .io .CharacterEscapes ;
6
10
import com .fasterxml .jackson .core .io .IOContext ;
7
11
import com .fasterxml .jackson .core .io .NumberOutput ;
8
12
9
- import java .io .IOException ;
10
- import java .io .InputStream ;
11
- import java .io .Reader ;
12
- import java .io .Writer ;
13
- import java .math .BigDecimal ;
14
- import java .math .BigInteger ;
15
-
16
13
/**
17
14
* {@link JsonGenerator} that outputs JSON content using a {@link java.io.Writer}
18
15
* which handles character encoding.
@@ -75,23 +72,20 @@ public class WriterBasedJsonGenerator
75
72
*/
76
73
protected char [] _entityBuffer ;
77
74
78
- /**
79
- * Intermediate buffer in which characters of a String are copied
80
- * before being encoded.
81
- */
82
- protected char [] _charBuffer ;
83
-
84
- /**
85
- * Length of <code>_charBuffer</code>
86
- */
87
- protected final int _charBufferLength ;
88
-
89
75
/**
90
76
* When custom escapes are used, this member variable is used
91
77
* internally to hold a reference to currently used escape
92
78
*/
93
79
protected SerializableString _currentEscape ;
94
80
81
+ /**
82
+ * Intermediate buffer in which characters of a String are copied
83
+ * before being encoded.
84
+ *
85
+ * @since 2.9
86
+ */
87
+ protected char [] _charBuffer ;
88
+
95
89
/*
96
90
/**********************************************************
97
91
/* Life-cycle
@@ -105,16 +99,14 @@ public WriterBasedJsonGenerator(IOContext ctxt, int features,
105
99
_writer = w ;
106
100
_outputBuffer = ctxt .allocConcatBuffer ();
107
101
_outputEnd = _outputBuffer .length ;
108
- _charBuffer = new char [_outputBuffer .length ];
109
- _charBufferLength = _charBuffer .length ;
110
102
}
111
-
103
+
112
104
/*
113
105
/**********************************************************
114
106
/* Overridden configuration, introspection methods
115
107
/**********************************************************
116
108
*/
117
-
109
+
118
110
@ Override
119
111
public Object getOutputTarget () {
120
112
return _writer ;
@@ -395,46 +387,38 @@ public void writeString(Reader reader, int len) throws IOException {
395
387
_verifyValueWrite (WRITE_STRING );
396
388
if (reader == null ) {
397
389
_reportError ("null reader" );
398
- } else {
399
- int toRead = (len >= 0 ) ? len : Integer .MAX_VALUE ;
400
-
401
- //TODO: Check that this use is OK
402
- final char [] buf = _charBuffer ;
403
-
404
- //Add leading quote
405
- if ((_outputTail + len ) >= _outputEnd ) {
406
- _flushBuffer ();
407
- }
408
- _outputBuffer [_outputTail ++] = _quoteChar ;
409
-
410
- //read
411
- while (toRead > 0 ) {
412
- int toReadNow = Math .min (toRead , buf .length );
413
- if (toRead <= 0 ) {
414
- break ;
415
- }
416
- int numRead = reader .read (buf , 0 , toReadNow );
417
- if (numRead <= 0 ) {
418
- break ;
419
- }
420
- if ((_outputTail + len ) >= _outputEnd ) {
421
- _flushBuffer ();
422
- }
423
- _writeString (buf , 0 , numRead );
390
+ }
391
+ int toRead = (len >= 0 ) ? len : Integer .MAX_VALUE ;
392
+ final char [] buf = _allocateCopyBuffer ();
393
+ //Add leading quote
394
+ if ((_outputTail + len ) >= _outputEnd ) {
395
+ _flushBuffer ();
396
+ }
397
+ _outputBuffer [_outputTail ++] = _quoteChar ;
424
398
425
- //decrease tracker
426
- toRead -= numRead ;
399
+ //read
400
+ while (toRead > 0 ) {
401
+ int toReadNow = Math .min (toRead , buf .length );
402
+ int numRead = reader .read (buf , 0 , toReadNow );
403
+ if (numRead <= 0 ) {
404
+ break ;
427
405
}
428
-
429
- //Add trailing quote
430
406
if ((_outputTail + len ) >= _outputEnd ) {
431
407
_flushBuffer ();
432
408
}
433
- _outputBuffer [ _outputTail ++] = _quoteChar ;
409
+ _writeString ( buf , 0 , numRead ) ;
434
410
435
- if (toRead > 0 && len >= 0 ) {
436
- _reportError ("Didn't read enough from reader" );
437
- }
411
+ //decrease tracker
412
+ toRead -= numRead ;
413
+ }
414
+ //Add trailing quote
415
+ if ((_outputTail + len ) >= _outputEnd ) {
416
+ _flushBuffer ();
417
+ }
418
+ _outputBuffer [_outputTail ++] = _quoteChar ;
419
+
420
+ if (toRead > 0 && len >= 0 ) {
421
+ _reportError ("Didn't read enough from reader" );
438
422
}
439
423
}
440
424
@@ -957,7 +941,11 @@ protected void _releaseBuffers()
957
941
_outputBuffer = null ;
958
942
_ioContext .releaseConcatBuffer (buf );
959
943
}
960
- _charBuffer = null ;
944
+ buf = _charBuffer ;
945
+ if (buf != null ) {
946
+ _charBuffer = null ;
947
+ _ioContext .releaseNameCopyBuffer (buf );
948
+ }
961
949
}
962
950
963
951
/*
@@ -1938,7 +1926,17 @@ private char[] _allocateEntityBuffer()
1938
1926
_entityBuffer = buf ;
1939
1927
return buf ;
1940
1928
}
1941
-
1929
+
1930
+ /**
1931
+ * @since 2.9
1932
+ */
1933
+ private char [] _allocateCopyBuffer () {
1934
+ if (_charBuffer == null ) {
1935
+ _charBuffer = _ioContext .allocNameCopyBuffer (2000 );
1936
+ }
1937
+ return _charBuffer ;
1938
+ }
1939
+
1942
1940
protected void _flushBuffer () throws IOException
1943
1941
{
1944
1942
int len = _outputTail - _outputHead ;
0 commit comments