Class: Proxy::RemoteExecution::NetSSHCompat::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = +'')) ⇒ Buffer

Creates a new buffer, initialized to the given content. The position is initialized to the beginning of the buffer.



12
13
14
15
# File 'lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb', line 12

def initialize(content = +'')
  @content = content.to_s
  @position = 0
end

Instance Attribute Details

#contentObject (readonly)

exposes the raw content of the buffer



5
6
7
# File 'lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb', line 5

def content
  @content
end

#positionObject

the current position of the pointer in the buffer



8
9
10
# File 'lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb', line 8

def position
  @position
end

Instance Method Details

#append(text) ⇒ Object

Appends the given text to the end of the buffer. Does not alter the read position. Returns the buffer object itself.



78
79
80
81
# File 'lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb', line 78

def append(text)
  @content << text
  self
end

#availableObject

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).



24
25
26
# File 'lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb', line 24

def available
  length - position
end

#clear!Object

Resets the buffer, making it empty. Also, resets the read position to 0.



52
53
54
55
# File 'lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb', line 52

def clear!
  @content = +''
  @position = 0
end

#consume!(count = position) ⇒ Object

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.



64
65
66
67
68
69
70
71
72
73
74
# 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?Boolean

Returns true if the buffer contains no data (e.g., it is of zero length).

Returns:

  • (Boolean)


34
35
36
# File 'lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb', line 34

def empty?
  @content.empty?
end

#eof?Boolean

Returns true if the pointer is at the end of the buffer. Subsequent reads will return nil, in this case.

Returns:

  • (Boolean)


46
47
48
# File 'lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb', line 46

def eof?
  @position >= length
end

#lengthObject

Returns the length of the buffer’s content.



18
19
20
# File 'lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb', line 18

def length
  @content.length
end

#read(count = nil) ⇒ Object

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.



86
87
88
89
90
91
# 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!Object

Resets the pointer to the start of the buffer. Subsequent reads will begin at position 0.



40
41
42
# File 'lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb', line 40

def reset!
  @position = 0
end

#to_sObject

Returns a copy of the buffer’s content.



29
30
31
# File 'lib/smart_proxy_remote_execution_ssh/net_ssh_compat.rb', line 29

def to_s
  (@content || "").dup
end

#write(*data) ⇒ Object

Writes the given data literally into the string. Does not alter the read position. Returns the buffer object.



95
96
97
98
# 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