lib/net/ssh/buffer.rb in net-ssh-6.2.0.rc2 vs lib/net/ssh/buffer.rb in net-ssh-6.3.0.beta1

- old
+ new

@@ -3,11 +3,10 @@ require 'net/ssh/authentication/certificate' require 'net/ssh/authentication/ed25519_loader' module Net module SSH - # Net::SSH::Buffer is a flexible class for building and parsing binary # data packets. It provides a stream-like interface for sequentially # reading data items from the buffer, as well as a useful helper method # for building binary packets given a signature. # @@ -69,11 +68,11 @@ # the current position of the pointer in the buffer attr_accessor :position # Creates a new buffer, initialized to the given content. The position # is initialized to the beginning of the buffer. - def initialize(content="") + def initialize(content=String.new) @content = content.to_s @position = 0 end # Returns the length of the buffer's content. @@ -116,11 +115,11 @@ end # Resets the buffer, making it empty. Also, resets the read position to # 0. def clear! - @content = "" + @content = String.new @position = 0 end # Consumes n bytes from the buffer, where n is the current position # unless otherwise specified. This is useful for removing data from the @@ -132,11 +131,11 @@ def consume!(n=position) if n >= length # optimize for a fairly common case clear! elsif n > 0 - @content = @content[n..-1] || "" + @content = @content[n..-1] || String.new @position -= n @position = 0 if @position < 0 end self end @@ -235,10 +234,11 @@ # essentially just a string, which is reinterpreted to be a bignum in # binary format. def read_bignum data = read_string return unless data + OpenSSL::BN.new(data, 2) end # Read a key from the buffer. The key will start with a string # describing its type. The remainder of the key is defined by the @@ -344,10 +344,15 @@ end # Optimized version of write where the caller gives up ownership of string # to the method. This way we can mutate the string. def write_moved(string) - @content << string.force_encoding('BINARY') + @content << + if string.frozen? + string.dup.force_encoding('BINARY') + else + string.force_encoding('BINARY') + end self end # Writes each argument to the buffer as a network-byte-order-encoded # 64-bit integer (8 bytes). Does not alter the read position. Returns the