From: "godfat (Lin Jen-Shin)" Date: 2013-08-11T03:02:41+09:00 Subject: [ruby-core:56518] [ruby-trunk - Bug #8767][Open] IO.copy_stream should write in binary mode. Issue #8767 has been reported by godfat (Lin Jen-Shin). ---------------------------------------- Bug #8767: IO.copy_stream should write in binary mode. https://bugs.ruby-lang.org/issues/8767 Author: godfat (Lin Jen-Shin) Status: Open Priority: Normal Assignee: Category: core Target version: current: 2.1.0 ruby -v: ruby 2.1.0dev (2013-08-11 trunk 42495) [x86_64-darwin12.2.1] Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN This patch makes `IO.copy_stream' always copy in binary mode, fixing the following scenario: require 'tempfile' require 'stringio' Encoding.default_internal = 'UTF-8' out = Tempfile.new('out') out.binmode # before this patch it raises: # in `write': "\xDE" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError) IO.copy_stream(StringIO.new("\xDE\xAD\xBE\xEF"), out) The other way to fix this would be trying to preserve the file mode from Tempfile instead of always writing in binary mode. However, this won't be the case for other objects responding to `to_path'. I think as we're treating destination as streams, we would always want writing in binary. So I guess this is ok. Thank you for reviewing. This is discovered by using rubyzip, which is using Tempfile for buffering input/output stream. Commit on Github: https://github.com/godfat/ruby/commit/94d9f3dd733fd19f5ade7b6e6f5bdf0c904e06c1 -- http://bugs.ruby-lang.org/