Class: Piperator::IO
- Inherits:
-
Object
- Object
- Piperator::IO
- Defined in:
- lib/piperator/io.rb
Overview
Pseudo I/O on Enumerators
Constant Summary collapse
- FLUSH_THRESHOLD =
128KiB
128 * 1028
Instance Attribute Summary collapse
-
#eof ⇒ Object
(also: #eof?)
readonly
Returns the value of attribute eof.
-
#pos ⇒ Object
(also: #tell)
readonly
Returns the value of attribute pos.
Instance Method Summary collapse
-
#flush ⇒ Object
Flush internal buffer until the last unread byte.
-
#gets(separator = $INPUT_RECORD_SEPARATOR, _limit = nil) ⇒ String
Reads the next “line” from the I/O stream; lines are separated by separator.
-
#initialize(enumerator, flush_threshold: FLUSH_THRESHOLD) ⇒ IO
constructor
A new instance of IO.
-
#peek(bytes) ⇒ Object
Return the first bytes of the buffer without marking the buffer as read.
-
#read(length = nil) ⇒ Object
Reads length bytes from the I/O stream.
-
#used ⇒ Integer
Current buffer size - including non-freed read content.
Constructor Details
#initialize(enumerator, flush_threshold: FLUSH_THRESHOLD) ⇒ IO
Returns a new instance of IO.
11 12 13 14 15 16 17 18 |
# File 'lib/piperator/io.rb', line 11 def initialize(enumerator, flush_threshold: FLUSH_THRESHOLD) @enumerator = enumerator @flush_threshold = flush_threshold @buffer = StringIO.new @pos = 0 @buffer_read_pos = 0 @eof = false end |
Instance Attribute Details
#eof ⇒ Object (readonly) Also known as: eof?
Returns the value of attribute eof.
8 9 10 |
# File 'lib/piperator/io.rb', line 8 def eof @eof end |
#pos ⇒ Object (readonly) Also known as: tell
Returns the value of attribute pos.
9 10 11 |
# File 'lib/piperator/io.rb', line 9 def pos @pos end |
Instance Method Details
#flush ⇒ Object
Flush internal buffer until the last unread byte
53 54 55 56 57 58 59 60 |
# File 'lib/piperator/io.rb', line 53 def flush if @buffer.pos == @buffer_read_pos initialize_buffer else @buffer.pos = @buffer_read_pos initialize_buffer(@buffer.read) end end |
#gets(separator = $INPUT_RECORD_SEPARATOR, _limit = nil) ⇒ String
Reads the next “line” from the I/O stream; lines are separated by separator.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/piperator/io.rb', line 40 def gets(separator = $INPUT_RECORD_SEPARATOR, _limit = nil) while !@eof && !contains_line?(separator) begin @buffer.write(@enumerator.next) rescue StopIteration @eof = true nil end end read_with { @buffer.gets(separator) } end |
#peek(bytes) ⇒ Object
Return the first bytes of the buffer without marking the buffer as read.
24 25 26 27 28 29 30 31 32 |
# File 'lib/piperator/io.rb', line 24 def peek(bytes) while @eof == false && readable_bytes < (bytes || 1) @buffer.write(@enumerator.next) end peek_buffer(bytes) rescue StopIteration @eof = true peek_buffer(bytes) end |
#read(length = nil) ⇒ Object
Reads length bytes from the I/O stream.
66 67 68 69 70 71 72 73 |
# File 'lib/piperator/io.rb', line 66 def read(length = nil) return @enumerator.next.tap { |e| @pos += e.bytesize } if length.nil? && readable_bytes.zero? @buffer.write(@enumerator.next) while !@eof && readable_bytes < (length || 1) read_with { @buffer.read(length) } rescue StopIteration @eof = true read_with { @buffer.read(length) } if readable_bytes > 0 end |
#used ⇒ Integer
Current buffer size - including non-freed read content
78 79 80 |
# File 'lib/piperator/io.rb', line 78 def used @buffer.size end |