Skip to content

Commit

Permalink
Support readpartial objects in IO#copy_stream. Fixes #5167.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed May 13, 2018
1 parent a10ae0a commit 30e9fa4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
6 changes: 6 additions & 0 deletions core/src/main/java/org/jruby/RubyIO.java
Expand Up @@ -44,6 +44,7 @@
import org.jcodings.specific.ASCIIEncoding;
import org.jcodings.transcode.EConvFlags;
import org.jruby.api.API;
import org.jruby.exceptions.EOFError;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.JavaSites.IOSites;
import org.jruby.runtime.callsite.CachingCallSite;
Expand Down Expand Up @@ -4300,6 +4301,8 @@ public static IRubyObject copy_stream(ThreadContext context, IRubyObject recv, I
local1 = true;
} else if (sites.respond_to_read.respondsTo(context, arg1, arg1, true)) {
channel1 = new IOChannel.IOReadableByteChannel(arg1);
} else if (sites.respond_to_readpartial.respondsTo(context, arg1, arg1, true)) {
channel1 = new IOChannel.IOReadableByteChannel(arg1, "readpartial");
} else {
throw runtime.newArgumentError("Should be String or IO");
}
Expand Down Expand Up @@ -4378,6 +4381,9 @@ public static IRubyObject copy_stream(ThreadContext context, IRubyObject recv, I
}
}

return context.runtime.newFixnum(size);
} catch (EOFError eof) {
// ignore EOF, reached end of input
return context.runtime.newFixnum(size);
} catch (IOException ioe) {
ioe.printStackTrace();
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/runtime/JavaSites.java
Expand Up @@ -329,6 +329,7 @@ public static class IOSites {
public final RespondToCallSite respond_to_write = new RespondToCallSite("write");
public final CachingCallSite write = new FunctionalCachingCallSite("write");
public final RespondToCallSite respond_to_read = new RespondToCallSite("read");
public final RespondToCallSite respond_to_readpartial = new RespondToCallSite("readpartial");
public final CallSite read = new FunctionalCachingCallSite("read");
public final CallSite to_f = new FunctionalCachingCallSite("to_f");
public final CallSite new_ = new FunctionalCachingCallSite("new");
Expand Down
16 changes: 10 additions & 6 deletions core/src/main/java/org/jruby/util/IOChannel.java
Expand Up @@ -107,12 +107,12 @@ protected int write(CallSite write, ByteBuffer src) throws IOException {
return (int)written.convertToInteger().getLongValue();
}

protected CallSite initReadSite() {
protected CallSite initReadSite(String readMethod) {
// no call site use here since this will only be called once
if(io.respondsTo("read")) {
return MethodIndex.getFunctionalCallSite("read");
if(io.respondsTo(readMethod)) {
return MethodIndex.getFunctionalCallSite(readMethod);
} else {
throw new IllegalArgumentException(io.getMetaClass() + "not coercible to " + getClass().getSimpleName() + ": no `read' method");
throw new IllegalArgumentException(io.getMetaClass() + "not coercible to " + getClass().getSimpleName() + ": no `" + readMethod + "' method");
}
}

Expand All @@ -134,8 +134,12 @@ public static class IOReadableByteChannel extends IOChannel implements ReadableB
private final CallSite read;

public IOReadableByteChannel(final IRubyObject io) {
this(io, "read");
}

public IOReadableByteChannel(final IRubyObject io, final String readMethod) {
super(io);
read = initReadSite();
read = initReadSite(readMethod);
}

public int read(ByteBuffer dst) throws IOException {
Expand Down Expand Up @@ -168,7 +172,7 @@ public static class IOReadableWritableByteChannel extends IOChannel implements R
private final CallSite read;
public IOReadableWritableByteChannel(final IRubyObject io) {
super(io);
read = initReadSite();
read = initReadSite("read");
write = initWriteSite();
}

Expand Down
4 changes: 1 addition & 3 deletions test/mri/excludes/TestIO.rb
Expand Up @@ -6,10 +6,8 @@
exclude :test_copy_stream_no_busy_wait, "expectations of MRI-fast subprocess run/cpu time"
exclude :test_copy_stream_pipe_nonblock, "copy_stream does not block against a nonblocking stream (#2439)"
exclude :test_copy_stream_read_pipe, "needs investigation"
exclude :test_copy_stream_rot13_to_io, "needs investigation"
exclude :test_copy_stream_rot13_to_rot13, "needs investigation"
exclude :test_copy_stream_smaller, "needs investigation"
exclude :test_copy_stream_socket4, "hangs"
exclude :test_copy_stream_socket4, "needs investigation"
exclude :test_copy_stream_socket6, "needs investigation"
exclude :test_copy_stream_socket7, "uses fork"
exclude :test_copy_stream_strio_off, "needs investigation"
Expand Down

0 comments on commit 30e9fa4

Please sign in to comment.