Class: MultiCompress::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_compress.rb,
ext/multi_compress/multi_compress.c

Overview

Streaming compressed writer.

Supports block form via Writer.open or manual lifecycle management.

Constant Summary collapse

CHUNK_BUFFER_SIZE =
8192

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, algo: :zstd, level: nil, dictionary: nil) ⇒ Writer

Returns a new instance of Writer.



87
88
89
90
91
92
# File 'lib/multi_compress.rb', line 87

def initialize(io, algo: :zstd, level: nil, dictionary: nil)
  @io       = io
  @deflater = Deflater.new(algo: algo, level: level, dictionary: dictionary)
  @closed   = false
  @owned_io = false
end

Class Method Details

.open(path_or_io, algo: nil, level: nil, dictionary: nil, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/multi_compress.rb', line 73

def self.open(path_or_io, algo: nil, level: nil, dictionary: nil, &block)
  io, algo, owned = resolve_io(path_or_io, algo, mode: "wb")
  writer = new(io, algo: algo || :zstd, level: level, dictionary: dictionary)
  writer.instance_variable_set(:@owned_io, owned)

  return writer unless block

  begin
    yield writer
  ensure
    writer.close
  end
end

Instance Method Details

#<<(data) ⇒ Object



122
123
124
125
# File 'lib/multi_compress.rb', line 122

def <<(data)
  write(data)
  self
end

#closeObject



108
109
110
111
112
113
114
115
116
# File 'lib/multi_compress.rb', line 108

def close
  return if @closed

  flush_compressed(@deflater.finish)
  @deflater.close
  @io.close if @owned_io && @io.respond_to?(:close)
  @closed = true
  nil
end

#closed?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/multi_compress.rb', line 118

def closed?
  @closed
end

#flushObject



101
102
103
104
105
106
# File 'lib/multi_compress.rb', line 101

def flush
  ensure_open!
  flush_compressed(@deflater.flush)
  @io.flush if @io.respond_to?(:flush)
  self
end


136
137
138
139
# File 'lib/multi_compress.rb', line 136

def print(*args)
  args.each { |arg| write(arg.to_s) }
  nil
end

#puts(*args) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/multi_compress.rb', line 127

def puts(*args)
  args.each do |arg|
    str = arg.to_s
    write(str)
    write("\n") unless str.end_with?("\n")
  end
  nil
end

#write(data) ⇒ Object



94
95
96
97
98
99
# File 'lib/multi_compress.rb', line 94

def write(data)
  ensure_open!
  bytes = data.to_s
  flush_compressed(@deflater.write(bytes))
  bytes.bytesize
end