Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9a3fe9c8f5d7
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c22987adcf0d
Choose a head ref
  • 4 commits
  • 6 files changed
  • 1 contributor

Commits on Aug 27, 2017

  1. Copy the full SHA
    3f46242 View commit details
  2. Copy the full SHA
    1100f22 View commit details
  3. Copy the full SHA
    71e4f85 View commit details
  4. Copy the full SHA
    c22987a View commit details
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
@@ -1396,7 +1396,7 @@ public static RubyString get_path(ThreadContext context, IRubyObject path) {
// FIXME: MRI skips this logic on windows? Does not make sense to me why so I left it in.
// mri: file_path_convert
private static RubyString filePathConvert(ThreadContext context, RubyString path) {
if (!org.jruby.platform.Platform.IS_WINDOWS) {
if (!Platform.IS_WINDOWS) {
Ruby runtime = context.getRuntime();
EncodingService encodingService = runtime.getEncodingService();
Encoding pathEncoding = path.getEncoding();
70 changes: 40 additions & 30 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
@@ -2481,7 +2481,7 @@ private static IRubyObject writeSeparator(ThreadContext context, IRubyObject may
RubyString separator = (RubyString) runtime.getGlobalVariables().getDefaultSeparator();

write(context, maybeIO, separator);
return runtime.getNil();
return context.nil;
}

private static IRubyObject putsArray(ThreadContext context, IRubyObject maybeIO, IRubyObject[] args) {
@@ -2493,7 +2493,7 @@ private static IRubyObject putsArray(ThreadContext context, IRubyObject maybeIO,
putsSingle(context, runtime, maybeIO, args[i], separator);
}

return runtime.getNil();
return context.nil;
}

private static final ByteList RECURSIVE_BYTELIST = ByteList.create("[...]");
@@ -2530,7 +2530,7 @@ private static void putsSingle(ThreadContext context, Ruby runtime, IRubyObject
private static IRubyObject inspectPuts(ThreadContext context, IRubyObject maybeIO, RubyArray array) {
try {
context.runtime.registerInspecting(array);
return putsArray(context, maybeIO, array.toJavaArray());
return putsArray(context, maybeIO, array.toJavaArrayMaybeUnsafe());
} finally {
context.runtime.unregisterInspecting(array);
}
@@ -2551,8 +2551,7 @@ public static IRubyObject write(ThreadContext context, IRubyObject maybeIO, IRub
@JRubyMethod
@Override
public IRubyObject inspect() {
Ruby runtime = getRuntime();

final OpenFile openFile = this.openFile;
if (openFile == null) return super.inspect();

String className = getMetaClass().getRealClass().getName();
@@ -2570,9 +2569,7 @@ public IRubyObject inspect() {
status = " (closed)";
}

String inspectStr = "#<" + className + ":" + path + status + ">";

return runtime.newString(inspectStr);
return getRuntime().newString("#<" + className + ':' + path + status + '>');
}

/** Read a line.
@@ -2610,11 +2607,10 @@ public IRubyObject getc() {
// rb_io_readchar
@JRubyMethod
public IRubyObject readchar(ThreadContext context) {
IRubyObject c = getc19(context);
IRubyObject c = getc(context);

if (c == context.nil) throw context.runtime.newEOFError();

if (c.isNil()) {
throw context.runtime.newEOFError();
}
return c;
}

@@ -2670,7 +2666,7 @@ public IRubyObject readbyte(ThreadContext context) {

// rb_io_getc
@JRubyMethod(name = "getc")
public IRubyObject getc19(ThreadContext context) {
public IRubyObject getc(ThreadContext context) {
Ruby runtime = context.runtime;
Encoding enc;

@@ -2688,6 +2684,11 @@ public IRubyObject getc19(ThreadContext context) {
}
}

@Deprecated
public final IRubyObject getc19(ThreadContext context) {
return getc(context);
}

// rb_io_ungetbyte
@JRubyMethod
public IRubyObject ungetbyte(ThreadContext context, IRubyObject b) {
@@ -3723,20 +3724,21 @@ static IRubyObject seekBeforeAccess(ThreadContext context, RubyIO io, IRubyObjec
return io.doSeek(context, offset, mode);
}

public static IRubyObject readlines(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) {
return readlines19(context, recv, args, unusedBlock);
@Deprecated
public static IRubyObject readlines19(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) {
return readlines(context, recv, args, unusedBlock);
}

// rb_io_s_readlines
@JRubyMethod(name = "readlines", required = 1, optional = 3, meta = true)
public static IRubyObject readlines19(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) {
public static IRubyObject readlines(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) {
IRubyObject opt = ArgsUtil.getOptionsArg(context.runtime, args);
IRubyObject io = openKeyArgs(context, recv, args, opt);
if (io.isNil()) return io;

IRubyObject[] methodArguments = processReadlinesMethodArguments(context, args);

return readlinesCommon19(context, (RubyIO)io, methodArguments);
return readlinesCommon(context, (RubyIO) io, methodArguments);
}

private static IRubyObject[] processReadlinesMethodArguments(ThreadContext context, IRubyObject[] args) {
@@ -3755,7 +3757,7 @@ private static IRubyObject[] processReadlinesMethodArguments(ThreadContext conte
return methodArguments;
}

private static RubyArray readlinesCommon19(ThreadContext context, RubyIO file, IRubyObject[] newArguments) {
private static RubyArray readlinesCommon(ThreadContext context, RubyIO file, IRubyObject[] newArguments) {
try {
return file.readlines(context, newArguments);
} finally {
@@ -3807,12 +3809,12 @@ private void setupPopen(ModeFlags modes, POpenProcess process) throws RaiseExcep
}
}

private static class Ruby19POpen {
private static final class RubyPOpen {
final RubyString cmd;
final IRubyObject[] cmdPlusArgs;
final RubyHash env;

Ruby19POpen(Ruby runtime, IRubyObject[] args) {
RubyPOpen(Ruby runtime, IRubyObject[] args) {
IRubyObject[] _cmdPlusArgs;
IRubyObject _env;
IRubyObject _cmd;
@@ -3916,18 +3918,18 @@ public static IRubyObject popen(ThreadContext context, IRubyObject recv, IRubyOb
args = newArgs;
}

Ruby19POpen r19Popen = new Ruby19POpen(runtime, args);
RubyPOpen pOpen = new RubyPOpen(runtime, args);

if ("-".equals(r19Popen.cmd.toString())) {
if ("-".equals(pOpen.cmd.toString())) {
throw runtime.newNotImplementedError("popen(\"-\") is unimplemented");
}

try {
ShellLauncher.POpenProcess process;
if (r19Popen.cmdPlusArgs == null) {
process = ShellLauncher.popen(runtime, r19Popen.cmd, modes);
if (pOpen.cmdPlusArgs == null) {
process = ShellLauncher.popen(runtime, pOpen.cmd, modes);
} else {
process = ShellLauncher.popen(runtime, r19Popen.cmdPlusArgs, r19Popen.env, modes);
process = ShellLauncher.popen(runtime, pOpen.cmdPlusArgs, pOpen.env, modes);
}

checkPopenOptions(options);
@@ -3948,20 +3950,28 @@ public static IRubyObject popen(ThreadContext context, IRubyObject recv, IRubyOb
}
}

public static IRubyObject pipe(ThreadContext context, IRubyObject recv) {
return pipe19(context, recv);
}

@Deprecated
public static IRubyObject pipe19(ThreadContext context, IRubyObject recv) {
return pipe19(context, recv, IRubyObject.NULL_ARRAY, Block.NULL_BLOCK);
}

@Deprecated
public static IRubyObject pipe19(ThreadContext context, IRubyObject recv, IRubyObject modes) {
return pipe19(context, recv, new IRubyObject[] {modes}, Block.NULL_BLOCK);
}

@JRubyMethod(name = "pipe", optional = 3, meta = true)
@Deprecated
public static IRubyObject pipe19(ThreadContext context, IRubyObject klass, IRubyObject[] argv, Block block) {
return pipe(context, klass, argv, block);
}


public static IRubyObject pipe(ThreadContext context, IRubyObject recv) {
return pipe(context, recv, IRubyObject.NULL_ARRAY, Block.NULL_BLOCK);
}

@JRubyMethod(name = "pipe", optional = 3, meta = true)
public static IRubyObject pipe(ThreadContext context, IRubyObject klass, IRubyObject[] argv, Block block) {
Ruby runtime = context.runtime;
int state;
RubyIO r, w;
18 changes: 7 additions & 11 deletions core/src/main/java/org/jruby/java/addons/IOJavaAddons.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.jruby.java.addons;

import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.RubyIO;
import org.jruby.anno.JRubyMethod;
import org.jruby.javasupport.JavaUtil;
@@ -10,27 +8,25 @@
import org.jruby.util.IOChannel;
import org.jruby.util.IOInputStream;
import org.jruby.util.IOOutputStream;
import org.jruby.util.io.BadDescriptorException;
import org.jruby.util.io.InvalidValueException;

public class IOJavaAddons {
// FIXME This whole thing could probably be implemented as a module and
// mixed into appropriate classes, especially if it uses either
// IOInput/OutputStream or is smart about the kind of IO-like object
// it's being used against.

@JRubyMethod
@JRubyMethod(name = {"to_input_stream", "to_inputstream"})
public static IRubyObject to_inputstream(ThreadContext context, IRubyObject self) {
RubyIO io = (RubyIO)self;
RubyIO io = (RubyIO) self;

io.getOpenFile().checkReadable(context);

return JavaUtil.convertJavaToUsableRubyObject(context.runtime, io.getInStream());
}

@JRubyMethod
@JRubyMethod(name = {"to_output_stream", "to_outputstream"})
public static IRubyObject to_outputstream(ThreadContext context, IRubyObject self) {
RubyIO io = (RubyIO)self;
RubyIO io = (RubyIO) self;

io.getOpenFile().checkWritable(context);

@@ -39,19 +35,19 @@ public static IRubyObject to_outputstream(ThreadContext context, IRubyObject sel

@JRubyMethod
public static IRubyObject to_channel(ThreadContext context, IRubyObject self) {
RubyIO io = (RubyIO)self;
RubyIO io = (RubyIO) self;

return JavaUtil.convertJavaToUsableRubyObject(context.runtime, io.getChannel());
}

public static class AnyIO {
@JRubyMethod(name = "to_inputstream")
@JRubyMethod(name = {"to_input_stream", "to_inputstream"})
public static IRubyObject any_to_inputstream(ThreadContext context, IRubyObject self) {
// using IOInputStream may not be the most performance way, but it's easy.
return JavaUtil.convertJavaToUsableRubyObject(context.runtime, new IOInputStream(self));
}

@JRubyMethod(name = "to_outputstream")
@JRubyMethod(name = {"to_output_stream", "to_outputstream"})
public static IRubyObject any_to_outputstream(ThreadContext context, IRubyObject self) {
// using IOOutputStream may not be the most performance way, but it's easy.
return JavaUtil.convertJavaToUsableRubyObject(context.runtime, new IOOutputStream(self));
20 changes: 6 additions & 14 deletions core/src/main/java/org/jruby/util/IOInputStream.java
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
import java.io.IOException;
import org.jcodings.Encoding;

import org.jruby.Ruby;
import org.jruby.RubyFixnum;
import org.jruby.RubyIO;
import org.jruby.RubyString;
@@ -50,7 +51,7 @@
public class IOInputStream extends InputStream {
private final IRubyObject io;
private final InputStream in;
private final IRubyObject numOne;
private final RubyFixnum numOne;
private static final CallSite readAdapter = MethodIndex.getFunctionalCallSite("read");
private static final CallSite closeAdapter = MethodIndex.getFunctionalCallSite("close");

@@ -66,7 +67,7 @@ public IOInputStream(IRubyObject io) {
this.io = io;
this.in = ((io instanceof RubyIO) && !((RubyIO)io).isClosed() && ((RubyIO)io).isBuiltin("read"))
? ((RubyIO)io).getInStream() : null;
this.numOne = RubyFixnum.one(this.io.getRuntime());
this.numOne = RubyFixnum.one(io.getRuntime());
}

@Override
@@ -103,25 +104,16 @@ public int read() throws IOException {

@Override
public int read(byte[] b) throws IOException {
if (in != null) {
return in.read(b, 0, b.length);
}
IRubyObject readValue = readAdapter.call(io.getRuntime().getCurrentContext(), io, io, io.getRuntime().newFixnum(b.length));
int returnValue = -1;
if (!readValue.isNil()) {
ByteList str = readValue.convertToString().getByteList();
System.arraycopy(str.getUnsafeBytes(), str.getBegin(), b, 0, str.getRealSize());
returnValue = str.getRealSize();
}
return returnValue;
return read(b, 0, b.length);
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
if (in != null) {
return in.read(b, off, len);
}
IRubyObject readValue = readAdapter.call(io.getRuntime().getCurrentContext(), io, io, io.getRuntime().newFixnum(len));
final Ruby runtime = io.getRuntime();
IRubyObject readValue = readAdapter.call(runtime.getCurrentContext(), io, io, runtime.newFixnum(len));
int returnValue = -1;
if (!readValue.isNil()) {
ByteList str = readValue.convertToString().getByteList();
12 changes: 8 additions & 4 deletions spec/java_integration/addons/io_spec.rb
Original file line number Diff line number Diff line change
@@ -43,21 +43,23 @@
expect(String.from_java_bytes(output.to_byte_array)).to eq("12345")
end

it "is coercible to java.io.InputStream with IO#to_inputstream" do
it "is coercible to java.io.InputStream with IO#to_input_stream" do
file = File.open(__FILE__)
first_ten = file.read(10)
file.seek(0)
stream = file.to_inputstream
stream = file.to_input_stream
expect(java.io.InputStream).to be === stream

bytes = "0000000000".to_java_bytes
expect(stream.read(bytes)).to eq(10)
expect(String.from_java_bytes(bytes)).to eq(first_ten)

expect(java.io.InputStream).to be === file.to_inputstream # old-naming
end

it "is coercible to java.io.OutputStream with IO#to_outputstream" do
it "is coercible to java.io.OutputStream with IO#to_output_stream" do
file = Tempfile.new("io_spec")
stream = file.to_outputstream
stream = file.to_output_stream
expect(java.io.OutputStream).to be === stream

bytes = input_number.to_java_bytes
@@ -66,6 +68,8 @@
file.seek(0)
str = file.read(10)
expect(str).to eq(String.from_java_bytes(bytes))

expect(java.io.OutputStream).to be === file.to_outputstream # old-naming
end

it "gets an IO from a java.nio.channels.Channel" do
15 changes: 10 additions & 5 deletions spec/java_integration/addons/stringio_addons.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
require File.dirname(__FILE__) + "/../spec_helper"
require 'tempfile'
require 'stringio'

describe "Ruby StringIO" do
it "should be coercible to java.io.InputStream with StringIO#to_inputstream" do
it "should be coercible to java.io.InputStream with StringIO#to_input_stream" do
file = StringIO.new("\xC3\x80abcdefghij")
stream = file.to_inputstream
stream = file.to_input_stream
expect(java.io.InputStream).to be === stream

expect(stream.read).to eq(0xc3)
@@ -14,11 +13,14 @@
bytes = "0000000000".to_java_bytes
expect(stream.read(bytes)).to eq(10)
expect(String.from_java_bytes(bytes)).to eq('abcdefghij')

# compatiblity with old-naming
expect(java.io.InputStream).to be === file.to_inputstream
end

it "should be coercible to java.io.OutputStream with StringIO#to_outputstream" do
it "should be coercible to java.io.OutputStream with StringIO#to_output_stream" do
file = StringIO.new
stream = file.to_outputstream
stream = file.to_output_stream
expect(java.io.OutputStream).to be === stream

bytes = "1234567890".to_java_bytes
@@ -27,6 +29,9 @@
file.seek(0)
str = file.read(10)
expect(str).to eq(String.from_java_bytes(bytes))

# compatiblity with old-naming
expect(java.io.OutputStream).to be === file.to_outputstream
end

it "should be coercible to java.nio.channels.Channel with StringIO#to_channel" do