Sha256: 21e3a17159f5523617d9f923b1ecc8440097d6783eec7a866f41a47062753b2f

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 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

  def read_little_endian_int_16
    safe_read(@buf, 2).unpack('v').first
  end

  def read_little_endian_int_32
    safe_read(@buf, 4).unpack('V').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

6 entries across 6 versions & 1 rubygems

Version Path
format_parser-1.7.0 lib/io_utils.rb
format_parser-1.6.0 lib/io_utils.rb
format_parser-1.5.0 lib/io_utils.rb
format_parser-1.4.2 lib/io_utils.rb
format_parser-1.4.1 lib/io_utils.rb
format_parser-1.4.0 lib/io_utils.rb