Sha256: 6f284f85d99a5de2de244551612a0b2b3a4df2d139cf060e6f16e6b131cad5af

Contents?: true

Size: 806 Bytes

Versions: 7

Compression:

Stored size: 806 Bytes

Contents

module FormatParser::IOUtils
  class InvalidRead < 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

    if io.respond_to?(:pos)
      io.seek(io.pos + n)
    else
      safe_read(io, n)
    end
    nil
  end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
format_parser-0.4.0 lib/io_utils.rb
format_parser-0.3.5 lib/io_utils.rb
format_parser-0.3.4 lib/io_utils.rb
format_parser-0.3.3 lib/io_utils.rb
format_parser-0.3.2 lib/io_utils.rb
format_parser-0.3.1 lib/io_utils.rb
format_parser-0.3.0 lib/io_utils.rb