Class: Zim::ClusterEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/zim/cluster.rb

Overview

entry in the cluster list

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f) ⇒ ClusterEntry

read a cluster from the file



10
11
12
13
14
15
16
# File 'lib/zim/cluster.rb', line 10

def initialize(f)
  @compression = f.read_int8

  @f = f
  @pos = f.tell
  @offsets = nil
end

Instance Attribute Details

#compressionObject (readonly)

compression of the cluster



7
8
9
# File 'lib/zim/cluster.rb', line 7

def compression
  @compression
end

Instance Method Details

#read_blob(blob) ⇒ Object

read the blob blob from the cluster



19
20
21
22
23
24
25
26
27
28
# File 'lib/zim/cluster.rb', line 19

def read_blob(blob)
  case @compression
  when 0, 1
    read_blob_uncompressed(nil, blob)
  when 4
    read_blob_lzma(blob)
  else
    raise UnknownCompression
  end
end

#read_blob_lzma(blob) ⇒ Object

:nodoc:



55
56
57
58
59
# File 'lib/zim/cluster.rb', line 55

def read_blob_lzma(blob) # :nodoc:
  @f.seek(@pos)
  io = StringIO.new(XZ.decompress_stream(@f.file))
  read_blob_uncompressed(io, blob, false)
end

#read_blob_uncompressed(io, blob, seek = true) ⇒ Object

:nodoc:

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zim/cluster.rb', line 30

def read_blob_uncompressed(io, blob, seek = true) # :nodoc:
  if @offsets.nil?
    @offsets = Array.new

    @f.seek(@pos, io) if seek

    off = @f.read_int32(nil, io)
    count = (off >> 2) - 1

    @offsets << off
    count.times do
      @offsets << @f.read_int32(nil, io)
    end
  end

  raise InvalidBlobNumber if (blob < 0) || (blob >= (@offsets.count - 1))

  off = @offsets[blob]
  next_off = @offsets[blob + 1]
  len = next_off - off
  off += @pos if seek

  @f.read_str(len, off, io)
end