Class: MultiCompress::Reader

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

Constant Summary collapse

CHUNK_SIZE =
8192

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, algo: nil, dictionary: nil, **opts) ⇒ Reader

Returns a new instance of Reader.



266
267
268
269
270
271
272
273
# File 'lib/multi_compress.rb', line 266

def initialize(io, algo: nil, dictionary: nil, **opts)
  @io       = io
  @inflater = Inflater.new(algo: algo, dictionary: dictionary, **opts)
  @closed   = false
  @owned_io = false
  @buffer   = +""
  @eof      = false
end

Class Method Details

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



252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/multi_compress.rb', line 252

def self.open(path_or_io, algo: nil, dictionary: nil, **opts, &block)
  io, algo, owned = resolve_io(path_or_io, algo, mode: "rb")
  reader = new(io, algo: algo, dictionary: dictionary, **opts)
  reader.instance_variable_set(:@owned_io, owned)

  return reader unless block

  begin
    yield reader
  ensure
    reader.close
  end
end

Instance Method Details

#closeObject



294
295
296
297
298
299
300
301
# File 'lib/multi_compress.rb', line 294

def close
  return if @closed

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

#closed?Boolean

Returns:

  • (Boolean)


303
304
305
# File 'lib/multi_compress.rb', line 303

def closed?
  @closed
end

#each_chunk(size) ⇒ Object



319
320
321
322
323
324
325
# File 'lib/multi_compress.rb', line 319

def each_chunk(size)
  return enum_for(:each_chunk, size) unless block_given?

  while (chunk = read(size))
    yield chunk
  end
end

#each_lineObject



311
312
313
314
315
316
317
# File 'lib/multi_compress.rb', line 311

def each_line
  return enum_for(:each_line) unless block_given?

  while (line = gets)
    yield line
  end
end

#eof?Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/multi_compress.rb', line 307

def eof?
  @eof && @buffer.empty?
end

#gets(separator = "\n") ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
# File 'lib/multi_compress.rb', line 282

def gets(separator = "\n")
  ensure_open!
  return nil if @eof && @buffer.empty?

  fill_buffer_until { @buffer.include?(separator) }

  return extract_line(separator) if @buffer.include?(separator)
  return consume_buffer unless @buffer.empty?

  nil
end

#read(length = nil) ⇒ Object



275
276
277
278
279
280
# File 'lib/multi_compress.rb', line 275

def read(length = nil)
  ensure_open!
  return read_all if length.nil?

  read_exactly(length)
end

#readlineObject



327
328
329
# File 'lib/multi_compress.rb', line 327

def readline
  gets || raise(EOFError, "end of file reached")
end