Sha256: 71af26b13a0cdd4d00dcb37fa6574bccfc422e1095afc51f78d6195d53f254f6

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 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 ArgumentError, "invalid ConditionalCompressor version"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
paquito-0.2.0 lib/paquito/conditional_compressor.rb
paquito-0.1.0 lib/paquito/conditional_compressor.rb