Class: IO::LikeHelpers::BlockingIO

Inherits:
DelegatedIO show all
Defined in:
lib/io/like_helpers/blocking_io.rb

Overview

This class implements a stream that always blocks regardless of the blocking state of the delegate.

Instance Method Summary collapse

Methods inherited from DelegatedIO

#advise, #autoclose=, #autoclose?, #close, #close_on_exec=, #close_on_exec?, create_finalizer, #fcntl, #fdatasync, #fileno, #fsync, #initialize, #inspect, #ioctl, #nonblock=, #nonblock?, #nread, #path, #pid, #pread, #pwrite, #readable?, #ready?, #seek, #stat, #to_io, #tty?, #wait, #writable?

Methods inherited from AbstractIO

#advise, #close, #close_on_exec=, #close_on_exec?, #closed?, #fcntl, #fdatasync, #fileno, #fsync, #initialize, #ioctl, #nonblock, #nonblock=, #nonblock?, #nread, open, #path, #pid, #pread, #pwrite, #readable?, #ready?, #seek, #stat, #to_io, #tty?, #wait, #writable?

Constructor Details

This class inherits a constructor from IO::LikeHelpers::DelegatedIO

Instance Method Details

#read(length, buffer: nil, buffer_offset: 0) ⇒ Integer, String

Reads bytes from the stream.

Note that a partial read will occur if the stream is in non-blocking mode and reading more bytes would block. If no bytes can be read, however, the read will block until at least 1 byte can be read.

Parameters:

  • length (Integer)

    the number of bytes to read

  • buffer (String) (defaults to: nil)

    the buffer into which bytes will be read (encoding assumed to be binary)

  • buffer_offset (Integer) (defaults to: 0)

    the index at which to insert bytes into ‘buffer`

Returns:

  • (Integer)

    the number of bytes read if ‘buffer` is not `nil`

  • (String)

    a buffer containing the bytes read if ‘buffer` is `nil`

Raises:

  • (EOFError)

    when reading at the end of the stream

  • (IOError)

    if the stream is not readable



29
30
31
# File 'lib/io/like_helpers/blocking_io.rb', line 29

def read(length, buffer: nil, buffer_offset: 0)
  ensure_blocking { super }
end

#write(buffer, length: buffer.bytesize) ⇒ Integer

Writes bytes to the stream.

Note that a partial write will occur if the stream is in non-blocking mode and writing more bytes would block. If no bytes can be written, however, the write will block until at least 1 byte can be written.

Parameters:

  • buffer (String)

    the bytes to write (encoding assumed to be binary)

  • length (Integer) (defaults to: buffer.bytesize)

    the number of bytes to write from ‘buffer`

Returns:

  • (Integer)

    the number of bytes written

Raises:

  • (IOError)

    if the stream is not writable



46
47
48
# File 'lib/io/like_helpers/blocking_io.rb', line 46

def write(buffer, length: buffer.bytesize)
  ensure_blocking { super }
end