Class: Net::BufferedIO

Inherits:
Object
  • Object
show all
Defined in:
lib/net/protocol.rb

Overview

:nodoc: internal use only

Direct Known Subclasses

InternetMessageIO

Constant Summary

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (BufferedIO) initialize(io)



50
51
52
53
54
55
# File 'lib/net/protocol.rb', line 50

def initialize(io)
  @io = io
  @read_timeout = 60
  @debug_output = nil
  @rbuf = ''
end

Instance Attribute Details

- (Object) debug_output

Returns the value of attribute debug_output



59
60
61
# File 'lib/net/protocol.rb', line 59

def debug_output
  @debug_output
end

- (Object) io (readonly)

Returns the value of attribute io



57
58
59
# File 'lib/net/protocol.rb', line 57

def io
  @io
end

- (Object) read_timeout

Returns the value of attribute read_timeout



58
59
60
# File 'lib/net/protocol.rb', line 58

def read_timeout
  @read_timeout
end

Instance Method Details

- (Object) close



69
70
71
# File 'lib/net/protocol.rb', line 69

def close
  @io.close
end

- (Boolean) closed?



65
66
67
# File 'lib/net/protocol.rb', line 65

def closed?
  @io.closed?
end

- (Object) inspect



61
62
63
# File 'lib/net/protocol.rb', line 61

def inspect
  "#<#{self.class} io=#{@io}>"
end

- (Object) read(len, dest = '', ignore_eof = false)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/net/protocol.rb', line 79

def read(len, dest = '', ignore_eof = false)
  LOG "reading #{len} bytes..."
  read_bytes = 0
  begin
    while read_bytes + @rbuf.size < len
      dest << (s = rbuf_consume(@rbuf.size))
      read_bytes += s.size
      rbuf_fill
    end
    dest << (s = rbuf_consume(len - read_bytes))
    read_bytes += s.size
  rescue EOFError
    raise unless ignore_eof
  end
  LOG "read #{read_bytes} bytes"
  dest
end

- (Object) read_all(dest = '')



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/net/protocol.rb', line 97

def read_all(dest = '')
  LOG 'reading all...'
  read_bytes = 0
  begin
    while true
      dest << (s = rbuf_consume(@rbuf.size))
      read_bytes += s.size
      rbuf_fill
    end
  rescue EOFError
    ;
  end
  LOG "read #{read_bytes} bytes"
  dest
end

- (Object) readline



125
126
127
# File 'lib/net/protocol.rb', line 125

def readline
  readuntil("\n").chop
end

- (Object) readuntil(terminator, ignore_eof = false)



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/net/protocol.rb', line 113

def readuntil(terminator, ignore_eof = false)
  begin
    until idx = @rbuf.index(terminator)
      rbuf_fill
    end
    return rbuf_consume(idx + terminator.size)
  rescue EOFError
    raise unless ignore_eof
    return rbuf_consume(@rbuf.size)
  end
end

- (Object) write(str)



151
152
153
154
155
# File 'lib/net/protocol.rb', line 151

def write(str)
  writing {
    write0 str
  }
end

- (Object) writeline(str)



157
158
159
160
161
# File 'lib/net/protocol.rb', line 157

def writeline(str)
  writing {
    write0 str + "\r\n"
  }
end