Sha256: 4e55e6b73a7dc96a5b5b04cfe63e19285e03889f341437152bbd6b2a22ff344d

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# encoding: utf-8

require 'zlib'

module LogStash
  module Inputs
    module CloudStorage
      # FileReader provides a unified way to read different types of log files
      # with predictable callbacks.
      class FileReader
        # read_lines reads lines from a file one at a time, optionally decoding
        # the file as gzip if decode_gzip is true.
        #
        # Handles files with both UNIX and Windows line endings.
        def self.read_lines(filename, decode_gzip, &block)
          if decode_gzip && gzip?(filename)
            read_gzip_lines(filename, &block)
          else
            read_plain_lines(filename, &block)
          end
        end

        # gzip? returns true if the given filename has a gzip file extension.
        def self.gzip?(filename)
          filename.end_with? '.gz'
        end

        def self.read_plain_lines(filename, &block)
          line_num = 1
          ::File.open(filename).each do |line|
            block.call(line, line_num)
            line_num += 1
          end
        end

        def self.read_gzip_lines(filename, &block)
          line_num = 1
          Zlib::GzipReader.open(filename).each_line do |line|
            block.call(line, line_num)
            line_num += 1
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logstash-input-google_cloud_storage-0.10.0-java lib/logstash/inputs/cloud_storage/file_reader.rb