class Ms::Data::LazyIO
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.
Constants
- BASE_64
- LITTLE_ENDIAN_DOUBLE
- LITTLE_ENDIAN_FLOAT
- NETWORK_DOUBLE
- NETWORK_FLOAT
Attributes
boolean: whether the peaks string is zlib compressed
Indicates a decoding format, may be false to unpack string without decoding.
The IO
from which string is read
The number of bytes to be read from io when evaluating string
The start index for reading string
Indicates the unpacking format
Public Class Methods
# 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
Returns the unpacking code for the given precision (32 or 64-bit) and network order (true for big-endian).
# 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
Public Instance Methods
Resets the cached array (returned by to_a
) so that the array will be re-read from io.
# File lib/ms/data/lazy_io.rb, line 67 def reset @array = nil end
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.
# 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
Reads string and unpacks using decode_format
and unpack_code. The array is cached internally; to re-read the array, use reset.
# 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