def read(length = nil, buffer = nil)
      length ||= (mem.size - pos)
      buffer ||= ''
      buffer.force_encoding('ASCII-8BIT') unless RUBY_VERSION.start_with?('1.8')
      
      actual_length = [(mem.size - pos), length].min
      actual_length.times do
        buffer << _getbyte
      end
      buffer
    end
def read(length = nil, buffer = nil)
      length ||= (mem.size - pos)
      buffer ||= ''
      buffer.force_encoding('ASCII-8BIT') unless RUBY_VERSION.start_with?('1.8')
      
      actual_length = [(mem.size - pos), length].min
      actual_length.times do
        buffer << _getbyte
      end
      buffer
    end
FIXME: this doesn't match IO#read exactly (corner cases about
 EOF and whether length was nil or not), but it's enough for
 {Marshal::load}.