Class: Net::BufferedIO

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

Overview

:nodoc: internal use only

Direct Known Subclasses

InternetMessageIO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, read_timeout: 60, write_timeout: 60, continue_timeout: nil, debug_output: nil) ⇒ BufferedIO

Returns a new instance of BufferedIO.



116
117
118
119
120
121
122
123
124
125
# File 'lib/net/protocol.rb', line 116

def initialize(io, read_timeout: 60, write_timeout: 60, continue_timeout: nil, debug_output: nil)
  @io = io
  @read_timeout = read_timeout
  @write_timeout = write_timeout
  @continue_timeout = continue_timeout
  @debug_output = debug_output
  @rbuf = ''.b
  @rbuf_empty = true
  @rbuf_offset = 0
end

Instance Attribute Details

#continue_timeoutObject

Returns the value of attribute continue_timeout.



130
131
132
# File 'lib/net/protocol.rb', line 130

def continue_timeout
  @continue_timeout
end

#debug_outputObject

Returns the value of attribute debug_output.



131
132
133
# File 'lib/net/protocol.rb', line 131

def debug_output
  @debug_output
end

#ioObject (readonly)

Returns the value of attribute io.



127
128
129
# File 'lib/net/protocol.rb', line 127

def io
  @io
end

#read_timeoutObject

Returns the value of attribute read_timeout.



128
129
130
# File 'lib/net/protocol.rb', line 128

def read_timeout
  @read_timeout
end

#write_timeoutObject

Returns the value of attribute write_timeout.



129
130
131
# File 'lib/net/protocol.rb', line 129

def write_timeout
  @write_timeout
end

Instance Method Details

#closeObject



145
146
147
# File 'lib/net/protocol.rb', line 145

def close
  @io.close
end

#closed?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/net/protocol.rb', line 141

def closed?
  @io.closed?
end

#eof?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/net/protocol.rb', line 137

def eof?
  @io.eof?
end

#inspectObject



133
134
135
# File 'lib/net/protocol.rb', line 133

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

#read(len, dest = ''.b, ignore_eof = false) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/net/protocol.rb', line 155

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

#read_all(dest = ''.b) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/net/protocol.rb', line 176

def read_all(dest = ''.b)
  LOG 'reading all...'
  read_bytes = 0
  begin
    while true
      if s = rbuf_consume_all
        read_bytes += s.bytesize
        dest << s
      end
      rbuf_fill
    end
  rescue EOFError
    ;
  end
  LOG "read #{read_bytes} bytes"
  dest
end

#readlineObject



208
209
210
# File 'lib/net/protocol.rb', line 208

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

#readuntil(terminator, ignore_eof = false) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/net/protocol.rb', line 194

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

#write(*strs) ⇒ Object Also known as: <<



285
286
287
288
289
# File 'lib/net/protocol.rb', line 285

def write(*strs)
  writing {
    write0(*strs)
  }
end

#writeline(str) ⇒ Object



293
294
295
296
297
# File 'lib/net/protocol.rb', line 293

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