io.rb

lib/nwn/io.rb
Last Update: Sun Jul 19 20:42:22 +0200 2009

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
24:     def e_read(n, mesg = nil)
25:       str = read(n)
26:       if str == nil
27:         raise EOFError, "End of file reached"
28:       end
29:       if str.size < n
30:         raise MissingDataError.new("data truncated" + (mesg ? ": " + mesg : nil), str)
31:       end
32:       str
33:     end