class Proxy::RemoteExecution::NetSSHCompat::Buffer

Attributes

content[R]

exposes the raw content of the buffer

position[RW]

the current position of the pointer in the buffer

Public Class Methods

new(content = +'') click to toggle source

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

append(text) click to toggle source

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
available() click to toggle source

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
clear!() click to toggle source

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
consume!(count = position) click to toggle source

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
empty?() click to toggle source

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
eof?() click to toggle source

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
length() click to toggle source

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
read(count = nil) click to toggle source

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
reset!() click to toggle source

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
to_s() click to toggle source

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
write(*data) click to toggle source

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