Sha256: 01428cffa4f33432f7d7dc509a24aacbce8c76a766bd8021b197eed0d9be0943

Contents?: true

Size: 652 Bytes

Versions: 2

Compression:

Stored size: 652 Bytes

Contents

# A tiny wrapper over any object that supports :<<.
# Adds :tell and :advance_position_by.
class ZipTricks::WriteAndTell
  def initialize(io)
    @io = io
    @pos = 0
  end
  
  def <<(bytes)
    return self if bytes.nil?
    binary_bytes = binary(bytes)
    @io << binary_bytes
    @pos += binary_bytes.bytesize
    self
  end
  
  def advance_position_by(num_bytes)
    @pos += num_bytes
  end
  
  def tell
    @pos
  end
  
  private
  
  def binary(str)
    return str if str.encoding == Encoding::BINARY
    str.force_encoding(Encoding::BINARY)
  rescue RuntimeError # the string is frozen
    str.dup.force_encoding(Encoding::BINARY)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
zip_tricks-2.6.0 lib/zip_tricks/write_and_tell.rb
zip_tricks-2.5.0 lib/zip_tricks/write_and_tell.rb