Method: Down::ChunkedIO#read

Defined in:
lib/down/chunked_io.rb

#read(length = nil, outbuf = nil) ⇒ Object

Implements IO#read semantics. Without arguments it retrieves and returns all content.

With ‘length` argument returns exactly that number of bytes if they’re available.

With ‘outbuf` argument each call will return that same string object, where the value is replaced with retrieved content.

If end of file is reached, returns empty string if called without arguments, or nil if called with arguments. Raises IOError if closed.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/down/chunked_io.rb', line 63

def read(length = nil, outbuf = nil)
  fail IOError, "closed stream" if closed?

  remaining_length = length

  begin
    data = readpartial(remaining_length, outbuf)
    data = data.dup unless outbuf
    remaining_length = length - data.bytesize if length
  rescue EOFError
  end

  until remaining_length == 0 || eof?
    data << readpartial(remaining_length)
    remaining_length = length - data.bytesize if length
  end

  data.to_s unless length && length > 0 && (data.nil? || data.empty?)
end