# class IO

### Public Class Methods

binread(name, length = nil, offset = 0) click to toggle source

    # File lib/puppet/util/monkey_patches.rb, line 170
    def self.binread(name, length = nil, offset = 0)
      File.open(name, 'rb') do |f|
        f.seek(offset) if offset > 0
        f.read(length)
      end
    end

binwrite(name, string, offset = nil) click to toggle source

    # File lib/puppet/util/monkey_patches.rb, line 177
    def self.binwrite(name, string, offset = nil)
      # Determine if we should truncate or not.  Since the truncate method on a
      # file handle isn't implemented on all platforms, safer to do this in what
      # looks like the libc / POSIX flag - which is usually pretty robust.
      # --daniel 2012-03-11
      mode = Fcntl::O_CREAT | Fcntl::O_WRONLY | (offset.nil? ? Fcntl::O_TRUNC : 0)

      # We have to duplicate the mode because Ruby on Windows is a bit precious,
      # and doesn't actually carry over the mode.  It won't work to just use
      # open, either, because that doesn't like our system modes and the default
      # open bits don't do what we need, which is awesome. --daniel 2012-03-30
      IO.open(IO::sysopen(name, mode), mode) do |f|
        # ...seek to our desired offset, then write the bytes.  Don't try to
        # seek past the start of the file, eh, because who knows what platform
        # would legitimately blow up if we did that.
        #
        # Double-check the positioning, too, since destroying data isn't my idea
        # of a good time. --daniel 2012-03-11
        target = [0, offset.to_i].max
        unless (landed = f.sysseek(target, IO::SEEK_SET)) == target
          raise "unable to seek to target offset #{target} in #{name}: got to #{landed}"
        end

        f.syswrite(string)
      end
    end