Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions ext/java/org/jruby/ext/stringio/StringIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public Encoding getEncoding() {
}

RubyString string = ptr.string;
if (string != null && !string.isNil()) {
if (string != null) {
return string.getEncoding();
}

Expand Down Expand Up @@ -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")
Expand All @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading