io.rb

lib/nwn/io.rb
Last Update: Sun Jul 31 18:16:20 +0200 2011

This is a loose adaption of readbytes.rb, but with more flexibility and StringIO support.

Methods

public instance

  1. e_read

Public instance methods

e_read (n, mesg = nil)

Reads exactly n bytes.

If the data read is nil an EOFError is raised.

If the data read is too short a MissingDataError is raised and the read data is obtainable via its data method.

[show source]
# File lib/nwn/io.rb, line 24
    def e_read(n, mesg = nil)
      str = read(n)
      if str == nil
        raise EOFError, "End of file reached"
      end
      if str.size < n
        raise MissingDataError.new("data truncated" + (mesg ? ": " + mesg : nil), str)
      end
      str
    end