Skip to content

Commit ef0fca9

Browse files
committed
[fix] IO#reopen: copy encoding from source IO
After reopen, the IO retained its old encoding settings instead of inheriting the source's external/internal encoding. CRuby's io_reopen explicitly copies fptr->encs = orig->encs right after the mode assignment.
1 parent 4fd8090 commit ef0fca9

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

core/src/main/java/org/jruby/RubyIO.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ protected RubyIO reopenIO(ThreadContext context, RubyIO nfile) {
533533
boolean locked2 = orig.lock(); // TODO: This WILL deadlock if two threads try to reopen the same IOs in opposite directions. Fix?
534534
try {
535535
fptr.setMode(orig.getMode() | (fptr.getMode() & (OpenFile.PREP | OpenFile.SYNC)));
536+
fptr.encs = orig.encs;
536537
fptr.setProcess(orig.getProcess());
537538
fptr.setLineNumber(orig.getLineNumber());
538539
if (orig.getPath() != null) fptr.setPath(orig.getPath());

test/jruby/test_io.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ def test_reopen
153153
assert_nothing_raised { file.reopen(@file) }
154154
end
155155

156+
def test_reopen_copies_encoding
157+
ensure_files @file, @file2
158+
f1 = File.open(@file)
159+
@to_close << f1
160+
f1.set_encoding("EUC-JP:UTF-8")
161+
162+
f2 = File.open(@file2)
163+
@to_close << f2
164+
f2.set_encoding("ISO-8859-1:US-ASCII")
165+
166+
f1.reopen(f2)
167+
168+
assert_equal Encoding::ISO_8859_1, f1.external_encoding
169+
assert_equal Encoding::US_ASCII, f1.internal_encoding
170+
end
171+
156172
def test_file_read
157173
ensure_files @file
158174
# test that read returns correct values

0 commit comments

Comments
 (0)