Module: IOExtensions

Defined in:
lib/archive/support/ioextensions.rb

Overview

IOExtensions provides convenience wrappers for certain IO functionality.

Class Method Summary collapse

Class Method Details

.read_exactly(io, length, buffer = '') ⇒ Object

Reads and returns exactly length bytes from io using the read method on io. If there is insufficient data available, an EOFError is raised.


7
8
9
10
11
12
13
14
15
# File 'lib/archive/support/ioextensions.rb', line 7

def self.read_exactly(io, length, buffer = '')
  buffer.slice!(0..-1) unless buffer.empty?
  while buffer.size < length do
    internal = io.read(length - buffer.size)
    raise EOFError, 'unexpected end of file' if internal.nil?
    buffer << internal
  end
  buffer
end