Class: Net::BufferedIO

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

Overview

:nodoc: internal use only

Direct Known Subclasses

FTP::BufferedSocket, InternetMessageIO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ BufferedIO


62
63
64
65
66
67
68
# File 'lib/net/protocol.rb', line 62

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

Instance Attribute Details

#continue_timeoutObject

Returns the value of attribute continue_timeout


72
73
74
# File 'lib/net/protocol.rb', line 72

def continue_timeout
  @continue_timeout
end

#debug_outputObject

Returns the value of attribute debug_output


73
74
75
# File 'lib/net/protocol.rb', line 73

def debug_output
  @debug_output
end

#ioObject (readonly)

Returns the value of attribute io


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

def io
  @io
end

#read_timeoutObject

Returns the value of attribute read_timeout


71
72
73
# File 'lib/net/protocol.rb', line 71

def read_timeout
  @read_timeout
end

Instance Method Details

#closeObject


87
88
89
# File 'lib/net/protocol.rb', line 87

def close
  @io.close
end

#closed?Boolean


83
84
85
# File 'lib/net/protocol.rb', line 83

def closed?
  @io.closed?
end

#eof?Boolean


79
80
81
# File 'lib/net/protocol.rb', line 79

def eof?
  @io.eof?
end

#inspectObject


75
76
77
# File 'lib/net/protocol.rb', line 75

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

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


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

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

#read_all(dest = '') ⇒ Object


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

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

#readlineObject


143
144
145
# File 'lib/net/protocol.rb', line 143

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

#readuntil(terminator, ignore_eof = false) ⇒ Object


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/net/protocol.rb', line 131

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

#write(str) ⇒ Object Also known as: <<


183
184
185
186
187
# File 'lib/net/protocol.rb', line 183

def write(str)
  writing {
    write0 str
  }
end

#writeline(str) ⇒ Object


191
192
193
194
195
# File 'lib/net/protocol.rb', line 191

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