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

Returns a new instance of BufferedIO.



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

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



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

def continue_timeout
  @continue_timeout
end

#debug_outputObject

Returns the value of attribute debug_output



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

def debug_output
  @debug_output
end

#ioObject (readonly)

Returns the value of attribute io



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

def io
  @io
end

#read_timeoutObject

Returns the value of attribute read_timeout



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

def read_timeout
  @read_timeout
end

Instance Method Details

#closeObject



89
90
91
# File 'lib/net/protocol.rb', line 89

def close
  @io.close
end

#closed?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/net/protocol.rb', line 85

def closed?
  @io.closed?
end

#eof?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/net/protocol.rb', line 81

def eof?
  @io.eof?
end

#inspectObject



77
78
79
# File 'lib/net/protocol.rb', line 77

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

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



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

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



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

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



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

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

#readuntil(terminator, ignore_eof = false) ⇒ Object



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

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