Class: MultiCompress::Writer

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

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.



174
175
176
177
178
179
# File 'lib/multi_compress.rb', line 174

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



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/multi_compress.rb', line 160

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



209
210
211
212
# File 'lib/multi_compress.rb', line 209

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

#closeObject



195
196
197
198
199
200
201
202
203
# File 'lib/multi_compress.rb', line 195

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)


205
206
207
# File 'lib/multi_compress.rb', line 205

def closed?
  @closed
end

#flushObject



188
189
190
191
192
193
# File 'lib/multi_compress.rb', line 188

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


223
224
225
226
# File 'lib/multi_compress.rb', line 223

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

#puts(*args) ⇒ Object



214
215
216
217
218
219
220
221
# File 'lib/multi_compress.rb', line 214

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

#write(data) ⇒ Object



181
182
183
184
185
186
# File 'lib/multi_compress.rb', line 181

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