Created
December 5, 2012 05:13
-
-
Save tka/4212518 to your computer and use it in GitHub Desktop.
JRuby 1.7.1 vs MRI 1.9.3-p327 write binary string to file when Encoding.default_internal is UTF-8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When Encoding.default_internal is nil | |
============================== | |
Wirte remote_binary_data to test_file | |
Encoding.default_external:#<Encoding:UTF-8> | |
Encoding.default_internal:nil | |
data encoding: #<Encoding:ASCII-8BIT> | |
file external encoding: nil | |
file internal encoding: nil | |
file size: 13686 | |
Wirte local_binary_data to test_file | |
Encoding.default_external:#<Encoding:UTF-8> | |
Encoding.default_internal:nil | |
data encoding: #<Encoding:UTF-8> | |
file external encoding: nil | |
file internal encoding: nil | |
file size: 13686 | |
When Encoding.default_internal is UTF-8 | |
============================== | |
Wirte remote_binary_data to test_file | |
Encoding.default_external:#<Encoding:UTF-8> | |
Encoding.default_internal:#<Encoding:UTF-8> | |
data encoding: #<Encoding:ASCII-8BIT> | |
file external encoding: #<Encoding:UTF-8> | |
file internal encoding: nil | |
file size: 20025 | |
Wirte local_binary_data to test_file | |
Encoding.default_external:#<Encoding:UTF-8> | |
Encoding.default_internal:#<Encoding:UTF-8> | |
data encoding: #<Encoding:UTF-8> | |
file external encoding: #<Encoding:UTF-8> | |
file internal encoding: nil | |
file size: #<Encoding::UndefinedConversionError: Input length = 1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When Encoding.default_internal is nil | |
============================== | |
Wirte remote_binary_data to test_file | |
Encoding.default_external:#<Encoding:UTF-8> | |
Encoding.default_internal:nil | |
data encoding: #<Encoding:ASCII-8BIT> | |
file external encoding: nil | |
file internal encoding: nil | |
file size: 13686 | |
Wirte local_binary_data to test_file | |
Encoding.default_external:#<Encoding:UTF-8> | |
Encoding.default_internal:nil | |
data encoding: #<Encoding:UTF-8> | |
file external encoding: nil | |
file internal encoding: nil | |
file size: 13686 | |
When Encoding.default_internal is UTF-8 | |
============================== | |
Wirte remote_binary_data to test_file | |
Encoding.default_external:#<Encoding:UTF-8> | |
Encoding.default_internal:#<Encoding:UTF-8> | |
data encoding: #<Encoding:ASCII-8BIT> | |
file external encoding: #<Encoding:UTF-8> | |
file internal encoding: nil | |
file size: #<Encoding::UndefinedConversionError: "\x89" from ASCII-8BIT to UTF-8> | |
Wirte local_binary_data to test_file | |
Encoding.default_external:#<Encoding:UTF-8> | |
Encoding.default_internal:#<Encoding:UTF-8> | |
data encoding: #<Encoding:UTF-8> | |
file external encoding: #<Encoding:UTF-8> | |
file internal encoding: nil | |
file size: 13686 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
Encoding.default_external=Encoding::UTF_8 | |
remote_binary_data = open('https://github.com/apple-touch-icon-114.png').read | |
open('touch-icon-114.png','wb'){ |f| f.write(remote_binary_data)} | |
local_binary_data = open('touch-icon-114.png').read | |
def write_file(data) | |
puts "Encoding.default_external:" + Encoding.default_external.inspect | |
puts "Encoding.default_internal:" + Encoding.default_internal.inspect | |
puts "data encoding: #{data.encoding.inspect}" | |
begin | |
open('test_file','w') do |f| | |
puts "file external encoding: #{f.external_encoding.inspect}"; | |
puts "file internal encoding: #{f.internal_encoding.inspect}"; | |
f.write(data) | |
end | |
rescue Exception => e | |
e.inspect | |
end | |
end | |
puts "When Encoding.default_internal is nil" | |
puts "="*30 | |
puts "Wirte remote_binary_data to test_file " | |
puts "file size: #{write_file(remote_binary_data)}\n\n" | |
puts "Wirte local_binary_data to test_file " | |
puts "file size: #{write_file(local_binary_data)}" | |
puts "" | |
Encoding.default_internal=Encoding::UTF_8 | |
puts "When Encoding.default_internal is UTF-8" | |
puts "="*30 | |
puts "Wirte remote_binary_data to test_file " | |
puts "file size: #{write_file(remote_binary_data)}\n\n" | |
puts "Wirte local_binary_data to test_file " | |
puts "file size: #{write_file(local_binary_data)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment