Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

StringIO#read(length) returns binary string when passed length

Test Script
----
require 'stringio'
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

str = "abcdefg" * 100
str.force_encoding('UTF-8')

io = StringIO.new(str)
s  = io.read(0)
assert_equal("", s)
assert_equal(Encoding::ASCII_8BIT, s.encoding)

s = io.read(10)
assert_equal("abcdefgabc", s)
assert_equal(Encoding::ASCII_8BIT, s.encoding)

s = io.read()
assert_equal(Encoding::UTF_8, s.encoding)

length = Object.new
def length.to_int
  0
end
io = StringIO.new("")
s = io.read(length)
assert_equal("", s)
assert_equal(Encoding::ASCII_8BIT, s.encoding)
  • Loading branch information...
commit 2ef505b1a30abd35717e8f08291af616bd269762 1 parent 28f7615
@Watson1978 Watson1978 authored
Showing with 15 additions and 10 deletions.
  1. +15 −10 lib/stringio.rb
View
25 lib/stringio.rb
@@ -131,23 +131,28 @@ def read(length = nil, buffer = String.new)
raise TypeError unless buffer.respond_to?(:to_str)
buffer = buffer.to_str
- if length == 0
- buffer.replace("")
- elsif length == nil
+ if length.nil?
return buffer.replace("") if self.eof?
buffer.replace(@string[@pos..-1])
@pos = @string.size
else
- if self.eof?
- buffer.replace("")
- return nil
- end
raise TypeError unless length.respond_to?(:to_int)
length = length.to_int
raise ArgumentError if length < 0
- buffer.replace(@string[@pos, length])
- @pos += buffer.length
- end
+
+ if length == 0
+ buffer.replace("")
+ else
+ if self.eof?
+ buffer.replace("")
+ return nil
+ end
+ buffer.replace(@string[@pos, length])
+ @pos += buffer.length
+ end
+ buffer.force_encoding('BINARY')
+ end
+
buffer
end
Please sign in to comment.
Something went wrong with that request. Please try again.