Sha256: 2566b6cf7363a46c92e50b1a252fc773115d95c87bb025a8313b3ecc36fcc18e

Contents?: true

Size: 673 Bytes

Versions: 19

Compression:

Stored size: 673 Bytes

Contents

# frozen_string_literal: true

# 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

19 entries across 19 versions & 1 rubygems

Version Path
zip_tricks-4.8.3 lib/zip_tricks/write_and_tell.rb
zip_tricks-5.3.1 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.8.2 lib/zip_tricks/write_and_tell.rb
zip_tricks-5.3.0 lib/zip_tricks/write_and_tell.rb
zip_tricks-5.2.0 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.8.1 lib/zip_tricks/write_and_tell.rb
zip_tricks-5.1.1 lib/zip_tricks/write_and_tell.rb
zip_tricks-5.1.0 lib/zip_tricks/write_and_tell.rb
zip_tricks-5.0.0 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.8.0 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.7.4 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.7.3 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.7.2 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.7.1 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.7.0 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.6.0 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.5.2 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.5.1 lib/zip_tricks/write_and_tell.rb
zip_tricks-4.5.0 lib/zip_tricks/write_and_tell.rb