1 | =begin
|
---|
2 | = $RCSfile$ -- Buffering mix-in module.
|
---|
3 |
|
---|
4 | = Info
|
---|
5 | 'OpenSSL for Ruby 2' project
|
---|
6 | Copyright (C) 2001 GOTOU YUUZOU <[email protected]>
|
---|
7 | All rights reserved.
|
---|
8 |
|
---|
9 | = Licence
|
---|
10 | This program is licenced under the same licence as Ruby.
|
---|
11 | (See the file 'LICENCE'.)
|
---|
12 |
|
---|
13 | = Version
|
---|
14 | $Id: buffering.rb 11708 2007-02-12 23:01:19Z shyouhei $
|
---|
15 | =end
|
---|
16 |
|
---|
17 | module Buffering
|
---|
18 | include Enumerable
|
---|
19 | attr_accessor :sync
|
---|
20 | BLOCK_SIZE = 1024*16
|
---|
21 |
|
---|
22 | def initialize(*args)
|
---|
23 | @eof = false
|
---|
24 | @rbuffer = ""
|
---|
25 | @sync = @io.sync
|
---|
26 | end
|
---|
27 |
|
---|
28 | #
|
---|
29 | # for reading.
|
---|
30 | #
|
---|
31 | private
|
---|
32 |
|
---|
33 | def fill_rbuff
|
---|
34 | begin
|
---|
35 | @rbuffer << self.sysread(BLOCK_SIZE)
|
---|
36 | rescue Errno::EAGAIN
|
---|
37 | retry
|
---|
38 | rescue EOFError
|
---|
39 | @eof = true
|
---|
40 | end
|
---|
41 | end
|
---|
42 |
|
---|
43 | def consume_rbuff(size=nil)
|
---|
44 | if @rbuffer.empty?
|
---|
45 | nil
|
---|
46 | else
|
---|
47 | size = @rbuffer.size unless size
|
---|
48 | ret = @rbuffer[0, size]
|
---|
49 | @rbuffer[0, size] = ""
|
---|
50 | ret
|
---|
51 | end
|
---|
52 | end
|
---|
53 |
|
---|
54 | public
|
---|
55 |
|
---|
56 | def read(size=nil, buf=nil)
|
---|
57 | if size == 0
|
---|
58 | if buf
|
---|
59 | buf.clear
|
---|
60 | else
|
---|
61 | buf = ""
|
---|
62 | end
|
---|
63 | return @eof ? nil : buf
|
---|
64 | end
|
---|
65 | until @eof
|
---|
66 | break if size && size <= @rbuffer.size
|
---|
67 | fill_rbuff
|
---|
68 | end
|
---|
69 | ret = consume_rbuff(size) || ""
|
---|
70 | if buf
|
---|
71 | buf.replace(ret)
|
---|
72 | ret = buf
|
---|
73 | end
|
---|
74 | (size && ret.empty?) ? nil : ret
|
---|
75 | end
|
---|
76 |
|
---|
77 | def readpartial(maxlen, buf=nil)
|
---|
78 | if maxlen == 0
|
---|
79 | if buf
|
---|
80 | buf.clear
|
---|
81 | else
|
---|
82 | buf = ""
|
---|
83 | end
|
---|
84 | return @eof ? nil : buf
|
---|
85 | end
|
---|
86 | if @rbuffer.empty?
|
---|
87 | begin
|
---|
88 | return sysread(maxlen, buf)
|
---|
89 | rescue Errno::EAGAIN
|
---|
90 | retry
|
---|
91 | end
|
---|
92 | end
|
---|
93 | ret = consume_rbuff(maxlen)
|
---|
94 | if buf
|
---|
95 | buf.replace(ret)
|
---|
96 | ret = buf
|
---|
97 | end
|
---|
98 | raise EOFError if ret.empty?
|
---|
99 | ret
|
---|
100 | end
|
---|
101 |
|
---|
102 | def gets(eol=$/)
|
---|
103 | idx = @rbuffer.index(eol)
|
---|
104 | until @eof
|
---|
105 | break if idx
|
---|
106 | fill_rbuff
|
---|
107 | idx = @rbuffer.index(eol)
|
---|
108 | end
|
---|
109 | if eol.is_a?(Regexp)
|
---|
110 | size = idx ? idx+$&.size : nil
|
---|
111 | else
|
---|
112 | size = idx ? idx+eol.size : nil
|
---|
113 | end
|
---|
114 | consume_rbuff(size)
|
---|
115 | end
|
---|
116 |
|
---|
117 | def each(eol=$/)
|
---|
118 | while line = self.gets(eol)
|
---|
119 | yield line
|
---|
120 | end
|
---|
121 | end
|
---|
122 | alias each_line each
|
---|
123 |
|
---|
124 | def readlines(eol=$/)
|
---|
125 | ary = []
|
---|
126 | while line = self.gets(eol)
|
---|
127 | ary << line
|
---|
128 | end
|
---|
129 | ary
|
---|
130 | end
|
---|
131 |
|
---|
132 | def readline(eol=$/)
|
---|
133 | raise EOFError if eof?
|
---|
134 | gets(eol)
|
---|
135 | end
|
---|
136 |
|
---|
137 | def getc
|
---|
138 | c = read(1)
|
---|
139 | c ? c[0] : nil
|
---|
140 | end
|
---|
141 |
|
---|
142 | def each_byte
|
---|
143 | while c = getc
|
---|
144 | yield(c)
|
---|
145 | end
|
---|
146 | end
|
---|
147 |
|
---|
148 | def readchar
|
---|
149 | raise EOFError if eof?
|
---|
150 | getc
|
---|
151 | end
|
---|
152 |
|
---|
153 | def ungetc(c)
|
---|
154 | @rbuffer[0,0] = c.chr
|
---|
155 | end
|
---|
156 |
|
---|
157 | def eof?
|
---|
158 | fill_rbuff if !@eof && @rbuffer.empty?
|
---|
159 | @eof && @rbuffer.empty?
|
---|
160 | end
|
---|
161 | alias eof eof?
|
---|
162 |
|
---|
163 | #
|
---|
164 | # for writing.
|
---|
165 | #
|
---|
166 | private
|
---|
167 |
|
---|
168 | def do_write(s)
|
---|
169 | @wbuffer = "" unless defined? @wbuffer
|
---|
170 | @wbuffer << s
|
---|
171 | @sync ||= false
|
---|
172 | if @sync or @wbuffer.size > BLOCK_SIZE or idx = @wbuffer.rindex($/)
|
---|
173 | remain = idx ? idx + $/.size : @wbuffer.length
|
---|
174 | nwritten = 0
|
---|
175 | while remain > 0
|
---|
176 | str = @wbuffer[nwritten,remain]
|
---|
177 | begin
|
---|
178 | nwrote = syswrite(str)
|
---|
179 | rescue Errno::EAGAIN
|
---|
180 | retry
|
---|
181 | end
|
---|
182 | remain -= nwrote
|
---|
183 | nwritten += nwrote
|
---|
184 | end
|
---|
185 | @wbuffer[0,nwritten] = ""
|
---|
186 | end
|
---|
187 | end
|
---|
188 |
|
---|
189 | public
|
---|
190 |
|
---|
191 | def write(s)
|
---|
192 | do_write(s)
|
---|
193 | s.length
|
---|
194 | end
|
---|
195 |
|
---|
196 | def << (s)
|
---|
197 | do_write(s)
|
---|
198 | self
|
---|
199 | end
|
---|
200 |
|
---|
201 | def puts(*args)
|
---|
202 | s = ""
|
---|
203 | if args.empty?
|
---|
204 | s << "\n"
|
---|
205 | end
|
---|
206 | args.each{|arg|
|
---|
207 | s << arg.to_s
|
---|
208 | if $/ && /\n\z/ !~ s
|
---|
209 | s << "\n"
|
---|
210 | end
|
---|
211 | }
|
---|
212 | do_write(s)
|
---|
213 | nil
|
---|
214 | end
|
---|
215 |
|
---|
216 | def print(*args)
|
---|
217 | s = ""
|
---|
218 | args.each{ |arg| s << arg.to_s }
|
---|
219 | do_write(s)
|
---|
220 | nil
|
---|
221 | end
|
---|
222 |
|
---|
223 | def printf(s, *args)
|
---|
224 | do_write(s % args)
|
---|
225 | nil
|
---|
226 | end
|
---|
227 |
|
---|
228 | def flush
|
---|
229 | osync = @sync
|
---|
230 | @sync = true
|
---|
231 | do_write ""
|
---|
232 | @sync = osync
|
---|
233 | end
|
---|
234 |
|
---|
235 | def close
|
---|
236 | flush rescue nil
|
---|
237 | sysclose
|
---|
238 | end
|
---|
239 | end
|
---|