Class: LZ4::StreamDecoder

Inherits:
Object
  • Object
show all
Includes:
BasicStream
Defined in:
lib/extlz4/oldstream.rb

Overview

LZ4 ストリームを伸張するためのクラスです。

Constant Summary

Constants included from BasicStream

BasicStream::BLOCK_CHECKSUM, BasicStream::BLOCK_INDEPENDENCY, BasicStream::BLOCK_MAXIMUM_SIZES, BasicStream::LITERAL_DATA_BLOCK_FLAG, BasicStream::MAGIC_NUMBER, BasicStream::MAGIC_NUMBER_LEGACY, BasicStream::PRESET_DICTIONARY, BasicStream::STREAM_CHECKSUM, BasicStream::STREAM_SIZE, BasicStream::VERSION_NUMBER, BasicStream::VERSION_NUMBER_MASK

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ StreamDecoder

Returns a new instance of StreamDecoder.



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/extlz4/oldstream.rb', line 359

def initialize(io)
  magic = io.read(4).unpack("V")[0]
  case magic
  when MAGIC_NUMBER
    sf = io.getbyte
    @version = (sf >> 6) & 0x03
    raise "stream header error - wrong version number" unless @version == 0x01
    @blockindependence = ((sf >> 5) & 0x01) == 0 ? false : true
    @blockchecksum = ((sf >> 4) & 0x01) == 0 ? false : true
    streamsize = ((sf >> 3) & 0x01) == 0 ? false : true
    @streamchecksum = ((sf >> 2) & 0x01) == 0 ? false : true
    # reserved = (sf >> 1) & 0x01
    presetdict = ((sf >> 0) & 0x01) == 0 ? false : true

    bd = io.getbyte
    # reserved = (bd >> 7) & 0x01
    blockmax = (bd >> 4) & 0x07
    # reserved = (bd >> 0) & 0x0f

    @blockmaximum = BLOCK_MAXIMUM_SIZES[blockmax]
    raise Error, "stream header error - wrong block maximum size (#{blockmax} for 4 .. 7)" unless @blockmaximum

    @streamsize = io.read(8).unpack("Q<")[0] if streamsize
    @presetdict = io.read(4).unpack("V")[0] if presetdict

    headerchecksum = io.getbyte

    if @blockindependence
      @decoder = LZ4.method(:block_decode)
    else
      @decoder = LZ4::BlockDecoder.new.method(:update)
    end
  when MAGIC_NUMBER_LEGACY
    @version = -1
    @blockindependence = true
    @blockchecksum = false
    @streamchecksum = false
    @blockmaximum = 1 << 23 # 8 MiB
    @streamsize = nil
    @presetdict = nil
    @decoder = LZ4.method(:block_decode)
  else
    raise Error, "stream header error - wrong magic number"
  end

  @io = io
  @pos = 0

  @readbuf = "".b
  @decodebuf = "".b
end

Instance Attribute Details

#blockchecksumObject (readonly)

Returns the value of attribute blockchecksum.



353
354
355
# File 'lib/extlz4/oldstream.rb', line 353

def blockchecksum
  @blockchecksum
end

#blockindependenceObject (readonly)

Returns the value of attribute blockindependence.



352
353
354
# File 'lib/extlz4/oldstream.rb', line 352

def blockindependence
  @blockindependence
end

#blockmaximumObject (readonly)

Returns the value of attribute blockmaximum.



355
356
357
# File 'lib/extlz4/oldstream.rb', line 355

def blockmaximum
  @blockmaximum
end

#presetdictObject (readonly)

Returns the value of attribute presetdict.



357
358
359
# File 'lib/extlz4/oldstream.rb', line 357

def presetdict
  @presetdict
end

#streamchecksumObject (readonly)

Returns the value of attribute streamchecksum.



354
355
356
# File 'lib/extlz4/oldstream.rb', line 354

def streamchecksum
  @streamchecksum
end

#streamsizeObject (readonly)

Returns the value of attribute streamsize.



356
357
358
# File 'lib/extlz4/oldstream.rb', line 356

def streamsize
  @streamsize
end

#versionObject (readonly)

Returns the value of attribute version.



351
352
353
# File 'lib/extlz4/oldstream.rb', line 351

def version
  @version
end

Instance Method Details

#closeObject



411
412
413
# File 'lib/extlz4/oldstream.rb', line 411

def close
  @io = nil
end

#eofObject Also known as: eof?



439
440
441
# File 'lib/extlz4/oldstream.rb', line 439

def eof
  !@pos
end

#getbyteObject



434
435
436
437
# File 'lib/extlz4/oldstream.rb', line 434

def getbyte
  w = read(1) or return nil
  w.getbyte(0)
end

#posObject

Raises:

  • (NotImplementedError)


453
454
455
# File 'lib/extlz4/oldstream.rb', line 453

def pos
  raise NotImplementedError
end

#pos=(pos) ⇒ Object

Raises:

  • (NotImplementedError)


457
458
459
# File 'lib/extlz4/oldstream.rb', line 457

def pos=(pos)
  raise NotImplementedError
end

#read(*args) ⇒ Object

call-seq:

read -> string or nil
read(size) -> string or nil
read(size, dest) -> string or nil


421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/extlz4/oldstream.rb', line 421

def read(*args)
  case args.size
  when 0
    read_all
  when 1
    read_part(args[0].to_i, "")
  when 2
    read_part(args[0].to_i, args[1])
  else
    raise ArgumentError, "wrong number of arguments (#{args.size} for 0 .. 2)"
  end
end

#seek(off, cur) ⇒ Object

Raises:

  • (NotImplementedError)


449
450
451
# File 'lib/extlz4/oldstream.rb', line 449

def seek(off, cur)
  raise NotImplementedError
end

#tellObject

Raises:

  • (NotImplementedError)


445
446
447
# File 'lib/extlz4/oldstream.rb', line 445

def tell
  raise NotImplementedError
end