Method: FTW::Connection#read

Defined in:
lib/ftw/connection.rb

#read(length = 16384, timeout = nil) ⇒ Object

Read data from this connection This method blocks until the read succeeds unless a timeout is given.

This method is not guaranteed to read exactly ‘length’ bytes. See IO#sysread



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/ftw/connection.rb', line 231

def read(length=16384, timeout=nil)
  data = ""
  data.force_encoding("BINARY") if data.respond_to?(:force_encoding)
  have_pushback = !@pushback_buffer.empty?
  if have_pushback
    data << @pushback_buffer
    @pushback_buffer = ""
    # We have data 'now' so don't wait.
    timeout = 0
  end

  if readable?(timeout)
    begin
      # Read at most 'length' data, so read less from the socket
      # We'll read less than 'length' if the pushback buffer has
      # data in it already.
      @socket.sysread(length - data.length, @read_buffer)
      data << @read_buffer
      return data
    rescue EOFError => e
      @socket.close
      @connected = false
      raise e
    end
  else
    if have_pushback
      return data
    else
      raise ReadTimeout.new
    end
  end
end