diff options
Diffstat (limited to 'lib/openssl/buffering.rb')
-rw-r--r-- | lib/openssl/buffering.rb | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/lib/openssl/buffering.rb b/lib/openssl/buffering.rb index 5d0ed3b6..a5f4241b 100644 --- a/lib/openssl/buffering.rb +++ b/lib/openssl/buffering.rb @@ -22,6 +22,29 @@ module OpenSSL::Buffering include Enumerable + # A buffer which will retain binary encoding. + class Buffer < String + BINARY = Encoding::BINARY + + def initialize + super + + force_encoding(BINARY) + end + + def << string + if string.encoding == BINARY + super(string) + else + super(string.b) + end + + return self + end + + alias concat << + end + ## # The "sync mode" of the SSLSocket. # @@ -40,7 +63,7 @@ module OpenSSL::Buffering def initialize(*) super @eof = false - @rbuffer = String.new + @rbuffer = Buffer.new @sync = @io.sync end @@ -312,7 +335,7 @@ module OpenSSL::Buffering # buffer is flushed to the underlying socket. def do_write(s) - @wbuffer = String.new unless defined? @wbuffer + @wbuffer = Buffer.new unless defined? @wbuffer @wbuffer << s @wbuffer.force_encoding(Encoding::BINARY) @sync ||= false @@ -398,7 +421,7 @@ module OpenSSL::Buffering # See IO#puts for full details. def puts(*args) - s = String.new + s = Buffer.new if args.empty? s << "\n" end @@ -416,7 +439,7 @@ module OpenSSL::Buffering # See IO#print for full details. def print(*args) - s = String.new + s = Buffer.new args.each{ |arg| s << arg.to_s } do_write(s) nil |