Class: BufferedIO::ReadBuffer
- Inherits:
-
Object
- Object
- BufferedIO::ReadBuffer
- Defined in:
- lib/buffered_io/read_buffer.rb
Overview
Maintains a read buffer for an IO object.
Constant Summary collapse
- DEFAULT_BUFFER_SIZE =
:nodoc:
1024 * 16
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
Instance Method Summary collapse
-
#consume(len) ⇒ Object
Consume bytes from the IOBuffer.
-
#fill ⇒ Object
Attempt to fill the IOBuffer.
- #index(i) ⇒ Object
-
#initialize(io) ⇒ ReadBuffer
constructor
A new instance of ReadBuffer.
-
#size ⇒ Object
Number of bytes in the buffer.
Constructor Details
#initialize(io) ⇒ ReadBuffer
Returns a new instance of ReadBuffer.
7 8 9 10 11 12 |
# File 'lib/buffered_io/read_buffer.rb', line 7 def initialize(io) @io = io @buffer = '' @read_timeout = 60 @buffer_size = DEFAULT_BUFFER_SIZE end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
14 15 16 |
# File 'lib/buffered_io/read_buffer.rb', line 14 def io @io end |
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
15 16 17 |
# File 'lib/buffered_io/read_buffer.rb', line 15 def read_timeout @read_timeout end |
Instance Method Details
#consume(len) ⇒ Object
Consume bytes from the IOBuffer
29 30 31 |
# File 'lib/buffered_io/read_buffer.rb', line 29 def consume(len) return @buffer.slice!(0, len) end |
#fill ⇒ Object
Attempt to fill the IOBuffer
18 19 20 21 22 23 24 25 26 |
# File 'lib/buffered_io/read_buffer.rb', line 18 def fill begin @buffer << @io.read_nonblock(@buffer_size) rescue ::IO::WaitReadable IO.select([@io], nil, nil, @read_timeout) ? retry : (raise ::Timeout::Error) rescue ::IO::WaitWritable IO.select(nil, [@io], nil, @read_timeout) ? retry : (raise ::Timeout::Error) end end |
#index(i) ⇒ Object
38 39 40 |
# File 'lib/buffered_io/read_buffer.rb', line 38 def index(i) @buffer.index(i) end |
#size ⇒ Object
Number of bytes in the buffer
34 35 36 |
# File 'lib/buffered_io/read_buffer.rb', line 34 def size @buffer.size end |