lib/bindata/io.rb in bindata-0.11.1 vs lib/bindata/io.rb in bindata-1.0.0

- old
+ new

@@ -1,10 +1,20 @@ +require 'stringio' + module BinData # A wrapper around an IO object. The wrapper provides a consistent # interface for BinData objects to use when accessing the IO. class IO + # Creates a StringIO around +str+. + def self.create_string_io(str = "") + if RUBY_VERSION >= "1.9" + str = str.dup.force_encoding(Encoding::BINARY) + end + StringIO.new(str) + end + # Create a new IO wrapper around +io+. +io+ must support #read if used # for reading, #write if used for writing, #pos if reading the current # stream position and #seek if setting the current stream position. If # +io+ is a string it will be automatically wrapped in an StringIO object. # @@ -23,10 +33,10 @@ def initialize(io) raise ArgumentError, "io must not be a BinData::IO" if BinData::IO === io # wrap strings in a StringIO if io.respond_to?(:to_str) - io = StringIO.new(io) + io = BinData::IO.create_string_io(io.to_str) end @raw_io = io # initial stream position if stream supports positioning