Index: src/org/jruby/RubyIO.java =================================================================== --- src/org/jruby/RubyIO.java (revision 2193) +++ src/org/jruby/RubyIO.java (arbetskopia) @@ -859,11 +859,13 @@ return getRuntime().newFixnum(0); } + private final static String LS = System.getProperty("line.separator"); + public IRubyObject puts(IRubyObject[] args) { checkArgumentCount(args, 0, -1); if (args.length == 0) { - callMethod("write", getRuntime().newString("\n")); + callMethod("write", getRuntime().newString(handler.getNewline())); return getRuntime().getNil(); } @@ -878,7 +880,7 @@ line = args[i].toString(); } callMethod("write", getRuntime().newString(line+ - (line.endsWith("\n") ? "" : "\n"))); + (line.endsWith("\n") ? "" : handler.getNewline()))); } return getRuntime().getNil(); } Index: src/org/jruby/util/IOHandlerSeekable.java =================================================================== --- src/org/jruby/util/IOHandlerSeekable.java (revision 2193) +++ src/org/jruby/util/IOHandlerSeekable.java (arbetskopia) @@ -52,6 +52,11 @@ protected RandomAccessFile file; protected String path; + private boolean shouldReplace = false; + private boolean isWin = true; + + private String fileNewline; + public IOHandlerSeekable(IRuby runtime, String path, IOModes modes) throws IOException, InvalidValueException { super(runtime); @@ -88,11 +93,23 @@ if (modes.isAppendable()) { seek(0, SEEK_END); } + final String ls = System.getProperty("line.separator"); + if(!modes.isBinary() && !"\n".equals(ls)) { + shouldReplace = true; + if("\r".equals(ls)) { + isWin = false; + } + } + fileNewline = ls; // We give a fileno last so that we do not consume these when // we have a problem opening a file. fileno = RubyIO.getNewFileno(); } + + public String getNewline() { + return fileNewline; + } public IOHandler cloneIOHandler() throws IOException, PipeException, InvalidValueException { IOHandler newHandler = new IOHandlerSeekable(getRuntime(), path, modes); @@ -233,7 +250,24 @@ * @see org.jruby.util.IOHandler#sysread() */ public int sysread() throws IOException { - return file.read(); + if(!shouldReplace) { + return file.read(); + } else { + final int curr = file.read(); + if(curr != '\r') { + return curr; + } else if(!isWin) { + return '\n'; + } else { + final int next = file.read(); + if(next == '\n') { + return next; + } else { + file.seek(file.getFilePointer()-1); + return curr; + } + } + } } /** Index: src/org/jruby/util/IOHandler.java =================================================================== --- src/org/jruby/util/IOHandler.java (revision 2193) +++ src/org/jruby/util/IOHandler.java (arbetskopia) @@ -201,6 +201,10 @@ */ public abstract void seek(long offset, int type) throws IOException, PipeException, InvalidValueException; public abstract void truncate(long newLength) throws IOException, PipeException; + + public String getNewline() { + return "\n"; + } public class PipeException extends Exception { private static final long serialVersionUID = 1L;