Class: Ms::Data::LazyIO

Inherits:
Object
  • Object
show all
Defined in:
lib/ms/data/lazy_io.rb

Overview

LazyIO represents data to be lazily read from an IO. To read the data from the IO, either string or to_a may be called (to_a unpacks the string into an array using the decode_format and unpack_format).

LazyIO is a suitable unresolved_data source for Ms::Data formats.

Direct Known Subclasses

LazyString

Constant Summary collapse

NETWORK_FLOAT =
'g*'
NETWORK_DOUBLE =
'G*'
LITTLE_ENDIAN_FLOAT =
'e*'
LITTLE_ENDIAN_DOUBLE =
'E*'
BASE_64 =
'm'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, start_index = io.pos, num_bytes = nil, unpack_format = NETWORK_FLOAT, compressed = false, decode_format = BASE_64) ⇒ LazyIO



49
50
51
52
53
54
55
56
# File 'lib/ms/data/lazy_io.rb', line 49

def initialize(io, start_index=io.pos, num_bytes=nil, unpack_format=NETWORK_FLOAT, compressed=false, decode_format=BASE_64)
  @io = io
  @start_index = start_index
  @num_bytes = num_bytes
  @unpack_format = unpack_format
  @compressed = compressed
  @decode_format = decode_format
end

Instance Attribute Details

#compressedObject (readonly)

boolean: whether the peaks string is zlib compressed



47
48
49
# File 'lib/ms/data/lazy_io.rb', line 47

def compressed
  @compressed
end

#decode_formatObject (readonly)

Indicates a decoding format, may be false to unpack string without decoding.



44
45
46
# File 'lib/ms/data/lazy_io.rb', line 44

def decode_format
  @decode_format
end

#ioObject (readonly)

The IO from which string is read



31
32
33
# File 'lib/ms/data/lazy_io.rb', line 31

def io
  @io
end

#num_bytesObject (readonly)

The number of bytes to be read from io when evaluating string



37
38
39
# File 'lib/ms/data/lazy_io.rb', line 37

def num_bytes
  @num_bytes
end

#start_indexObject (readonly)

The start index for reading string



34
35
36
# File 'lib/ms/data/lazy_io.rb', line 34

def start_index
  @start_index
end

#unpack_formatObject (readonly)

Indicates the unpacking format



40
41
42
# File 'lib/ms/data/lazy_io.rb', line 40

def unpack_format
  @unpack_format
end

Class Method Details

.unpack_code(precision, network_order) ⇒ Object

Returns the unpacking code for the given precision (32 or 64-bit) and network order (true for big-endian).



21
22
23
24
25
26
27
# File 'lib/ms/data/lazy_io.rb', line 21

def unpack_code(precision, network_order)
  case precision
  when 32 then network_order ? NETWORK_FLOAT : LITTLE_ENDIAN_FLOAT
  when 64 then network_order ? NETWORK_DOUBLE : LITTLE_ENDIAN_DOUBLE
  else raise ArgumentError, "unknown precision (should be 32 or 64): #{precision}"
  end
end

Instance Method Details

#resetObject

Resets the cached array (returned by to_a) so that the array will be re-read from io.



67
68
69
# File 'lib/ms/data/lazy_io.rb', line 67

def reset
  @array = nil
end

#stringObject

Positions io at start_index and reads a string of num_bytes length. The string is newly read from io each time string is called.



60
61
62
63
# File 'lib/ms/data/lazy_io.rb', line 60

def string
  io.pos = start_index unless io.pos == start_index
  io.read(num_bytes)
end

#to_aObject

Reads string and unpacks using decode_format and unpack_code. The array is cached internally; to re-read the array, use reset.



73
74
75
76
77
78
79
80
81
82
# File 'lib/ms/data/lazy_io.rb', line 73

def to_a
  return @array if @array
  decoded = decode_format ?  string.unpack(decode_format)[0] : string
  if string.size == 0
    []
  else
    uncompressed = @compressed ? Zlib::Inflate.inflate(decoded) : decoded
    uncompressed.unpack(unpack_format)
  end
end