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
BUFFER_COMPACT_THRESHOLD =
64 * 1024

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Reader.



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

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

Class Method Details

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



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

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



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

def close
  return if @closed

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

#closed?Boolean

Returns:

  • (Boolean)


305
306
307
# File 'lib/multi_compress.rb', line 305

def closed?
  @closed
end

#each_chunk(size) ⇒ Object



321
322
323
324
325
326
327
# File 'lib/multi_compress.rb', line 321

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

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

#each_lineObject



313
314
315
316
317
318
319
# File 'lib/multi_compress.rb', line 313

def each_line
  return enum_for(:each_line) unless block_given?

  while (line = gets)
    yield line
  end
end

#eof?Boolean

Returns:

  • (Boolean)


309
310
311
# File 'lib/multi_compress.rb', line 309

def eof?
  @eof && buffer_empty?
end

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



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

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

  fill_buffer_until { buffer_includes?(separator) }

  return extract_line(separator) if buffer_includes?(separator)
  return consume_buffer unless buffer_empty?

  nil
end

#read(length = nil) ⇒ Object



277
278
279
280
281
282
# File 'lib/multi_compress.rb', line 277

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

  read_exactly(length)
end

#readlineObject



329
330
331
# File 'lib/multi_compress.rb', line 329

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