Sha256: a04cb836cce1b9830fe01663f4c19720f28b3db49e07c68e6e69274446d411ca

Contents?: true

Size: 1.01 KB

Versions: 17

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module Paquito
  class ConditionalCompressor
    UNCOMPRESSED = 0
    COMPRESSED = 1

    def initialize(compressor, compress_threshold)
      @compressor = Paquito.cast(compressor)
      @compress_threshold = compress_threshold
    end

    def dump(uncompressed)
      uncompressed_size = uncompressed.bytesize
      version = UNCOMPRESSED
      value = uncompressed

      if @compress_threshold && uncompressed_size > @compress_threshold
        compressed = @compressor.dump(uncompressed)
        if compressed.bytesize < uncompressed_size
          version = COMPRESSED
          value = compressed
        end
      end

      version.chr(Encoding::BINARY) << value
    end

    def load(payload)
      payload_version = payload.getbyte(0)
      data = payload.byteslice(1..-1)
      case payload_version
      when UNCOMPRESSED
        data
      when COMPRESSED
        @compressor.load(data)
      else
        raise UnpackError, "invalid ConditionalCompressor version"
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
paquito-0.11.2 lib/paquito/conditional_compressor.rb
paquito-0.11.1 lib/paquito/conditional_compressor.rb
paquito-0.11.0 lib/paquito/conditional_compressor.rb
paquito-0.10.0 lib/paquito/conditional_compressor.rb
paquito-0.9.2 lib/paquito/conditional_compressor.rb
paquito-0.9.1 lib/paquito/conditional_compressor.rb
paquito-0.9.0 lib/paquito/conditional_compressor.rb
paquito-0.8.0 lib/paquito/conditional_compressor.rb
paquito-0.7.0 lib/paquito/conditional_compressor.rb
paquito-0.6.2 lib/paquito/conditional_compressor.rb
paquito-0.6.1 lib/paquito/conditional_compressor.rb
paquito-0.6.0 lib/paquito/conditional_compressor.rb
paquito-0.5.0 lib/paquito/conditional_compressor.rb
paquito-0.4.0 lib/paquito/conditional_compressor.rb
paquito-0.3.1 lib/paquito/conditional_compressor.rb
paquito-0.3.0 lib/paquito/conditional_compressor.rb
paquito-0.2.1 lib/paquito/conditional_compressor.rb