Skip to content

Commit

Permalink
If we have actual stdio channels, use them and correct fileno.
Browse files Browse the repository at this point in the history
Relates to #2598.
  • Loading branch information
headius committed Feb 24, 2015
1 parent 1e379ff commit b881f9f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/src/main/java/org/jruby/RubyGlobal.java
Expand Up @@ -64,6 +64,8 @@

import static org.jruby.internal.runtime.GlobalVariable.Scope.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.Channel;
Expand Down Expand Up @@ -283,9 +285,17 @@ private static Channel prepareStdioChannel(Ruby runtime, STDIO stdio, Object str
} else {
switch (stdio) {
case IN:
if (stream instanceof FileInputStream) {
return ((FileInputStream)stream).getChannel();
}

return Channels.newChannel((InputStream)stream);
case OUT:
case ERR:
if (stream instanceof FileOutputStream) {
return ((FileOutputStream)stream).getChannel();
}

return Channels.newChannel((OutputStream)stream);
default: throw new RuntimeException("invalid stdio: " + stdio);
}
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/java/org/jruby/RubyIO.java
Expand Up @@ -182,6 +182,12 @@ public static RubyIO prepStdio(Ruby runtime, InputStream f, Channel c, int fmode
RubyIO io = prepIO(runtime, c, fmode | OpenFile.PREP | EncodingUtils.DEFAULT_TEXTMODE, klass, path);

fptr = io.getOpenFileChecked();

// Use standard stdio filenos if we're using System.in et al.
if (f == System.in) {
fptr.fd().realFileno = 0;
}

prepStdioEcflags(fptr, fmode);
fptr.stdio_file = f;

Expand All @@ -195,6 +201,14 @@ public static RubyIO prepStdio(Ruby runtime, OutputStream f, Channel c, int fmod
RubyIO io = prepIO(runtime, c, fmode | OpenFile.PREP | EncodingUtils.DEFAULT_TEXTMODE, klass, path);

fptr = io.getOpenFileChecked();

// Use standard stdio filenos if we're using System.in et al.
if (f == System.out) {
fptr.fd().realFileno = 1;
} else if (f == System.err) {
fptr.fd().realFileno = 2;
}

prepStdioEcflags(fptr, fmode);
fptr.stdio_file = f;

Expand Down

0 comments on commit b881f9f

Please sign in to comment.