Module: FormatParser::IOUtils
- Included in:
- AACParser, AIFFParser, ARWParser, BMPParser, CR2Parser, CR3Parser, DPXParser, DPXParser::Binstr::Capture, FDXParser, FLACParser, GIFParser, HEIFParser, FormatParser::ISOBaseMediaFileFormat::Decoder, JPEGParser, JSONParser, M3UParser, MOVParser, MP3Parser, MP3Parser::ID3Extraction, MP4Parser, MPEGParser, NEFParser, OggParser, PDFParser, PNGParser, PSDParser, RW2Parser, TIFFParser, WAVParser, WebpParser, ZIPParser
- Defined in:
- lib/io_utils.rb
Defined Under Namespace
Classes: InvalidRead, MalformedFile
Constant Summary
collapse
- INTEGER_DIRECTIVES =
{
1 => 'C',
2 => 'S',
4 => 'L',
8 => 'Q'
}
Instance Method Summary
collapse
Instance Method Details
#read_bytes(n) ⇒ Object
Also known as:
read_string
ānā is the number of bytes to read
52
53
54
|
# File 'lib/io_utils.rb', line 52
def read_bytes(n)
safe_read(@buf, n)
end
|
#read_fixed_point(fractional_digits: 16, **kwargs) ⇒ Object
47
48
49
|
# File 'lib/io_utils.rb', line 47
def read_fixed_point(fractional_digits: 16, **kwargs)
read_int(**kwargs) / 2.0**fractional_digits
end
|
#read_int(n: 4, signed: false, big_endian: true) ⇒ Object
40
41
42
43
44
45
|
# File 'lib/io_utils.rb', line 40
def read_int(n: 4, signed: false, big_endian: true)
directive = INTEGER_DIRECTIVES[n]
directive.downcase! if signed
directive += (big_endian ? ">" : "<") if n > 1
read_bytes(n).unpack(directive).first
end
|
#safe_read(io, n) ⇒ Object
15
16
17
18
19
20
21
22
23
|
# File 'lib/io_utils.rb', line 15
def safe_read(io, n)
raise ArgumentError, 'Unbounded reads are not supported' if n.nil?
buf = io.read(n)
raise InvalidRead, "We wanted to read #{n} bytes from the IO, but the IO is at EOF" unless buf
raise InvalidRead, "We wanted to read #{n} bytes from the IO, but we got #{buf.bytesize} instead" if buf.bytesize != n
buf
end
|
#safe_skip(io, n) ⇒ Object
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/io_utils.rb', line 25
def safe_skip(io, n)
raise ArgumentError, 'Unbounded skips are not supported' if n.nil?
return if n == 0
raise InvalidRead, 'Negative skips are not supported' if n < 0
io.seek(io.pos + n)
nil
end
|
#skip_bytes(n) ⇒ Object
58
59
60
61
|
# File 'lib/io_utils.rb', line 58
def skip_bytes(n)
safe_skip(@buf, n)
yield if block_given?
end
|