class Proxy::RemoteExecution::NetSSHCompat::Buffer
Attributes
exposes the raw content of the buffer
the current position of the pointer in the buffer
Public Class Methods
Creates a new buffer, initialized to the given content. The position is initialized to the beginning of the buffer.
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 12 def initialize(content = +'') @content = content.to_s @position = 0 end
Public Instance Methods
Appends the given text to the end of the buffer. Does not alter the read position. Returns the buffer object itself.
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 78 def append(text) @content << text self end
Returns the number of bytes available to be read (e.g., how many bytes remain between the current position and the end of the buffer).
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 24 def available length - position end
Resets the buffer, making it empty. Also, resets the read position to 0.
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 52 def clear! @content = +'' @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 buffer that has previously been read, when you are expecting more data to be appended. It helps to keep the size of buffers down when they would otherwise tend to grow without bound.
Returns the buffer object itself.
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 64 def consume!(count = position) if count >= length # OPTIMIZE: a fairly common case clear! elsif count.positive? @content = @content[count..-1] || +'' @position -= count @position = 0 if @position.negative? end self end
Returns true
if the buffer contains no data (e.g., it is of zero length).
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 34 def empty? @content.empty? end
Returns true if the pointer is at the end of the buffer. Subsequent reads will return nil, in this case.
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 46 def eof? @position >= length end
Returns the length of the buffer's content.
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 18 def length @content.length end
Reads and returns the next count
bytes from the buffer, starting from the read position. If count
is nil
, this will return all remaining text in the buffer. This method will increment the pointer.
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 86 def read(count = nil) count ||= length count = length - @position if @position + count > length @position += count @content[@position - count, count] end
Resets the pointer to the start of the buffer. Subsequent reads will begin at position 0.
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 40 def reset! @position = 0 end
Returns a copy of the buffer's content.
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 29 def to_s (@content || "").dup end
Writes the given data literally into the string. Does not alter the read position. Returns the buffer object.
# File lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb, line 95 def write(*data) data.each { |datum| @content << datum.dup.force_encoding('BINARY') } self end