Class: BinaryStringIO

Inherits:
StringIO
  • Object
show all
Defined in:
lib/archive/support/binary_stringio.rb

Overview

This class is a version of StringIO that always uses the binary encoding on any Ruby platform that has a notion of encodings. On Ruby platforms without encoding support, this class is equivalent to StringIO.

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ BinaryStringIO

Creates a new instance of this class.

This takes all the arguments of StringIO.new.



12
13
14
15
16
17
18
19
20
# File 'lib/archive/support/binary_stringio.rb', line 12

def initialize(*args)
  super

  # Force a binary encoding when possible.
  if respond_to?(:set_encoding, true)
    @encoding_locked = false
    set_encoding('binary')
  end
end

Instance Method Details

#set_encoding(*args) ⇒ Object

Raise an exception on attempts to change the encoding.



24
25
26
27
28
# File 'lib/archive/support/binary_stringio.rb', line 24

def set_encoding(*args)
  raise 'Changing encoding is not allowed' if @encoding_locked
  @encoding_locked = true
  super
end