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

- (Buffer) initialize



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

def initialize
  clear!
end

Instance Method Details

- (Object) [](o)



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

- (Object) add(data, offset = endd)



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

- (Object) clear!



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

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

- (Boolean) empty?



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

def empty?; @start.nil?; end

- (Object) endd



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

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

- (Object) index(what, start = 0)



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

- (Object) rindex(what, start = 0)



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

- (Object) size



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

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

- (Object) start



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

def start; @start; end

- (Object) to_s

for debugging



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

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