Skip to content

Commit 4581b1e

Browse files
committed
Plumb OutputStream through generators
When an IO is given, we should try to write directly to it. This patch moves that direction by always doing JSON dumping into an OutputStream, which can be implemented on top of a given IO or by producing a ByteList via a ByteArrayOutputStream.
1 parent cd182ae commit 4581b1e

File tree

7 files changed

+156
-100
lines changed

7 files changed

+156
-100
lines changed

Rakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby'
161161
file JRUBY_PARSER_JAR => :compile do
162162
cd 'java/src' do
163163
parser_classes = FileList[
164-
"json/ext/ByteListTranscoder*.class",
164+
"json/ext/ByteList*.class",
165165
"json/ext/OptionsReader*.class",
166166
"json/ext/Parser*.class",
167167
"json/ext/RuntimeInfo*.class",
@@ -179,7 +179,7 @@ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby'
179179
file JRUBY_GENERATOR_JAR => :compile do
180180
cd 'java/src' do
181181
generator_classes = FileList[
182-
"json/ext/ByteListTranscoder*.class",
182+
"json/ext/ByteList*.class",
183183
"json/ext/OptionsReader*.class",
184184
"json/ext/Generator*.class",
185185
"json/ext/RuntimeInfo*.class",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package json.ext;
2+
3+
import org.jcodings.Encoding;
4+
import org.jruby.util.ByteList;
5+
6+
import java.io.ByteArrayOutputStream;
7+
8+
public class ByteListDirectOutputStream extends ByteArrayOutputStream {
9+
ByteListDirectOutputStream(int size) {
10+
super(size);
11+
}
12+
13+
public ByteList toByteListDirect(Encoding encoding) {
14+
return new ByteList(buf, 0, count, encoding, false);
15+
}
16+
}

java/src/json/ext/ByteListTranscoder.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import org.jruby.runtime.ThreadContext;
1010
import org.jruby.util.ByteList;
1111

12+
import java.io.IOException;
13+
import java.io.OutputStream;
14+
1215
/**
1316
* A class specialized in transcoding a certain String format into another,
1417
* using UTF-8 ByteLists as both input and output.
@@ -23,7 +26,7 @@ abstract class ByteListTranscoder {
2326
/** Position of the next character to read */
2427
protected int pos;
2528

26-
private ByteList out;
29+
private OutputStream out;
2730
/**
2831
* When a character that can be copied straight into the output is found,
2932
* its index is stored on this variable, and copying is delayed until
@@ -37,11 +40,11 @@ protected ByteListTranscoder(ThreadContext context) {
3740
this.context = context;
3841
}
3942

40-
protected void init(ByteList src, ByteList out) {
43+
protected void init(ByteList src, OutputStream out) {
4144
this.init(src, 0, src.length(), out);
4245
}
4346

44-
protected void init(ByteList src, int start, int end, ByteList out) {
47+
protected void init(ByteList src, int start, int end, OutputStream out) {
4548
this.src = src;
4649
this.pos = start;
4750
this.charStart = start;
@@ -142,19 +145,19 @@ protected void quoteStart() {
142145
* recently read character, or {@link #charStart} to quote
143146
* until the character before it.
144147
*/
145-
protected void quoteStop(int endPos) {
148+
protected void quoteStop(int endPos) throws IOException {
146149
if (quoteStart != -1) {
147-
out.append(src, quoteStart, endPos - quoteStart);
150+
out.write(src.bytes(), quoteStart, endPos - quoteStart);
148151
quoteStart = -1;
149152
}
150153
}
151154

152-
protected void append(int b) {
153-
out.append(b);
155+
protected void append(int b) throws IOException {
156+
out.write(b);
154157
}
155158

156-
protected void append(byte[] origin, int start, int length) {
157-
out.append(origin, start, length);
159+
protected void append(byte[] origin, int start, int length) throws IOException {
160+
out.write(origin, start, length);
158161
}
159162

160163

0 commit comments

Comments
 (0)