lib/bencode/parser.rb in bencode-0.8.2 vs lib/bencode/parser.rb in bencode-1.0.0

- old
+ new

@@ -1,20 +1,21 @@ require 'stringio' module BEncode class Parser - attr_reader :stream + attr_reader :stream, :encoding def initialize(stream) @stream = if stream.kind_of?(IO) || stream.kind_of?(StringIO) stream elsif stream.respond_to? :string StringIO.new stream.string elsif stream.respond_to? :to_s StringIO.new stream.to_s end + @encoding = @stream.external_encoding || Encoding::default_external end def parse! case peek when ?i then parse_integer! @@ -48,11 +49,11 @@ stream.getc hsh = {} until peek == ?e key = parse! - unless key.is_a? String or key.is_a? Fixnum + unless key.is_a? String or key.is_a? Integer raise BEncode::DecodeError, "key must be a string or number" end val = parse! @@ -67,10 +68,10 @@ raise BEncode::DecodeError, "invalid string length (no colon)" begin length = num.chop.to_i return "" if length == 0 # Workaround for Rubinius bug - str = stream.read(length) + str = stream.read(length).force_encoding(encoding) rescue raise BEncode::DecodeError, "invalid string length" end str