Method: Zip::IOExtras::AbstractInputStream#gets

Defined in:
lib/zip/ioextras/abstract_input_stream.rb

#gets(a_sep_string = $INPUT_RECORD_SEPARATOR, number_of_bytes = nil) ⇒ Object



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
85
86
87
88
89
90
91
92
93
# File 'lib/zip/ioextras/abstract_input_stream.rb', line 60

def gets(a_sep_string = $INPUT_RECORD_SEPARATOR, 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    = $INPUT_RECORD_SEPARATOR
  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 = "#{$INPUT_RECORD_SEPARATOR}#{$INPUT_RECORD_SEPARATOR}" 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
    return @output_buffer.empty? ? nil : flush if input_finished?

    @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
  @output_buffer.slice!(0...sep_index)
end