Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

IO.copy_stream should handle objects that respond to read or write. Fixes #437 #723

Merged
merged 1 commit into from

1 participant

@atambo
Collaborator

No description provided.

@atambo atambo merged commit 55c9e6e into from
@headius headius deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  spec/tags/1.9/ruby/core/io/copy_stream_tags.txt
@@ -7,5 +7,3 @@ fails:IO.copy_stream from a pipe IO to an IO starts writing at the destination I
fails:IO.copy_stream from a pipe IO to an IO leaves the destination IO position at the last write
fails:IO.copy_stream from a pipe IO to an IO copies only length bytes when specified
fails:IO.copy_stream with non-IO Objects calls #readpartial on the source Object if defined
-fails:IO.copy_stream with non-IO Objects calls #read on the source Object
-fails:IO.copy_stream with non-IO Objects calls #write on the destination Object
View
23 src/org/jruby/RubyIO.java
@@ -4233,6 +4233,8 @@ public static IRubyObject copy_stream(ThreadContext context, IRubyObject recv,
RubyIO io1 = null;
RubyIO io2 = null;
+ RubyString read = null;
+
if (args.length >= 3) {
length = args[2].convertToInteger();
if (args.length == 4) {
@@ -4248,6 +4250,12 @@ public static IRubyObject copy_stream(ThreadContext context, IRubyObject recv,
} else if (arg1.respondsTo("to_path")) {
RubyString path = (RubyString) TypeConverter.convertToType19(arg1, runtime.getString(), "to_path");
io1 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[] {path}, Block.NULL_BLOCK);
+ } else if (arg1.respondsTo("read")) {
+ if (length == null) {
+ read = arg1.callMethod(context, "read", runtime.getNil()).convertToString();
+ } else {
+ read = arg1.callMethod(context, "read", length).convertToString();
+ }
} else {
throw runtime.newArgumentError("Should be String or IO");
}
@@ -4259,10 +4267,25 @@ public static IRubyObject copy_stream(ThreadContext context, IRubyObject recv,
} else if (arg2.respondsTo("to_path")) {
RubyString path = (RubyString) TypeConverter.convertToType19(arg2, runtime.getString(), "to_path");
io2 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[] {path, runtime.newString("w")}, Block.NULL_BLOCK);
+ } else if (arg2.respondsTo("write")) {
+ if (read == null) {
+ if (length == null) {
+ read = io1.read(context, runtime.getNil()).convertToString();
+ } else {
+ read = io1.read(context, length).convertToString();
+ }
+ }
+ return arg2.callMethod(context, "write", read);
} else {
throw runtime.newArgumentError("Should be String or IO");
}
+ if (io1 == null) {
+ IRubyObject size = io2.write(context, read);
+ io2.flush();
+ return size;
+ }
+
if (!io1.openFile.isReadable()) throw runtime.newIOError("from IO is not readable");
if (!io2.openFile.isWritable()) throw runtime.newIOError("to IO is not writable");
Something went wrong with that request. Please try again.