Class: IO::LikeHelpers::AbstractIO Abstract
- Inherits:
-
Object
- Object
- IO::LikeHelpers::AbstractIO
- Defined in:
- lib/io/like_helpers/abstract_io.rb
Overview
It defines the structure expected of low level streams and largely reflects the structure of the IO class, but most methods raise NotImplementedError. Only the most basic semantics for stream opening and closing are provided.
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
-
#advise(advice, offset = 0, len = 0) ⇒ nil
Announces an intention to access data from the stream in a specific pattern.
-
#close ⇒ nil, ...
Closes the stream.
-
#close_on_exec=(close_on_exec) ⇒ Boolean
Sets the close-on-exec flag for the underlying file descriptor.
-
#close_on_exec? ⇒ Boolean
Returns ‘true` if the close-on-exec flag is set for this stream and `false` otherwise.
-
#closed? ⇒ Boolean
Returns ‘true` if this stream is closed and `false` otherwise.
-
#fcntl(integer_cmd, arg) ⇒ Integer
Issues low level commands to control or query the file-oriented stream upon which this stream is based.
-
#fdatasync ⇒ 0?
Triggers the operating system to write any buffered metadata to disk immediately.
-
#fileno ⇒ Integer
The numeric file descriptor for the stream.
-
#fsync ⇒ 0?
Triggers the operating system to write any buffered data to disk immediately.
-
#initialize(**kwargs) ⇒ AbstractIO
constructor
Creates a new instance of this class.
-
#ioctl(integer_cmd, arg) ⇒ Integer
Issues low level commands to control or query the device upon which this stream is based.
-
#nonblock(nonblock = true) {|self| ... } ⇒ self
Yields ‘self` to the given block after setting the blocking mode as dictated by `nonblock`.
-
#nonblock=(nonblock) ⇒ Object
Sets the stream into either blocking or non-blocking mode.
-
#nonblock? ⇒ true, false
Returns whether or not the stream is in non-blocking mode.
-
#nread ⇒ Integer
The number of bytes that can be read without blocking or ‘0` if unknown.
-
#path ⇒ String
Returns the path of the file associated with this stream.
-
#pid ⇒ Integer?
Returns the pid of the process associated with this stream.
-
#pread(length, offset, buffer: nil, buffer_offset: 0) ⇒ Integer, ...
Reads at most ‘length` bytes from the stream starting at `offset` without modifying the read position in the stream.
-
#pwrite(buffer, offset, length: buffer.bytesize) ⇒ Integer, ...
Writes at most ‘length` bytes to the stream starting at `offset` without modifying the write position in the stream.
-
#read(length, buffer: nil, buffer_offset: 0) ⇒ Integer, ...
Reads bytes from the stream.
-
#readable? ⇒ Boolean
Returns ‘true` if the stream is readable and `false` otherwise.
-
#ready? ⇒ true, false
Returns whether or not the stream has input available.
-
#seek(amount, whence) ⇒ Integer
Sets the current stream position to ‘amount` based on the setting of `whence`.
-
#stat ⇒ File::Stat
Returns status information for the stream.
-
#to_io ⇒ IO
Returns the native IO object upon which this stream is based.
-
#tty? ⇒ true, false
Returns whether or not the stream is a tty.
-
#wait(events, timeout = nil) ⇒ true, false
Waits until the stream becomes ready for at least 1 of the specified events.
-
#writable? ⇒ Boolean
Returns ‘true` if the stream is writable and `false` otherwise.
-
#write(buffer, length: buffer.bytesize) ⇒ Integer, ...
Writes bytes to the stream.
Constructor Details
#initialize(**kwargs) ⇒ AbstractIO
Remove explicit ‘kwargs` parameter when Ruby 2.6 support is dropped.
Creates a new instance of this class.
50 51 52 |
# File 'lib/io/like_helpers/abstract_io.rb', line 50 def initialize(**kwargs) @closed = false end |
Class Method Details
.open(*args, **kwargs) ⇒ Object .open(*args, **kwargs) {|stream| ... } ⇒ block result
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/io/like_helpers/abstract_io.rb', line 28 def self.open(*args, **kwargs) io = new(*args, **kwargs) return io unless block_given? begin yield(io) ensure while Symbol === io.close do warn 'warning: waiting for nonblocking close to complete at the end of the open method' # A wait timeout is used in order to allow a retry in case the stream # was closed in another thread while waiting. io.wait(IO::READABLE | IO::WRITABLE, 1) end end end |
Instance Method Details
#advise(advice, offset = 0, len = 0) ⇒ nil
Announces an intention to access data from the stream in a specific pattern.
This method is a no-op if not implemented. If ‘offset` and `len` are both `0`, then the entire stream is affected.
| ‘advice` | Meaning | | ——– | ——- | | `:normal` | No advice given; default assumption for the stream. | | `:sequential` | The data will be read sequentially from lower offsets to higher ones. | | `:random` | The data will be accessed in random order. | | `:willneed` | The data will be accessed in the near future. | | `:dontneed` | The data will not be accessed in the near future. | | `:noreuse` | The data will only be accessed once. |
See ‘posix_fadvise(2)` for more details.
79 80 81 |
# File 'lib/io/like_helpers/abstract_io.rb', line 79 def advise(advice, offset = 0, len = 0) nil end |
#close ⇒ nil, ...
Closes the stream.
Most operations on the stream after this method is called will result in IOErrors being raised.
92 93 94 95 |
# File 'lib/io/like_helpers/abstract_io.rb', line 92 def close @closed = true nil end |
#close_on_exec=(close_on_exec) ⇒ Boolean
Sets the close-on-exec flag for the underlying file descriptor.
Note that setting this to ‘false` can lead to file descriptor leaks in multithreaded applications that fork and exec or use the `system` method.
112 113 114 |
# File 'lib/io/like_helpers/abstract_io.rb', line 112 def close_on_exec=(close_on_exec) raise NotImplementedError end |
#close_on_exec? ⇒ Boolean
Returns ‘true` if the close-on-exec flag is set for this stream and `false` otherwise.
121 122 123 |
# File 'lib/io/like_helpers/abstract_io.rb', line 121 def close_on_exec? raise NotImplementedError end |
#closed? ⇒ Boolean
Returns ‘true` if this stream is closed and `false` otherwise.
101 102 103 |
# File 'lib/io/like_helpers/abstract_io.rb', line 101 def closed? @closed end |
#fcntl(integer_cmd, arg) ⇒ Integer
Issues low level commands to control or query the file-oriented stream upon which this stream is based.
139 140 141 |
# File 'lib/io/like_helpers/abstract_io.rb', line 139 def fcntl(integer_cmd, arg) raise NotImplementedError end |
#fdatasync ⇒ 0?
Triggers the operating system to write any buffered metadata to disk immediately.
The default implementation calls #fsync, but override this if the stream natively supports the equivalent of ‘fdatasync(2)` to offer better performance when only metadata needs to be flushed to disk.
152 153 154 |
# File 'lib/io/like_helpers/abstract_io.rb', line 152 def fdatasync fsync end |
#fileno ⇒ Integer
Returns the numeric file descriptor for the stream.
158 159 160 |
# File 'lib/io/like_helpers/abstract_io.rb', line 158 def fileno raise NotImplementedError end |
#fsync ⇒ 0?
Triggers the operating system to write any buffered data to disk immediately.
167 168 169 |
# File 'lib/io/like_helpers/abstract_io.rb', line 167 def fsync raise NotImplementedError end |
#ioctl(integer_cmd, arg) ⇒ Integer
Issues low level commands to control or query the device upon which this stream is based.
185 186 187 |
# File 'lib/io/like_helpers/abstract_io.rb', line 185 def ioctl(integer_cmd, arg) raise NotImplementedError end |
#nonblock(nonblock = true) {|self| ... } ⇒ self
Yields ‘self` to the given block after setting the blocking mode as dictated by `nonblock`.
Ensures that the original blocking mode is reinstated after yielding.
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/io/like_helpers/abstract_io.rb', line 203 def nonblock(nonblock = true) assert_open begin orig_nonblock = nonblock? self.nonblock = nonblock yield(self) ensure self.nonblock = orig_nonblock end end |
#nonblock=(nonblock) ⇒ Object
Sets the stream into either blocking or non-blocking mode.
220 221 222 |
# File 'lib/io/like_helpers/abstract_io.rb', line 220 def nonblock=(nonblock) raise NotImplementedError end |
#nonblock? ⇒ true, false
Returns whether or not the stream is in non-blocking mode.
231 232 233 |
# File 'lib/io/like_helpers/abstract_io.rb', line 231 def nonblock? raise NotImplementedError end |
#nread ⇒ Integer
Returns the number of bytes that can be read without blocking or ‘0` if unknown.
240 241 242 243 |
# File 'lib/io/like_helpers/abstract_io.rb', line 240 def nread assert_readable 0 end |
#path ⇒ String
Returns the path of the file associated with this stream.
249 250 251 |
# File 'lib/io/like_helpers/abstract_io.rb', line 249 def path raise NotImplementedError end |
#pid ⇒ Integer?
Returns the pid of the process associated with this stream.
260 261 262 263 |
# File 'lib/io/like_helpers/abstract_io.rb', line 260 def pid assert_open nil end |
#pread(length, offset, buffer: nil, buffer_offset: 0) ⇒ Integer, ...
Reads at most ‘length` bytes from the stream starting at `offset` without modifying the read position in the stream.
Note that a partial read will occur if the stream is in non-blocking mode and reading more bytes would block.
288 289 290 291 292 293 294 295 296 297 |
# File 'lib/io/like_helpers/abstract_io.rb', line 288 def pread(length, offset, buffer: nil, buffer_offset: 0) position = seek(0, IO::SEEK_CUR) seek(offset, IO::SEEK_SET) begin read(length, buffer: buffer, buffer_offset: buffer_offset) ensure seek(position, IO::SEEK_SET) end end |
#pwrite(buffer, offset, length: buffer.bytesize) ⇒ Integer, ...
Writes at most ‘length` bytes to the stream starting at `offset` without modifying the write position in the stream.
Note that a partial write will occur if the stream is in non-blocking mode and writing more bytes would block.
316 317 318 319 320 321 322 323 324 325 |
# File 'lib/io/like_helpers/abstract_io.rb', line 316 def pwrite(buffer, offset, length: buffer.bytesize) position = seek(0, IO::SEEK_CUR) seek(offset, IO::SEEK_SET) begin write(buffer, length: length) ensure seek(position, IO::SEEK_SET) end end |
#read(length, buffer: nil, buffer_offset: 0) ⇒ Integer, ...
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.
346 347 348 |
# File 'lib/io/like_helpers/abstract_io.rb', line 346 def read(length, buffer: nil, buffer_offset: 0) assert_readable end |
#readable? ⇒ Boolean
Returns ‘true` if the stream is readable and `false` otherwise.
354 355 356 |
# File 'lib/io/like_helpers/abstract_io.rb', line 354 def readable? false end |
#ready? ⇒ true, false
Returns whether or not the stream has input available.
363 364 365 366 |
# File 'lib/io/like_helpers/abstract_io.rb', line 363 def ready? assert_open false end |
#seek(amount, whence) ⇒ Integer
Sets the current stream position to ‘amount` based on the setting of `whence`.
| ‘whence` | `amount` Interpretation | | ——– | ———————– | | `:CUR` or `IO::SEEK_CUR` | `amount` added to current stream position | | `:END` or `IO::SEEK_END` | `amount` added to end of stream position (`amount` will usually be negative here) | | `:SET` or `IO::SEEK_SET` | `amount` used as absolute position |
386 387 388 389 |
# File 'lib/io/like_helpers/abstract_io.rb', line 386 def seek(amount, whence) assert_open raise Errno::ESPIPE end |
#stat ⇒ File::Stat
Returns status information for the stream.
395 396 397 |
# File 'lib/io/like_helpers/abstract_io.rb', line 395 def stat raise NotImplementedError end |
#to_io ⇒ IO
Returns the native IO object upon which this stream is based.
403 404 405 |
# File 'lib/io/like_helpers/abstract_io.rb', line 403 def to_io raise NotImplementedError end |
#tty? ⇒ true, false
Returns whether or not the stream is a tty.
414 415 416 417 |
# File 'lib/io/like_helpers/abstract_io.rb', line 414 def tty? assert_open false end |
#wait(events, timeout = nil) ⇒ true, false
Waits until the stream becomes ready for at least 1 of the specified events.
429 430 431 |
# File 'lib/io/like_helpers/abstract_io.rb', line 429 def wait(events, timeout = nil) raise NotImplementedError end |
#writable? ⇒ Boolean
Returns ‘true` if the stream is writable and `false` otherwise.
455 456 457 |
# File 'lib/io/like_helpers/abstract_io.rb', line 455 def writable? false 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.
447 448 449 |
# File 'lib/io/like_helpers/abstract_io.rb', line 447 def write(buffer, length: buffer.bytesize) assert_writable end |