1
1
package com .fasterxml .jackson .core .json ;
2
2
3
- import java .io .*;
3
+ import com .fasterxml .jackson .core .*;
4
+ import com .fasterxml .jackson .core .io .CharTypes ;
5
+ import com .fasterxml .jackson .core .io .CharacterEscapes ;
6
+ import com .fasterxml .jackson .core .io .IOContext ;
7
+ import com .fasterxml .jackson .core .io .NumberOutput ;
8
+
9
+ import java .io .IOException ;
10
+ import java .io .InputStream ;
11
+ import java .io .Reader ;
12
+ import java .io .Writer ;
4
13
import java .math .BigDecimal ;
5
14
import java .math .BigInteger ;
6
15
7
- import com .fasterxml .jackson .core .*;
8
- import com .fasterxml .jackson .core .io .*;
9
-
10
16
/**
11
17
* {@link JsonGenerator} that outputs JSON content using a {@link java.io.Writer}
12
18
* which handles character encoding.
@@ -69,6 +75,17 @@ public class WriterBasedJsonGenerator
69
75
*/
70
76
protected char [] _entityBuffer ;
71
77
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
+
72
89
/**
73
90
* When custom escapes are used, this member variable is used
74
91
* internally to hold a reference to currently used escape
@@ -88,6 +105,8 @@ public WriterBasedJsonGenerator(IOContext ctxt, int features,
88
105
_writer = w ;
89
106
_outputBuffer = ctxt .allocConcatBuffer ();
90
107
_outputEnd = _outputBuffer .length ;
108
+ _charBuffer = new char [_outputBuffer .length ];
109
+ _charBufferLength = _charBuffer .length ;
91
110
}
92
111
93
112
/*
@@ -371,6 +390,54 @@ public void writeString(String text) throws IOException
371
390
_outputBuffer [_outputTail ++] = _quoteChar ;
372
391
}
373
392
393
+ @ Override
394
+ public void writeString (Reader reader , int len ) throws IOException {
395
+ _verifyValueWrite (WRITE_STRING );
396
+ if (reader == null ) {
397
+ _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 );
424
+
425
+ //decrease tracker
426
+ toRead -= numRead ;
427
+ }
428
+
429
+ //Add trailing quote
430
+ if ((_outputTail + len ) >= _outputEnd ) {
431
+ _flushBuffer ();
432
+ }
433
+ _outputBuffer [_outputTail ++] = _quoteChar ;
434
+
435
+ if (toRead > 0 && len >= 0 ) {
436
+ _reportError ("Didn't read enough from reader" );
437
+ }
438
+ }
439
+ }
440
+
374
441
@ Override
375
442
public void writeString (char [] text , int offset , int len ) throws IOException
376
443
{
@@ -890,6 +957,7 @@ protected void _releaseBuffers()
890
957
_outputBuffer = null ;
891
958
_ioContext .releaseConcatBuffer (buf );
892
959
}
960
+ _charBuffer = null ;
893
961
}
894
962
895
963
/*
0 commit comments