Class: Omnizip::Formats::Ole::RangesIOMigrateable

Inherits:
RangesIOResizeable show all
Defined in:
lib/omnizip/formats/ole/ranges_io.rb

Overview

RangesIO that can migrate between BAT and SBAT based on size

Instance Attribute Summary collapse

Attributes inherited from RangesIOResizeable

#bat

Attributes inherited from RangesIO

#io, #pos, #ranges, #size

Instance Method Summary collapse

Methods inherited from RangesIOResizeable

#free

Methods inherited from RangesIO

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

Constructor Details

#initialize(dirent, mode = "r") ⇒ RangesIOMigrateable

Initialize migrateable RangesIO

Parameters:

  • dirent (Dirent)

    Associated dirent

  • mode (String) (defaults to: "r")

    Open mode



294
295
296
297
298
299
# File 'lib/omnizip/formats/ole/ranges_io.rb', line 294

def initialize(dirent, mode = "r")
  @dirent = dirent
  bat = dirent.ole.bat_for_size(dirent.size)
  super(bat, first_block: dirent.first_block, size: dirent.size)
  @mode = mode
end

Instance Attribute Details

#direntDirent (readonly)

Returns Associated dirent.

Returns:

  • (Dirent)

    Associated dirent



288
289
290
# File 'lib/omnizip/formats/ole/ranges_io.rb', line 288

def dirent
  @dirent
end

Instance Method Details

#first_blockObject

Forward first_block to dirent (for reading)



336
337
338
# File 'lib/omnizip/formats/ole/ranges_io.rb', line 336

def first_block
  @first_block
end

#first_block=(val) ⇒ Object



340
341
342
343
# File 'lib/omnizip/formats/ole/ranges_io.rb', line 340

def first_block=(val)
  @first_block = val
  @dirent.first_block = val
end

#truncate(new_size) ⇒ Object

Truncate with BAT migration support

Parameters:

  • new_size (Integer)

    New size in bytes



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/omnizip/formats/ole/ranges_io.rb', line 309

def truncate(new_size)
  new_bat = @dirent.ole.bat_for_size(new_size)

  if new_bat.instance_of?(@bat.class)
    super
  else
    # BAT migration needed
    pos = [@pos, new_size].min
    self.pos = 0
    keep = read([@size, new_size].min)
    super(0)

    @bat = new_bat
    @io = new_bat.io
    super

    self.pos = 0
    write(keep)
    self.pos = pos
  end

  # Update dirent's first_block and size after any resize
  @dirent.first_block = @first_block
  @dirent.size = new_size
end

#writeable?Boolean

Check if writable

Returns:

  • (Boolean)


302
303
304
# File 'lib/omnizip/formats/ole/ranges_io.rb', line 302

def writeable?
  @mode.include?("w") || @mode.include?("a") || @mode.include?("+")
end