Class: Redwood::MBox::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/sup/mbox/ssh-file.rb

Overview

a simple buffer of contiguous data

Instance Method Summary collapse

Constructor Details

#initializeBuffer

Returns a new instance of Buffer



27
28
29
# File 'lib/sup/mbox/ssh-file.rb', line 27

def initialize
  clear!
end

Instance Method Details

#[](o) ⇒ Object



60
61
62
63
# File 'lib/sup/mbox/ssh-file.rb', line 60

def [](o)
  raise "only ranges supported due to programmer's laziness" unless o.is_a? Range
  @buf[Range.new(o.first - @start, o.last - @start, o.exclude_end?)]
end

#add(data, offset = endd) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sup/mbox/ssh-file.rb', line 40

def add data, offset=endd
  #MBox::debug "+ adding #{data.length} bytes; size will be #{size + data.length}; limit #{SSHFile::MAX_BUF_SIZE}"

  if start.nil?
    @buf = data
    @start = offset
    return
  end

  raise "non-continguous data added to buffer (data #{offset}:#{offset + data.length}, buf range #{start}:#{endd})" if offset + data.length < start || offset > endd

  if offset < start
    @buf = data[0 ... (start - offset)] + @buf
    @start = offset
  else
    return if offset + data.length < endd
    @buf += data[(endd - offset) .. -1]
  end
end

#clear!Object



31
32
33
34
# File 'lib/sup/mbox/ssh-file.rb', line 31

def clear!
  @start = nil
  @buf = ""
end

#empty?Boolean

Returns:

  • (Boolean)


36
# File 'lib/sup/mbox/ssh-file.rb', line 36

def empty?; @start.nil?; end

#enddObject



38
# File 'lib/sup/mbox/ssh-file.rb', line 38

def endd; @start + @buf.length; end

#index(what, start = 0) ⇒ Object



65
66
67
68
# File 'lib/sup/mbox/ssh-file.rb', line 65

def index what, start=0
  x = @buf.index(what, start - @start)
  x.nil? ? nil : x + @start
end

#rindex(what, start = 0) ⇒ Object



70
71
72
73
# File 'lib/sup/mbox/ssh-file.rb', line 70

def rindex what, start=0
  x = @buf.rindex(what, start - @start)
  x.nil? ? nil : x + @start
end

#sizeObject



75
# File 'lib/sup/mbox/ssh-file.rb', line 75

def size; empty? ? 0 : @buf.size; end

#startObject



37
# File 'lib/sup/mbox/ssh-file.rb', line 37

def start; @start; end

#to_sObject

for debugging



76
# File 'lib/sup/mbox/ssh-file.rb', line 76

def to_s; empty? ? "<empty>" : "[#{start}, #{endd})"; end