Class: QIO::QueuedInputIO

Inherits:
Object
  • Object
show all
Includes:
BaseIO
Defined in:
lib/qio/queued_input_io.rb

Instance Method Summary collapse

Methods included from BaseIO

#<<, #advise, #autoclose=, #autoclose?, #binmode, #binmode?, #bytes, #chars, #close_on_exec=, #close_on_exec?, #close_read, #close_write, #codepoints, #external_encoding, #fcntl, #fdatasync, #fileno, #flush, #fsync, #getbyte, #getc, #gets, #internal_encoding, #ioctl, #isatty, #lineno, #lineno=, #lines, #pid, #pos, #pos=, #print, #printf, #putc, #puts, #read, #read_nonblock, #readbyte, #readchar, #readline, #readlines, #readpartial, #reopen, #rewind, #seek, #set_encoding, #stat, #sync, #sync=, #syswrite, #ungetbyte, #ungetc, #write, #write_nonblock

Constructor Details

#initializeQueuedInputIO

Returns a new instance of QueuedInputIO.



4
5
6
7
8
9
10
# File 'lib/qio/queued_input_io.rb', line 4

def initialize
  super
  @deque = [] #TODO: Consider using a real deque.
  @eof = false
  @closed = false
  @queued_eof = false
end

Instance Method Details

#add_input(string) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/qio/queued_input_io.rb', line 55

def add_input(string)
  raise(ArgumentError, 'input must be a string') unless string.is_a?(String)
  assert_open
  assert_accepting_input
  return self if string.empty?
  provide_readable do
    assert_open
    assert_accepting_input
    @deque.push(string)
    true
  end
  self
end

#closeObject



12
13
14
15
16
17
18
19
# File 'lib/qio/queued_input_io.rb', line 12

def close
  assert_open
  synchronize do
    @closed = true
    @deque.clear
    @deque = nil
  end
end

#closed?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/qio/queued_input_io.rb', line 21

def closed?
  @closed
end

#end_input!Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/qio/queued_input_io.rb', line 69

def end_input!
  return self if @queued_eof
  provide_readable do
    unless @queued_eof
      @deque.push(:eof)
      @queued_eof = true
    end
    true
  end
  self
end

#eof?Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/qio/queued_input_io.rb', line 25

def eof?
  assert_open
  @eof ||= (@queued_eof && @deque.length <= 1)
end

#sysread(maxlen, outbuf = nil) ⇒ Object

Raises:

  • (EOFError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/qio/queued_input_io.rb', line 30

def sysread(maxlen, outbuf=nil)
  q = @deque
  raise EOFError if eof?
  outbuf ||= ""
  consume_readable do
    raise EOFError if eof?
    data = @deque.shift
    raise SystemCallError if data.nil? || data.empty?
    if data.bytesize <= maxlen
      outbuf.replace(data)
      pos += outbuf.bytesize
    else
      outbuf.replace(data.byteslice(0, maxlen))
      @deque.unshift(data.byteslice(maxlen .. -1))
      pos += maxlen
    end
    @deque.any?
  end
  outbuf
end

#sysseek(offset, whence = IO::SEEK_SET) ⇒ Object



51
52
53
# File 'lib/qio/queued_input_io.rb', line 51

def sysseek(offset, whence=IO::SEEK_SET)
  # TODO: Implement (best-effort) sysseek
end