Class: Tofu::ChunkedStream

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

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ ChunkedStream

Returns a new instance of ChunkedStream.



591
592
593
594
595
# File 'lib/tofu.rb', line 591

def initialize(stream)
  @data = nil
  @cursor = 0
  @stream = stream
end

Instance Method Details

#closeObject



603
604
605
# File 'lib/tofu.rb', line 603

def close
  @stream.close
end

#next_chunkObject



597
598
599
600
601
# File 'lib/tofu.rb', line 597

def next_chunk
  @stream.pop
rescue
  raise EOFError
end

#readpartial(size, buf = '') ⇒ Object



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/tofu.rb', line 607

def readpartial(size, buf='')
  buf.clear
  unless @data
    @cursor = 0
    @data = next_chunk
    @data.force_encoding("ascii-8bit")
  end
  if @data.bytesize <= size
    buf << @data
    @data = nil
  else
    slice = @data.byteslice(@cursor, size)
    @cursor += slice.bytesize
    buf << slice
    if @data.bytesize <= @cursor
      @data = nil
    end
  end
  buf
end