Sha256: 31ad6a8e4286aa315cf2c5cc0912a6d019e0d1fda4e3b7b880e511c8f38a4b26

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module FormatParser::IOUtils
  class InvalidRead < ArgumentError
  end

  class MalformedFile < ArgumentError
  end

  def safe_read(io, n)
    raise ArgumentError, 'Unbounded reads are not supported' if n.nil?
    buf = io.read(n)

    unless buf
      raise InvalidRead, "We wanted to read #{n} bytes from the IO, but the IO is at EOF"
    end
    if buf.bytesize != n
      raise InvalidRead, "We wanted to read #{n} bytes from the IO, but we got #{buf.bytesize} instead"
    end

    buf
  end

  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

  def read_int_8
    safe_read(@buf, 1).unpack('C').first
  end

  def read_int_16
    safe_read(@buf, 2).unpack('n').first
  end

  def read_int_32
    safe_read(@buf, 4).unpack('N').first
  end

  # 'n' is the number of bytes to read
  def read_string(n)
    safe_read(@buf, n)
  end

  ### TODO: Some kind of built-in offset for the read
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
format_parser-1.3.0 lib/io_utils.rb