Sha256: e6f456b7293523bb1dce76bce6a57d47ae861371f13c905e514417af0ca869de

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

# encoding: utf-8

require 'zlib'
require 'mimemagic'

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)
          magic = MimeMagic.by_magic(::File.open(filename))
          magic ? magic.subtype == "gzip" : false
        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

5 entries across 5 versions & 1 rubygems

Version Path
logstash-input-google_cloud_storage-0.15.0-java lib/logstash/inputs/cloud_storage/file_reader.rb
logstash-input-google_cloud_storage-0.14.0-java lib/logstash/inputs/cloud_storage/file_reader.rb
logstash-input-google_cloud_storage-0.13.0-java lib/logstash/inputs/cloud_storage/file_reader.rb
logstash-input-google_cloud_storage-0.12.0-java lib/logstash/inputs/cloud_storage/file_reader.rb
logstash-input-google_cloud_storage-0.11.1-java lib/logstash/inputs/cloud_storage/file_reader.rb