Skip to content

Commit

Permalink
Be sure to get write IO for fast writes
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Mar 2, 2021
1 parent 659d6db commit b2b1e7b
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions core/src/main/java/org/jruby/util/IOOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand All @@ -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);
}
Expand Down

0 comments on commit b2b1e7b

Please sign in to comment.