Skip to content

Commit 0711081

Browse files
authored
Merge pull request #353 from uhhhh2/master
Alternate approach for issue #17
2 parents 50a1450 + 4ba30eb commit 0711081

File tree

7 files changed

+557
-18
lines changed

7 files changed

+557
-18
lines changed

src/main/java/com/fasterxml/jackson/core/JsonGenerator.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55
package com.fasterxml.jackson.core;
66

7-
import static com.fasterxml.jackson.core.JsonTokenId.*;
7+
import com.fasterxml.jackson.core.JsonParser.NumberType;
8+
import com.fasterxml.jackson.core.io.CharacterEscapes;
9+
import com.fasterxml.jackson.core.util.VersionUtil;
810

911
import java.io.*;
1012
import java.math.BigDecimal;
@@ -13,9 +15,7 @@
1315
import java.util.concurrent.atomic.AtomicInteger;
1416
import java.util.concurrent.atomic.AtomicLong;
1517

16-
import com.fasterxml.jackson.core.JsonParser.NumberType;
17-
import com.fasterxml.jackson.core.io.CharacterEscapes;
18-
import com.fasterxml.jackson.core.util.VersionUtil;
18+
import static com.fasterxml.jackson.core.JsonTokenId.*;
1919

2020
/**
2121
* Base class that defines public API for writing JSON content.
@@ -932,6 +932,18 @@ public void writeArray(double[] array, int offset, int length) throws IOExceptio
932932
*/
933933
public abstract void writeString(String text) throws IOException;
934934

935+
/**
936+
* Method for outputting a String value. Depending on context
937+
* this means either array element, (object) field value or
938+
* a stand alone String; but in all cases, String will be
939+
* surrounded in double quotes, and contents will be properly
940+
* escaped as required by JSON specification.
941+
* If the reader is null, then write a null.
942+
* If len is < 0, then write all contents of the reader.
943+
* Otherwise, write only len characters.
944+
*/
945+
public abstract void writeString(Reader reader, int len) throws IOException;
946+
935947
/**
936948
* Method for outputting a String value. Depending on context
937949
* this means either array element, (object) field value or

src/main/java/com/fasterxml/jackson/core/base/GeneratorBase.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.fasterxml.jackson.core.base;
22

3-
import java.io.*;
4-
import java.math.BigDecimal;
5-
63
import com.fasterxml.jackson.core.*;
74
import com.fasterxml.jackson.core.json.DupDetector;
85
import com.fasterxml.jackson.core.json.JsonWriteContext;
96
import com.fasterxml.jackson.core.json.PackageVersion;
107
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
118

9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.Reader;
12+
import java.math.BigDecimal;
13+
1214
/**
1315
* This base class implements part of API that a JSON generator exposes
1416
* to applications, adds shared internal methods that sub-classes
@@ -339,6 +341,12 @@ public int writeBinary(Base64Variant b64variant, InputStream data, int dataLengt
339341
return 0;
340342
}
341343

344+
@Override
345+
public void writeString(Reader reader, int len) throws IOException {
346+
// Let's implement this as "unsupported" to make it easier to add new parser impls
347+
_reportUnsupportedOperation();
348+
}
349+
342350
/*
343351
/**********************************************************
344352
/* Public API, write methods, primitive

src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java

+58-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.fasterxml.jackson.core.json;
22

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.OutputStream;
12+
import java.io.Reader;
413
import java.math.BigDecimal;
514
import java.math.BigInteger;
615

7-
import com.fasterxml.jackson.core.*;
8-
import com.fasterxml.jackson.core.io.*;
9-
1016
public class UTF8JsonGenerator
1117
extends JsonGeneratorImpl
1218
{
@@ -461,6 +467,54 @@ public void writeString(String text) throws IOException
461467
_outputBuffer[_outputTail++] = _quoteChar;
462468
}
463469

470+
@Override
471+
public void writeString(Reader reader, int len) throws IOException {
472+
_verifyValueWrite(WRITE_STRING);
473+
if (reader == null) {
474+
_reportError("null reader");
475+
}
476+
477+
int toRead = (len >= 0) ? len : Integer.MAX_VALUE;
478+
479+
//TODO: Check that this use is OK
480+
final char[] buf = _charBuffer;
481+
482+
//Add leading quote
483+
if ((_outputTail + len) >= _outputEnd) {
484+
_flushBuffer();
485+
}
486+
_outputBuffer[_outputTail++] = _quoteChar;
487+
488+
//read
489+
while(toRead > 0){
490+
int toReadNow = Math.min(toRead, buf.length);
491+
if(toRead <= 0){
492+
break;
493+
}
494+
int numRead = reader.read(buf, 0, toReadNow);
495+
if(numRead <= 0){
496+
break;
497+
}
498+
if ((_outputTail + len) >= _outputEnd) {
499+
_flushBuffer();
500+
}
501+
_writeStringSegments(buf, 0, numRead);
502+
503+
//decrease tracker
504+
toRead -= numRead;
505+
}
506+
507+
//Add trailing quote
508+
if ((_outputTail + len) >= _outputEnd) {
509+
_flushBuffer();
510+
}
511+
_outputBuffer[_outputTail++] = _quoteChar;
512+
513+
if(toRead > 0 && len >= 0){
514+
_reportError("Didn't read enough from reader");
515+
}
516+
}
517+
464518
@Override
465519
public void writeString(char[] text, int offset, int len) throws IOException
466520
{

src/main/java/com/fasterxml/jackson/core/json/WriterBasedJsonGenerator.java

+72-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.fasterxml.jackson.core.json;
22

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;
413
import java.math.BigDecimal;
514
import java.math.BigInteger;
615

7-
import com.fasterxml.jackson.core.*;
8-
import com.fasterxml.jackson.core.io.*;
9-
1016
/**
1117
* {@link JsonGenerator} that outputs JSON content using a {@link java.io.Writer}
1218
* which handles character encoding.
@@ -69,6 +75,17 @@ public class WriterBasedJsonGenerator
6975
*/
7076
protected char[] _entityBuffer;
7177

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+
7289
/**
7390
* When custom escapes are used, this member variable is used
7491
* internally to hold a reference to currently used escape
@@ -88,6 +105,8 @@ public WriterBasedJsonGenerator(IOContext ctxt, int features,
88105
_writer = w;
89106
_outputBuffer = ctxt.allocConcatBuffer();
90107
_outputEnd = _outputBuffer.length;
108+
_charBuffer = new char[_outputBuffer.length];
109+
_charBufferLength = _charBuffer.length;
91110
}
92111

93112
/*
@@ -371,6 +390,54 @@ public void writeString(String text) throws IOException
371390
_outputBuffer[_outputTail++] = _quoteChar;
372391
}
373392

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+
374441
@Override
375442
public void writeString(char[] text, int offset, int len) throws IOException
376443
{
@@ -890,6 +957,7 @@ protected void _releaseBuffers()
890957
_outputBuffer = null;
891958
_ioContext.releaseConcatBuffer(buf);
892959
}
960+
_charBuffer = null;
893961
}
894962

895963
/*

src/main/java/com/fasterxml/jackson/core/util/JsonGeneratorDelegate.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.fasterxml.jackson.core.util;
22

3+
import com.fasterxml.jackson.core.*;
4+
import com.fasterxml.jackson.core.io.CharacterEscapes;
5+
36
import java.io.IOException;
47
import java.io.InputStream;
8+
import java.io.Reader;
59
import java.math.BigDecimal;
610
import java.math.BigInteger;
711

8-
import com.fasterxml.jackson.core.*;
9-
import com.fasterxml.jackson.core.io.CharacterEscapes;
10-
1112
public class JsonGeneratorDelegate extends JsonGenerator
1213
{
1314
/**
@@ -246,6 +247,11 @@ public void writeArray(double[] array, int offset, int length) throws IOExceptio
246247
@Override
247248
public void writeString(String text) throws IOException { delegate.writeString(text); }
248249

250+
@Override
251+
public void writeString(Reader reader, int len) throws IOException {
252+
delegate.writeString(reader, len);
253+
}
254+
249255
@Override
250256
public void writeString(char[] text, int offset, int len) throws IOException { delegate.writeString(text, offset, len); }
251257

0 commit comments

Comments
 (0)