Method: Async::IO::Stream#read

Defined in:
lib/async/io/stream.rb

#read(size = nil) ⇒ Object

Reads size bytes from the stream. If size is not specified, read until end of file.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/async/io/stream.rb', line 74

def read(size = nil)
  return String.new(encoding: Encoding::BINARY) if size == 0
  
  if size
    until @eof or @read_buffer.bytesize >= size
      # Compute the amount of data we need to read from the underlying stream:
      read_size = size - @read_buffer.bytesize
      
      # Don't read less than @block_size to avoid lots of small reads:
      fill_read_buffer(read_size > @block_size ? read_size : @block_size)
    end
  else
    until @eof
      fill_read_buffer
    end
  end
  
  return consume_read_buffer(size)
end