Class: IO::Stream::Buffered
- Defined in:
- lib/io/stream/buffered.rb
Overview
A buffered stream implementation that wraps an underlying IO object to provide efficient buffered reading and writing.
Constant Summary
Constants included from Writable
Constants included from Readable
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
Attributes included from Writable
Attributes included from Readable
Class Method Summary collapse
-
.open(path, mode = "r+", **options) ⇒ Object
Open a file and wrap it in a buffered stream.
-
.wrap(io, **options) ⇒ Object
Wrap an existing IO object in a buffered stream.
Instance Method Summary collapse
-
#close_read ⇒ Object
Close the read end of the stream.
-
#close_write ⇒ Object
Close the write end of the stream.
-
#closed? ⇒ Boolean
Check if the stream is closed.
-
#initialize(io) ⇒ Buffered
constructor
Initialize a new buffered stream.
-
#readable? ⇒ Boolean
Check if the stream is readable.
- #The wrapped IO object.=(wrappedIOobject. = (value)) ⇒ Object
-
#to_io ⇒ Object
Get the underlying IO object.
Methods inherited from Generic
Methods included from Writable
#<<, async_safe?, #flush, #puts, #write
Methods included from Readable
async_safe?, #block_size, #block_size=, #discard_until, #finish!, #finished?, #gets, #peek, #read, #read_exactly, #read_partial, #read_until, #readpartial
Constructor Details
#initialize(io) ⇒ Buffered
Initialize a new buffered stream.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/io/stream/buffered.rb', line 53 def initialize(io, ...) super(...) @io = io if io.respond_to?(:timeout) @timeout = io.timeout else @timeout = nil end end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
65 66 67 |
# File 'lib/io/stream/buffered.rb', line 65 def io @io end |
Class Method Details
.open(path, mode = "r+", **options) ⇒ Object
Open a file and wrap it in a buffered stream.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/io/stream/buffered.rb', line 17 def self.open(path, mode = "r+", **) stream = self.new(::File.open(path, mode), **) return stream unless block_given? begin yield stream ensure stream.close end end |
.wrap(io, **options) ⇒ Object
Wrap an existing IO object in a buffered stream.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/io/stream/buffered.rb', line 33 def self.wrap(io, **) if io.respond_to?(:buffered=) io.buffered = false elsif io.respond_to?(:sync=) io.sync = true end stream = self.new(io, **) return stream unless block_given? begin yield stream ensure stream.close end end |
Instance Method Details
#close_read ⇒ Object
Close the read end of the stream.
80 81 82 |
# File 'lib/io/stream/buffered.rb', line 80 def close_read @io.close_read end |
#close_write ⇒ Object
Close the write end of the stream.
85 86 87 88 89 |
# File 'lib/io/stream/buffered.rb', line 85 def close_write super ensure @io.close_write end |
#closed? ⇒ Boolean
Check if the stream is closed.
75 76 77 |
# File 'lib/io/stream/buffered.rb', line 75 def closed? @io.closed? end |
#readable? ⇒ Boolean
Check if the stream is readable.
93 94 95 |
# File 'lib/io/stream/buffered.rb', line 93 def readable? super && @io.readable? end |
#The wrapped IO object.=(wrappedIOobject. = (value)) ⇒ Object
65 |
# File 'lib/io/stream/buffered.rb', line 65 attr :io |
#to_io ⇒ Object
Get the underlying IO object.
69 70 71 |
# File 'lib/io/stream/buffered.rb', line 69 def to_io @io.to_io end |