module Zip::IOExtras::AbstractInputStream

Implements many of the convenience methods of IO such as gets, getc, read, readline and readlines depends on: input_finished?, produce_input and read

Attributes

lineno[RW]

Returns (or sets) the current line number in the decompressed (possibly decrypted) data stream. See the Line Number documentation for the IO class for more information.

pos[R]

Returns the current position (in bytes) in the decompressed (possibly decrypted) data stream.

Public Instance Methods

each(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false) { |line| ... } click to toggle source

Calls the block with each remaining line read from the stream. Does nothing if already at end-of-stream. See the Line IO documentation in the IO class for more information.

With no arguments given, reads lines as determined by line separator ‘$/`. With only string argument `sep` given, reads lines as determined by line separator `sep`. See the Line Separator documentation in the IO class for more information. The two special values for `sep` (`nil` and `“”`) are honoured.

With only integer argument ‘limit` given, limits the number of bytes in each line; see the Line Limit documentation in the IO class for more information.

With arguments ‘sep` and `limit` given, combines the two behaviors.

Optional keyword argument ‘chomp` specifies whether line separators are to be omitted.

Returns an ‘Enumerator` if no block is given.

# File lib/zip/ioextras/abstract_input_stream.rb, line 189
def each(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false)
  return to_enum(:each, sep, limit, chomp: chomp) unless block_given?

  while (line = gets(sep, limit, chomp: chomp))
    yield line
  end
end
Also aliased as: each_line
each_line(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false)
Alias for: each
eof()

Alias for compatibility. Remove for version 4.

Alias for: eof?
eof?() click to toggle source

Returns ‘true` if the stream is positioned at its end, `false` otherwise. See Position documentation in the IO class for more information.

# File lib/zip/ioextras/abstract_input_stream.rb, line 202
def eof?
  @output_buffer.empty? && input_finished?
end
Also aliased as: eof
gets(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false) click to toggle source

Reads and returns a line from the stream. See the Line IO documentation in the IO class for more information.

With no arguments given, returns the next line as determined by line separator ‘$/`, or `nil` if none.

With only string argument ‘sep` given, returns the next line as determined by line separator `sep`, or `nil` if none. See the Line Separator documentation in the IO class for more information. The two special values for `sep` (`nil` and `“”`) are honoured.

With only integer argument ‘limit` given, limits the number of bytes in the line; see the Line Limit documentation in the IO class for more information.

With arguments ‘sep` and `limit` given, combines the two behaviors.

Optional keyword argument ‘chomp` specifies whether line separators are to be omitted.

# File lib/zip/ioextras/abstract_input_stream.rb, line 112
def gets(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false)
  if sep.nil?
    return nil if eof?

    @lineno = @lineno.next
    return read(limit)
  end

  if sep.respond_to?(:to_int)
    limit = sep.to_int
    sep   = $INPUT_RECORD_SEPARATOR
  elsif sep&.empty?
    sep = "#{$INPUT_RECORD_SEPARATOR}#{$INPUT_RECORD_SEPARATOR}"
  end

  buffer_index = 0
  while (sep_index = @output_buffer.index(sep, buffer_index)).nil?
    break if limit && @output_buffer.bytesize >= limit

    if input_finished?
      return nil if @output_buffer.empty?

      @lineno = @lineno.next
      @pos += @output_buffer.bytesize
      return @output_buffer.slice!(0..)
    end

    buffer_index = [buffer_index, @output_buffer.bytesize - sep.bytesize].max
    @output_buffer << produce_input
  end

  limit ||= @output_buffer.bytesize
  cut_index = sep_index ? [sep_index + sep.bytesize, limit].min : limit
  @lineno = @lineno.next
  @pos += cut_index
  chomp ? @output_buffer.slice!(0, cut_index).chomp(sep) : @output_buffer.slice!(0, cut_index)
end
read(maxlen = nil, out_string = nil) click to toggle source

Reads bytes from the stream decompressed (possibly decrypted) data stream. If ‘maxlen` is `nil`, reads all bytes; otherwise, reads up to `maxlen` bytes. If `maxlen` is zero, returns an empty string.

Returns a string (either a new string or the given ‘out_string`) containing the bytes read. The string’s encoding is the unchanged encoding of ‘out_string`, if `out_string` is given; `ASCII-8BIT`, otherwise.

# File lib/zip/ioextras/abstract_input_stream.rb, line 36
def read(maxlen = nil, out_string = nil) # rubocop:disable Metrics/PerceivedComplexity
  return (maxlen.nil? || maxlen.zero? ? '' : nil) if eof?

  tbuf = if @output_buffer.bytesize > 0
           if maxlen && maxlen <= @output_buffer.bytesize
             @output_buffer.slice!(0, maxlen)
           else
             maxlen -= @output_buffer.bytesize if maxlen
             rbuf = produce_input(maxlen)
             out  = @output_buffer
             out << rbuf if rbuf
             @output_buffer = +''.b
             out
           end
         else
           produce_input(maxlen)
         end

  if tbuf.nil? || tbuf.empty?
    return nil if maxlen&.positive?

    return ''
  end

  @pos += tbuf.length

  if out_string.nil?
    tbuf.force_encoding(Encoding::ASCII_8BIT)
  else
    encoding = out_string.encoding
    out_string.replace(tbuf).force_encoding(encoding)
  end
end
readline(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false) click to toggle source

Reads a line as with gets, but raises ‘EOFError` if already at end-of-stream.

Optional keyword argument ‘chomp` specifies whether line separators are to be omitted.

# File lib/zip/ioextras/abstract_input_stream.rb, line 163
def readline(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false)
  raise EOFError if eof?

  gets(sep, limit, chomp: chomp)
end
readlines(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false) click to toggle source

Reads and returns all remaining lines from the stream. See the Line IO documentation in the IO class for more information.

With no arguments given, returns lines as determined by line separator ‘$/`, or `nil` if none.

With only string argument ‘sep` given, returns lines as determined by line separator `sep`, or `nil` if none. See the Line Separator documentation in the IO class for more information. The two special values for `sep` (`nil` and `“”`) are honoured.

With only integer argument ‘limit` given, limits the number of bytes in each line; see the Line Limit documentation in the IO class for more information.

With arguments ‘sep` and `limit` given, combines the two behaviors.

Optional keyword argument ‘chomp` specifies whether line separators are to be omitted.

# File lib/zip/ioextras/abstract_input_stream.rb, line 89
def readlines(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false)
  each(sep, limit, chomp: chomp).to_a
end