Class: Piperator::IO

Inherits:
Object
  • Object
show all
Defined in:
lib/piperator/io.rb

Overview

Pseudo I/O on Enumerators

Constant Summary collapse

FLUSH_THRESHOLD =

128KiB

128 * 1028

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#eofObject (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

#posObject (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

#flushObject

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.

Parameters:

  • separator (String) (defaults to: $INPUT_RECORD_SEPARATOR)

    separator to split input

  • _limit (defaults to: nil)

    Unused parameter for compatiblity

Returns:

  • (String)


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.

Parameters:

  • length (Integer) (defaults to: nil)

    number of bytes to read

Returns:

  • String



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

#usedInteger

Current buffer size - including non-freed read content

Returns:

  • (Integer)

    number of bytes stored in buffer



78
79
80
# File 'lib/piperator/io.rb', line 78

def used
  @buffer.size
end