Skip to content

Instantly share code, notes, and snippets.

@headius
Created March 28, 2016 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save headius/a02a91c3c744930f3876 to your computer and use it in GitHub Desktop.
Save headius/a02a91c3c744930f3876 to your computer and use it in GitHub Desktop.
diff --git a/core/src/main/java/org/jruby/RubyIO.java b/core/src/main/java/org/jruby/RubyIO.java
index 4223b7c..5ea9d30 100644
--- a/core/src/main/java/org/jruby/RubyIO.java
+++ b/core/src/main/java/org/jruby/RubyIO.java
@@ -1068,7 +1068,7 @@ public class RubyIO extends RubyObject implements IOEncodable {
// We are creating a new IO object that shares the same
// IOHandler (and fileno).
try {
- return ChannelStream.fdopen(runtime, existingDescriptor, modes);
+ return ChannelStream.fdopen(runtime, existingDescriptor, modes, ecflags);
} catch (InvalidValueException ive) {
throw runtime.newErrnoEINVALError();
}
diff --git a/core/src/main/java/org/jruby/util/io/ChannelStream.java b/core/src/main/java/org/jruby/util/io/ChannelStream.java
index a728627..f7b7de9 100644
--- a/core/src/main/java/org/jruby/util/io/ChannelStream.java
+++ b/core/src/main/java/org/jruby/util/io/ChannelStream.java
@@ -1483,6 +1483,12 @@ public class ChannelStream implements Stream, Finalizable, NonblockWritingStream
return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, modes, true), modes);
}
+ public static Stream fdopen(Ruby runtime, ChannelDescriptor descriptor, ModeFlags modes, int ecflags) throws InvalidValueException {
+ // check these modes before constructing, so we don't finalize the partially-initialized stream
+ descriptor.checkNewModes(modes);
+ return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, modes, true), modes, ecflags);
+ }
+
public static Stream open(Ruby runtime, ChannelDescriptor descriptor, boolean autoclose) {
return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, autoclose), descriptor.getOriginalModes());
}
@@ -1501,6 +1507,24 @@ public class ChannelStream implements Stream, Finalizable, NonblockWritingStream
return stream;
}
+ private static Stream maybeWrapWithLineEndingWrapper(Stream stream, ModeFlags modes, int ecflags) {
+ if (modes.isText()) { // FIXME: Remove this one textmode is part of transcoding.
+ return new CRLFStreamWrapper(stream);
+ }
+
+ if (!Platform.IS_WINDOWS) return stream;
+ if (!(stream.getDescriptor().getChannel() instanceof FileChannel)) return stream;
+
+ // If not binary or we want crlf handling in ecflags, wrap
+ if (!modes.isBinary() ||
+ (ecflags & EncodingUtils.ECONV_UNIVERSAL_NEWLINE_DECORATOR) != 0 ||
+ (ecflags & EncodingUtils.ECONV_CRLF_NEWLINE_DECORATOR) != 0) {
+ return new CRLFStreamWrapper(stream);
+ }
+
+ return stream;
+ }
+
public static Stream fopen(Ruby runtime, String path, ModeFlags modes) throws FileNotFoundException, DirectoryAsFileException, FileExistsException, IOException, InvalidValueException, PipeException, BadDescriptorException {
try {
ChannelDescriptor descriptor = ChannelDescriptor.open(runtime.getCurrentDirectory(), path, modes, runtime.getClassLoader());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment