diff --git a/ext/java/org/jruby/ext/stringio/StringIO.java b/ext/java/org/jruby/ext/stringio/StringIO.java index c76fd25e..13329a02 100644 --- a/ext/java/org/jruby/ext/stringio/StringIO.java +++ b/ext/java/org/jruby/ext/stringio/StringIO.java @@ -149,7 +149,7 @@ public Encoding getEncoding() { } RubyString string = ptr.string; - if (string != null && !string.isNil()) { + if (string != null) { return string.getEncoding(); } @@ -1067,19 +1067,19 @@ public IRubyObject putc(ThreadContext context, IRubyObject ch) { public static final ByteList NEWLINE = ByteList.create("\n"); - @JRubyMethod(name = "read") + @JRubyMethod(name = "read") // strio.read() public IRubyObject read(ThreadContext context) { return readCommon(context, 0, null, null); } - @JRubyMethod(name = "read") - public IRubyObject read(ThreadContext context, IRubyObject arg0) { - return readCommon(context, 1, arg0, null); + @JRubyMethod(name = "read") // strio.read(length) + public IRubyObject read(ThreadContext context, IRubyObject length) { + return readCommon(context, 1, length, null); } - @JRubyMethod(name = "read") - public IRubyObject read(ThreadContext context, IRubyObject arg0, IRubyObject arg1) { - return readCommon(context, 2, arg0, arg1); + @JRubyMethod(name = "read") // strio.read(length, outbuf) + public IRubyObject read(ThreadContext context, IRubyObject length, IRubyObject outbuf) { + return readCommon(context, 2, length, outbuf); } @SuppressWarnings("fallthrough") @@ -1102,7 +1102,7 @@ private IRubyObject readCommon(ThreadContext context, int argc, IRubyObject arg0 str = arg1; if (!str.isNil()) { str = str.convertToString(); - ((RubyString) str).modify(); + modifyString((RubyString) str); } case 1: if (!arg0.isNil()) { @@ -1198,7 +1198,7 @@ private RubyString preadCommon(ThreadContext context, int argc, IRubyObject arg0 str = arg2; if (!str.isNil()) { str = str.convertToString(); - ((RubyString) str).modify(); + modifyString((RubyString) str); } case 2: len = RubyNumeric.fix2int(arg0); @@ -2210,12 +2210,13 @@ private void checkWritable() { } private void checkModifiable() { - if (getPtr().string == null || getPtr().string.isNil()) { + final RubyString string = getPtr().string; + if (string == null) { /* Null device StringIO */ - } else if (getPtr().string.isFrozen()) { + } else if (string.isFrozen()) { throw getRuntime().newIOError("not modifiable string"); } else { - getPtr().string.modify(); + string.modify(); } } diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 70bab8af..fe890406 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -1064,6 +1064,20 @@ def test_coderange_after_overwrite assert_predicate(s.string, :ascii_only?) end + def test_coderange_after_read_into_buffer + s = StringIO.new("01234567890".b) + + buf = "¿Cómo estás? Ça va bien?" + assert_not_predicate(buf, :ascii_only?) + + assert_predicate(s.string, :ascii_only?) + + s.read(10, buf) + + assert_predicate(buf, :ascii_only?) + assert_equal '0123456789', buf + end + require "objspace" if ObjectSpace.respond_to?(:dump) && ObjectSpace.dump(eval(%{"test"})).include?('"chilled":true') # Ruby 3.4+ chilled strings def test_chilled_string