Method: TCPSocket#read

Defined in:
lib/polyphony/extensions/socket.rb

#read(len = nil, buf = nil, buffer_pos = 0) ⇒ String

Reads from the socket. If maxlen is given, reads up to maxlen bytes from the socket, otherwise reads to EOF. If buf is given, it is used as the buffer to read into, otherwise a new string is allocated. If buffer_pos is given, reads into the given offset (in bytes) in the given buffer. If the given buffer offset is negative, it is calculated from the current end of the buffer (-1 means the read data will be appended to the end of the buffer).

If no bytes are available and EOF is not hit, this method will block until the socket is ready to read from.

Parameters:

  • len (Integer, nil) (defaults to: nil)

    maximum bytes to read from socket

  • buf (String, nil) (defaults to: nil)

    buffer to read into

  • buffer_pos (Number) (defaults to: 0)

    buffer position to read into

Returns:

  • (String)

    buffer used for reading



336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/polyphony/extensions/socket.rb', line 336

def read(len = nil, buf = nil, buffer_pos = 0)
  return '' if len == 0
  return Polyphony.backend_read(self, buf, len, true, buffer_pos) if buf

  @read_buffer ||= +''
  result = Polyphony.backend_read(self, @read_buffer, len, true, -1)
  return nil unless result

  already_read = @read_buffer
  @read_buffer = +''
  already_read
end