Skip to content

Commit c079793

Browse files
committed
Avoid fast-path IO writes when IO has ext enc
This works around a bug in JRuby's IOOutputStream logic whereby an IO with an external encoding will always fail to write incoming bytes. We use base logic to detect if the target object is a "real IO" and if so and it has an external encoding, we drop the realIO and. This causes the rest of IOOutputStream to avoid the fast path and always use dyncall logic with RubyString wrappers. This works around the issue in jruby/jruby#8682. This should be temporary, since it will definitely degrade direct writes to such IO objects, but a longer-term fix for the encoding issues spelled out in jruby/jruby#8682 will need to come first, or else json will have to be modified to not use IOOutputStream at all.
1 parent ac30b69 commit c079793

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

java/src/json/ext/Generator.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package json.ext;
77

8+
import org.jcodings.Encoding;
89
import org.jcodings.specific.UTF8Encoding;
910
import org.jruby.Ruby;
1011
import org.jruby.RubyArray;
@@ -15,6 +16,7 @@
1516
import org.jruby.RubyFixnum;
1617
import org.jruby.RubyFloat;
1718
import org.jruby.RubyHash;
19+
import org.jruby.RubyIO;
1820
import org.jruby.RubyString;
1921
import org.jruby.RubySymbol;
2022
import org.jruby.runtime.Helpers;
@@ -81,11 +83,41 @@ static <T extends IRubyObject> RubyString generateJson(ThreadContext context, T
8183
return handler.generateNew(context, session, object);
8284
}
8385

84-
BufferedOutputStream buffer = new BufferedOutputStream(new IOOutputStream(io), IO_BUFFER_SIZE);
86+
BufferedOutputStream buffer =
87+
new BufferedOutputStream(
88+
new PatchedIOOutputStream(io, UTF8Encoding.INSTANCE),
89+
IO_BUFFER_SIZE);
8590
handler.generateToBuffer(context, session, object, buffer);
8691
return io;
8792
}
8893

94+
/**
95+
* A version of IOOutputStream hacked to avoid fast-path RubyIO calls when the target IO has an external encoding.
96+
*
97+
* All calls to the underlying IO will be done dynamically and all incoming bytes wrapped in RubyString instances.
98+
* This avoids bugs in the fast-path logic in JRuby 9.4.12.0 and earlier that fails to properly handle writing bytes
99+
* when the source and target destination are the same.
100+
*
101+
* See https://github.com/jruby/jruby/issues/8682
102+
*/
103+
private static class PatchedIOOutputStream extends IOOutputStream {
104+
public PatchedIOOutputStream(IRubyObject io, Encoding encoding) {
105+
super(io, encoding);
106+
}
107+
108+
@Override
109+
public RubyIO getRealIO(IRubyObject io) {
110+
RubyIO realIO = super.getRealIO(io);
111+
112+
// if the real IO has an external encoding, don't use fast path
113+
if (realIO == null || realIO.getEnc() != null) {
114+
return null;
115+
}
116+
117+
return realIO;
118+
}
119+
}
120+
89121
/**
90122
* Returns the best serialization handler for the given object.
91123
*/

0 commit comments

Comments
 (0)