Class: Net::BufferedIO

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

Overview

Signature:

  • 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.



152
153
154
155
156
157
158
159
160
161
# File 'lib/net/protocol.rb', line 152

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.



166
167
168
# File 'lib/net/protocol.rb', line 166

def continue_timeout
  @continue_timeout
end

#debug_outputObject

Returns the value of attribute debug_output.



167
168
169
# File 'lib/net/protocol.rb', line 167

def debug_output
  @debug_output
end

#ioObject (readonly)

Returns the value of attribute io.



163
164
165
# File 'lib/net/protocol.rb', line 163

def io
  @io
end

#read_timeoutObject

Returns the value of attribute read_timeout.



164
165
166
# File 'lib/net/protocol.rb', line 164

def read_timeout
  @read_timeout
end

#write_timeoutObject

Returns the value of attribute write_timeout.



165
166
167
# File 'lib/net/protocol.rb', line 165

def write_timeout
  @write_timeout
end

Instance Method Details

#closeObject



181
182
183
# File 'lib/net/protocol.rb', line 181

def close
  @io.close
end

#closed?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/net/protocol.rb', line 177

def closed?
  @io.closed?
end

#eof?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/net/protocol.rb', line 173

def eof?
  @io.eof?
end

#inspectObject



169
170
171
# File 'lib/net/protocol.rb', line 169

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

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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/net/protocol.rb', line 191

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/net/protocol.rb', line 212

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



261
262
263
# File 'lib/net/protocol.rb', line 261

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

#readuntil(terminator, ignore_eof = false, limit: nil) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/net/protocol.rb', line 230

def readuntil(terminator, ignore_eof = false, limit: nil)
  unless limit.nil? || (Integer === limit && limit > 0)
    # Integer === calls nothing on limit, and only an Integer is
    # echoed back, so validation never runs the caller's code.
    got = Integer === limit ? limit : "a non-Integer"
    raise ArgumentError, "limit must be a positive Integer, got #{got}"
  end
  offset = @rbuf_offset
  begin
    until idx = @rbuf.index(terminator, offset)
      if limit && rbuf_size > limit
        raise ReadLimitExceeded, "exceeded the #{limit} byte read limit"
      end
      # Rewind so a terminator split across reads is still found. The
      # floor guards two things. String#index reads a negative offset
      # as counting from the end, and an offset below @rbuf_offset
      # matches inside bytes already returned.
      offset = [@rbuf.bytesize - terminator.bytesize + 1, @rbuf_offset].max
      rbuf_fill
    end
    len = idx + terminator.bytesize - @rbuf_offset
    if limit && len > limit
      raise ReadLimitExceeded, "exceeded the #{limit} byte read limit"
    end
    return rbuf_consume(len)
  rescue EOFError
    raise unless ignore_eof
    return rbuf_consume
  end
end

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



338
339
340
341
342
# File 'lib/net/protocol.rb', line 338

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

#writeline(str) ⇒ Object



346
347
348
349
350
# File 'lib/net/protocol.rb', line 346

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