From b2b1e7b0409289162c3de9398f4361bac816a0f2 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Mon, 1 Mar 2021 21:57:28 -0600 Subject: [PATCH] Be sure to get write IO for fast writes --- .../java/org/jruby/util/IOOutputStream.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/jruby/util/IOOutputStream.java b/core/src/main/java/org/jruby/util/IOOutputStream.java index bf85d2c2e0f..15d8d9ebfb1 100644 --- a/core/src/main/java/org/jruby/util/IOOutputStream.java +++ b/core/src/main/java/org/jruby/util/IOOutputStream.java @@ -67,9 +67,22 @@ public class IOOutputStream extends OutputStream { public IOOutputStream(final IRubyObject io, Encoding encoding, boolean checkAppend, boolean verifyCanWrite) { this.io = io; this.runtime = io.getRuntime(); - this.realIO = ( io instanceof RubyIO && !((RubyIO) io).isClosed() && - ((RubyIO) io).isBuiltin("write") ) ? - ((RubyIO) io) : null; + + // If we can get a real IO from the object, we can do fast writes + RubyIO realIO = null; + if (io instanceof RubyIO) { + RubyIO tmpIO = (RubyIO) io; + if (fastWritable(tmpIO)) { + tmpIO = tmpIO.GetWriteIO(); + + // recheck write IO for IOness + if (tmpIO == io || fastWritable(tmpIO)) { + realIO = tmpIO; + } + } + } + this.realIO = realIO; + if (realIO == null || verifyCanWrite) { final String site; if (io.respondsTo("write")) { @@ -89,7 +102,12 @@ public IOOutputStream(final IRubyObject io, Encoding encoding, boolean checkAppe } this.encoding = encoding; } - + + protected boolean fastWritable(RubyIO io) { + return !io.isClosed() && + io.isBuiltin("write"); + } + public IOOutputStream(final IRubyObject io, boolean checkAppend, boolean verifyCanWrite) { this(io, ASCIIEncoding.INSTANCE, checkAppend, verifyCanWrite); }