Sha256: 1af68008aa241951a994221f1ba179012796767597a4bd33611026da23a9a3dc

Contents?: true

Size: 781 Bytes

Versions: 53

Compression:

Stored size: 781 Bytes

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

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

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
format_parser-1.2.1 lib/io_utils.rb
format_parser-1.2.0 lib/io_utils.rb
format_parser-1.1.0 lib/io_utils.rb
format_parser-1.0.0 lib/io_utils.rb
format_parser-0.29.1 lib/io_utils.rb
format_parser-0.29.0 lib/io_utils.rb
format_parser-0.28.0 lib/io_utils.rb
format_parser-0.27.0 lib/io_utils.rb
format_parser-0.26.0 lib/io_utils.rb
format_parser-0.25.6 lib/io_utils.rb
format_parser-0.25.5 lib/io_utils.rb
format_parser-0.25.4 lib/io_utils.rb
format_parser-0.25.3 lib/io_utils.rb
format_parser-0.25.2 lib/io_utils.rb
format_parser-0.25.1 lib/io_utils.rb
format_parser-0.25.0 lib/io_utils.rb
format_parser-0.24.2 lib/io_utils.rb
format_parser-0.24.1 lib/io_utils.rb
format_parser-0.24.0 lib/io_utils.rb
format_parser-0.23.1 lib/io_utils.rb