Class: Omnizip::Formats::Ole::RangesIOResizeable

Inherits:
RangesIO
  • Object
show all
Defined in:
lib/omnizip/formats/ole/ranges_io.rb

Overview

Resizeable RangesIO backed by AllocationTable

Direct Known Subclasses

RangesIOMigrateable

Instance Attribute Summary collapse

Attributes inherited from RangesIO

#io, #pos, #ranges, #size

Instance Method Summary collapse

Methods inherited from RangesIO

#close, #eof?, #inspect, open, #read, #rewind, #write

Constructor Details

#initialize(bat, first_block:, size: nil) ⇒ RangesIOResizeable

Initialize resizeable RangesIO

Parameters:

  • bat (AllocationTable)

    Allocation table

  • first_block (Integer)

    First block index

  • size (Integer, nil) (defaults to: nil)

    Optional size



253
254
255
256
257
258
259
# File 'lib/omnizip/formats/ole/ranges_io.rb', line 253

def initialize(bat, first_block:, size: nil)
  @bat = bat
  @first_block = first_block
  @blocks = first_block == Constants::EOC ? [] : bat.chain(first_block)

  super(bat.io, bat.ranges(@blocks, size))
end

Instance Attribute Details

#batAllocationTable (readonly)

Returns Backing allocation table.

Returns:



243
244
245
# File 'lib/omnizip/formats/ole/ranges_io.rb', line 243

def bat
  @bat
end

#first_blockInteger

Returns First block index.

Returns:

  • (Integer)

    First block index



246
247
248
# File 'lib/omnizip/formats/ole/ranges_io.rb', line 246

def first_block
  @first_block
end

Instance Method Details

#freevoid

This method returns an undefined value.

Free resources to prevent memory leaks



278
279
280
281
282
# File 'lib/omnizip/formats/ole/ranges_io.rb', line 278

def free
  @bat = nil
  @blocks = nil
  super
end

#truncate(new_size) ⇒ Object

Truncate to new size

Parameters:

  • new_size (Integer)

    New size in bytes



264
265
266
267
268
269
270
271
272
273
# File 'lib/omnizip/formats/ole/ranges_io.rb', line 264

def truncate(new_size)
  @bat.resize_chain(@blocks, new_size)
  @pos = new_size if @pos > new_size
  self.ranges = @bat.ranges(@blocks, new_size)
  @first_block = @blocks.empty? ? Constants::EOC : @blocks.first

  # Grow underlying IO if needed
  max_pos = @ranges.map { |pos, len| pos + len }.max || 0
  @io.truncate(max_pos) if max_pos > @io.size
end