Module: Zip::IOExtras::AbstractInputStream
- Includes:
- Enumerable, FakeIO
- Included in:
- Zip::InputStream, NullInputStream
- Defined in:
- lib/zip/ioextras/abstract_input_stream.rb
Overview
Implements many of the convenience methods of IO such as gets, getc, readline and readlines depends on: input_finished?, produce_input and read
Instance Attribute Summary collapse
-
#lineno ⇒ Object
Returns the value of attribute lineno.
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
Instance Method Summary collapse
- #each_line(a_sep_string = $/) ⇒ Object (also: #each)
- #flush ⇒ Object
- #gets(a_sep_string = $/, number_of_bytes = nil) ⇒ Object
- #initialize ⇒ Object
- #read(number_of_bytes = nil, buf = nil) ⇒ Object
- #readline(a_sep_string = $/) ⇒ Object
- #readlines(a_sep_string = $/) ⇒ Object
Methods included from FakeIO
Instance Attribute Details
#lineno ⇒ Object
Returns the value of attribute lineno.
17 18 19 |
# File 'lib/zip/ioextras/abstract_input_stream.rb', line 17 def lineno @lineno end |
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
18 19 20 |
# File 'lib/zip/ioextras/abstract_input_stream.rb', line 18 def pos @pos end |
Instance Method Details
#each_line(a_sep_string = $/) ⇒ Object Also known as: each
98 99 100 101 102 103 |
# File 'lib/zip/ioextras/abstract_input_stream.rb', line 98 def each_line(a_sep_string = $/) while true yield readline(a_sep_string) end rescue EOFError end |
#flush ⇒ Object
86 87 88 89 90 |
# File 'lib/zip/ioextras/abstract_input_stream.rb', line 86 def flush ret_val = @output_buffer @output_buffer = '' ret_val end |
#gets(a_sep_string = $/, number_of_bytes = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/zip/ioextras/abstract_input_stream.rb', line 54 def gets(a_sep_string = $/, number_of_bytes = nil) @lineno = @lineno.next if number_of_bytes.respond_to?(:to_int) number_of_bytes = number_of_bytes.to_int a_sep_string = a_sep_string.to_str if a_sep_string elsif a_sep_string.respond_to?(:to_int) number_of_bytes = a_sep_string.to_int a_sep_string = $/ else number_of_bytes = nil a_sep_string = a_sep_string.to_str if a_sep_string end return read(number_of_bytes) if a_sep_string.nil? a_sep_string = "#{$/}#{$/}" if a_sep_string.empty? buffer_index = 0 over_limit = (number_of_bytes && @output_buffer.bytesize >= number_of_bytes) while (match_index = @output_buffer.index(a_sep_string, buffer_index)).nil? && !over_limit buffer_index = [buffer_index, @output_buffer.bytesize - a_sep_string.bytesize].max if input_finished? return @output_buffer.empty? ? nil : flush end @output_buffer << produce_input over_limit = (number_of_bytes && @output_buffer.bytesize >= number_of_bytes) end sep_index = [match_index + a_sep_string.bytesize, number_of_bytes || @output_buffer.bytesize].min @pos += sep_index return @output_buffer.slice!(0...sep_index) end |
#initialize ⇒ Object
10 11 12 13 14 15 |
# File 'lib/zip/ioextras/abstract_input_stream.rb', line 10 def initialize super @lineno = 0 @pos = 0 @output_buffer = '' end |
#read(number_of_bytes = nil, buf = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/zip/ioextras/abstract_input_stream.rb', line 20 def read(number_of_bytes = nil, buf = nil) tbuf = if @output_buffer.bytesize > 0 if number_of_bytes <= @output_buffer.bytesize @output_buffer.slice!(0, number_of_bytes) else number_of_bytes -= @output_buffer.bytesize if number_of_bytes rbuf = sysread(number_of_bytes, buf) out = @output_buffer out << rbuf if rbuf @output_buffer = '' out end else sysread(number_of_bytes, buf) end @pos += tbuf.length return nil unless tbuf if buf buf.replace(tbuf) else buf = tbuf end buf end |
#readline(a_sep_string = $/) ⇒ Object
92 93 94 95 96 |
# File 'lib/zip/ioextras/abstract_input_stream.rb', line 92 def readline(a_sep_string = $/) ret_val = gets(a_sep_string) raise EOFError unless ret_val ret_val end |
#readlines(a_sep_string = $/) ⇒ Object
48 49 50 51 52 |
# File 'lib/zip/ioextras/abstract_input_stream.rb', line 48 def readlines(a_sep_string = $/) ret_val = [] each_line(a_sep_string) { |line| ret_val << line } ret_val end |