Class: IOBlockReader::DataBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/ioblockreader/datablock.rb

Overview

Class defining a data block

Constant Summary collapse

@@access_time_sequence =

Use a Fixnum sequence instead of real Time values for last_access_time for performance reasons

0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ DataBlock

Constructor

Parameters
  • io (IO): IO to read from



25
26
27
28
29
30
31
# File 'lib/ioblockreader/datablock.rb', line 25

def initialize(io)
  @io = io
  @offset = nil
  @last_access_time = nil
  @data = ''
  @data.force_encoding(@io.external_encoding) if (@data.respond_to?(:force_encoding))
end

Instance Attribute Details

#dataObject (readonly)

Data contained in this block

_String_


19
20
21
# File 'lib/ioblockreader/datablock.rb', line 19

def data
  @data
end

#last_access_timeObject (readonly)

Timestamp indicating when this block has been touched (created or done with touch method)

_Fixnum_


15
16
17
# File 'lib/ioblockreader/datablock.rb', line 15

def last_access_time
  @last_access_time
end

#offsetObject (readonly)

Offset of this block, in bytes

_Fixnum_


11
12
13
# File 'lib/ioblockreader/datablock.rb', line 11

def offset
  @offset
end

Instance Method Details

#fill(offset, size) ⇒ Object

Fill the data block for a given IO

Parameters
  • offset (Fixnum): Offset of this block in the IO

  • size (Fixnum): Size of the block to be read



38
39
40
41
42
43
44
45
46
47
# File 'lib/ioblockreader/datablock.rb', line 38

def fill(offset, size)
  @offset = offset
  @last_access_time = @@access_time_sequence
  @@access_time_sequence += 1
  #puts "[IOBlockReader] - Read #{size} bytes @#{@offset} in datablock ##{self.object_id}"
  @io.seek(@offset)
  @io.read(size, @data)
  #puts "[IOBlockReader] - Data read: #{@data.inspect}"
  @last_block = @io.eof?
end

#last_block?Boolean

Is this block the last of its IO stream?

Result:

  • Boolean: Is this block the last of its IO stream?



53
54
55
# File 'lib/ioblockreader/datablock.rb', line 53

def last_block?
  return @last_block
end

#to_sObject

Get a string representation of this block. This is mainly used for debugging purposes.

Result
  • String: String representation



68
69
70
# File 'lib/ioblockreader/datablock.rb', line 68

def to_s
  return "[##{self.object_id}: @#{@offset} (last access: #{@last_access_time})#{@last_block ? ' (last block)' : ''}]"
end

#touchObject

Update the last access time



58
59
60
61
# File 'lib/ioblockreader/datablock.rb', line 58

def touch
  @last_access_time = @@access_time_sequence
  @@access_time_sequence += 1
end