Class: MultiCompress::Codec
- Inherits:
-
Object
- Object
- MultiCompress::Codec
- Defined in:
- lib/multi_compress/codec.rb
Constant Summary collapse
- MAGIC =
"\x89MCDB".b
- VERSION =
reserved 5-byte namespace; 0x89 is an invalid UTF-8 lead byte, so it won't collide with valid text. For BINARY data this prefix is reserved, not collision-free.
1- HEADER_SIZE =
magic + version byte + algo byte
MAGIC.bytesize + 2
- B64_PREFIX =
"mc1:"- MAX_NATIVE_OUTPUT =
(2**(0.size * 8 - 1)) - 1
- ALGO_ID =
{ zstd: 1, lz4: 2, brotli: 3 }.freeze
- ID_ALGO =
ALGO_ID.invert.freeze
Instance Attribute Summary collapse
-
#algo ⇒ Object
readonly
Returns the value of attribute algo.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#max_output_size ⇒ Object
readonly
Returns the value of attribute max_output_size.
Instance Method Summary collapse
- #dump(value) ⇒ Object
-
#initialize(algo: :zstd, level: nil, encode: nil, serializer: nil, encoding: Encoding::UTF_8, max_output_size: nil, legacy: :reject, dictionary: nil) ⇒ Codec
constructor
A new instance of Codec.
- #load(stored) ⇒ Object
Constructor Details
#initialize(algo: :zstd, level: nil, encode: nil, serializer: nil, encoding: Encoding::UTF_8, max_output_size: nil, legacy: :reject, dictionary: nil) ⇒ Codec
Returns a new instance of Codec.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/multi_compress/codec.rb', line 21 def initialize(algo: :zstd, level: nil, encode: nil, serializer: nil, encoding: Encoding::UTF_8, max_output_size: nil, legacy: :reject, dictionary: nil) raise ArgumentError, "unknown algo #{algo.inspect}" unless ALGO_ID.key?(algo) raise ArgumentError, "encode must be nil or :base64" unless [nil, :base64].include?(encode) @algo = algo @level = level @encode = encode @serializer = serializer @encoding = encoding limit = Integer(max_output_size || MultiCompress.config.max_output_size) raise ArgumentError, "max_output_size must be greater than 0" unless limit.positive? if limit > MAX_NATIVE_OUTPUT raise ArgumentError, "max_output_size must be at most #{MAX_NATIVE_OUTPUT} bytes on this platform" end @max_output_size = limit @legacy = normalize_legacy(legacy) @dictionary = dictionary end |
Instance Attribute Details
#algo ⇒ Object (readonly)
Returns the value of attribute algo.
19 20 21 |
# File 'lib/multi_compress/codec.rb', line 19 def algo @algo end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
19 20 21 |
# File 'lib/multi_compress/codec.rb', line 19 def level @level end |
#max_output_size ⇒ Object (readonly)
Returns the value of attribute max_output_size.
19 20 21 |
# File 'lib/multi_compress/codec.rb', line 19 def max_output_size @max_output_size end |
Instance Method Details
#dump(value) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/multi_compress/codec.rb', line 44 def dump(value) return nil if value.nil? payload = serialize_payload(value) body = MultiCompress.compress(payload, algo: @algo, level: @level, dictionary: @dictionary) framed = header + body @encode == :base64 ? B64_PREFIX + [framed].pack("m0") : framed end |
#load(stored) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/multi_compress/codec.rb', line 54 def load(stored) return nil if stored.nil? framed = decode_transport(stored) return decode_payload(unwrap(framed)) if framed read_legacy(stored) end |