class Ciphr::Stream

Public Class Methods

new(reader) click to toggle source
# File lib/ciphr/stream.rb, line 3
def initialize(reader)
  @reader = reader
  @buffer = ""
  @eof = false
end

Public Instance Methods

prepend(str) click to toggle source
# File lib/ciphr/stream.rb, line 31
def prepend(str)
  @buffer = str + @buffer
end
read(n=nil) click to toggle source
# File lib/ciphr/stream.rb, line 9
def read(n=nil) #fix this
  if n
    init
    while @buffer.size < n && !@eof 
      fill
    end
    if @buffer.size > 0
      ret = @buffer[0,n]
      @buffer = @buffer[n..-1] || ''
    else 
      ret = nil
    end
    ret
  else
    buff = ""
    while chunk=read(256)
      buff+=chunk
    end
    buff 
  end
end

Private Instance Methods

fill() click to toggle source
# File lib/ciphr/stream.rb, line 36
def fill
  data = @reader.call
  @eof = true if !data
  @buffer = @buffer + data if data
end
init() click to toggle source
# File lib/ciphr/stream.rb, line 42
def init #hack
  if !@init
    @init = true
    @reader = @reader.apply if @reader.respond_to?(:apply)
  end
end