Class: Async::IO::Stream
- Inherits:
-
Object
- Object
- Async::IO::Stream
- Defined in:
- lib/async/io/stream.rb
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
Instance Method Summary collapse
-
#<<(string) ⇒ Object
Writes ‘string` to the stream and returns self.
-
#close ⇒ Object
Closes the stream and flushes any unwritten data.
-
#eof? ⇒ Boolean
(also: #eof)
Returns true if the stream is at file which means there is no more data to be read.
-
#flush ⇒ Object
Flushes buffered data to the stream.
-
#initialize(io, block_size: 1024*8) ⇒ Stream
constructor
A new instance of Stream.
- #peek ⇒ Object
-
#read(size = nil) ⇒ Object
Reads ‘size` bytes from the stream.
-
#read_until(pattern) ⇒ String
Efficiently read data from the stream until encountering pattern.
-
#write(string) ⇒ Object
Writes ‘string` to the buffer.
Constructor Details
#initialize(io, block_size: 1024*8) ⇒ Stream
Returns a new instance of Stream.
27 28 29 30 31 32 33 34 35 |
# File 'lib/async/io/stream.rb', line 27 def initialize(io, block_size: 1024*8) @io = io @eof = false @block_size = block_size @read_buffer = BinaryString.new @write_buffer = BinaryString.new end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
37 38 39 |
# File 'lib/async/io/stream.rb', line 37 def io @io end |
Instance Method Details
#<<(string) ⇒ Object
Writes ‘string` to the stream and returns self.
65 66 67 68 69 |
# File 'lib/async/io/stream.rb', line 65 def <<(string) write(string) return self end |
#close ⇒ Object
Closes the stream and flushes any unwritten data.
78 79 80 81 82 |
# File 'lib/async/io/stream.rb', line 78 def close flush rescue nil @io.close end |
#eof? ⇒ Boolean Also known as: eof
Returns true if the stream is at file which means there is no more data to be read.
85 86 87 88 89 |
# File 'lib/async/io/stream.rb', line 85 def eof? fill_read_buffer if !@eof && @read_buffer.empty? return @eof && @read_buffer.empty? end |
#flush ⇒ Object
Flushes buffered data to the stream.
72 73 74 75 |
# File 'lib/async/io/stream.rb', line 72 def flush syswrite(@write_buffer) @write_buffer.clear end |
#peek ⇒ Object
115 116 117 118 119 |
# File 'lib/async/io/stream.rb', line 115 def peek until yield(@read_buffer) || @eof fill_read_buffer end end |
#read(size = nil) ⇒ Object
Reads ‘size` bytes from the stream. If size is not specified, read until end of file.
40 41 42 43 44 45 46 47 48 |
# File 'lib/async/io/stream.rb', line 40 def read(size = nil) return "" if size == 0 until @eof || (size && size <= @read_buffer.size) fill_read_buffer end return consume_read_buffer(size) end |
#read_until(pattern) ⇒ String
Efficiently read data from the stream until encountering pattern.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/async/io/stream.rb', line 96 def read_until(pattern) index = @read_buffer.index(pattern) until index offset = @read_buffer.size fill_read_buffer return if @eof index = @read_buffer.index(pattern, offset) end matched = @read_buffer.slice!(0, index) @read_buffer.slice!(0, pattern.bytesize) return matched end |
#write(string) ⇒ Object
Writes ‘string` to the buffer. When the buffer is full or #sync is true the buffer is flushed to the underlying `io`.
54 55 56 57 58 59 60 61 62 |
# File 'lib/async/io/stream.rb', line 54 def write(string) @write_buffer << string if @write_buffer.size > @block_size flush end return string.bytesize end |