Sha256: 9fe2c617e9e442da9ef75db91612904a6f9da00a07e01d979a264f4261293f3c

Contents?: true

Size: 921 Bytes

Versions: 1

Compression:

Stored size: 921 Bytes

Contents

class FormatParser::ReadLimiter
  NO_LIMIT = nil
  class BudgetExceeded < StandardError
  end

  def initialize(io, max_bytes: NO_LIMIT, max_reads: NO_LIMIT, max_seeks: NO_LIMIT)
    @max_bytes = max_bytes
    @max_reads = max_reads
    @max_seeks = max_seeks

    @io = io
    @seeks = 0
    @reads = 0
    @bytes = 0
  end

  def size
    @io.size
  end

  def pos
    @io.pos
  end

  def seek(to_offset)
    @seeks += 1
    if @max_seeks && @seeks > @max_seeks
      raise BudgetExceeded, "Seek budget exceeded (%d seeks performed)" % @max_seeks
    end
    @io.seek(to_offset)
  end

  def read(n)
    @bytes += n
    @reads += 1

    if @max_bytes && @bytes > @max_bytes
      raise BudgetExceeded, "Read bytes budget (%d) exceeded" % @max_bytes
    end

    if @max_reads && @reads > @max_reads
      raise BudgetExceeded, "Number of read() calls exceeded (%d max)" % @max_reads
    end

    @io.read(n)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
format_parser-0.1.2 lib/read_limiter.rb