Class: Zrip::FrameCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/zrip/frame_codec.rb

Overview

Zstandard frame-format codec.

FrameCodec is Ractor-shareable. It uses an internal lock around native compression and decompression contexts.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._native_new(dict, id, level) ⇒ FrameCodec

Native constructor used by .new.

Parameters:

  • dict (String, nil)

    dictionary bytes

  • id (Integer)

    dictionary ID, or 0 without a dictionary

  • level (Integer)

    compression level

Returns:

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zrip/frame_codec.rb', line 64

class FrameCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

.get_frame_content_size(bytes) ⇒ Integer?

Read Frame_Content_Size from a Zstandard frame header.

Parameters:

  • bytes (String)

    compressed Zstandard frame

Returns:

  • (Integer, nil)

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zrip/frame_codec.rb', line 64

class FrameCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

.new(dict: nil, level: DEFAULT_LEVEL) ⇒ FrameCodec

Create a frame codec.

Parameters:

  • dict (Dictionary, String, nil) (defaults to: nil)

    optional Zstandard dictionary

  • level (Integer) (defaults to: DEFAULT_LEVEL)

    compression level

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zrip/frame_codec.rb', line 64

class FrameCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

Instance Method Details

#_native_decompress(bytes, max_output_size) ⇒ String

Native decompression entry used by #decompress.

Parameters:

  • bytes (String)

    compressed Zstandard frame

  • max_output_size (Integer)

    output byte limit, or 0 for unbounded

Returns:

  • (String)

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zrip/frame_codec.rb', line 64

class FrameCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

#compress(bytes) ⇒ String

Compress bytes to a Zstandard frame.

Parameters:

  • bytes (String)

    uncompressed bytes

Returns:

  • (String)

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zrip/frame_codec.rb', line 64

class FrameCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

#decompress(bytes, max_output_size: nil) ⇒ String

Decompress a Zstandard frame.

Parameters:

  • bytes (String)

    compressed Zstandard frame

  • max_output_size (Integer, nil) (defaults to: nil)

    optional output byte limit

Returns:

  • (String)

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zrip/frame_codec.rb', line 64

class FrameCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

#has_dict?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zrip/frame_codec.rb', line 64

class FrameCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

#idInteger?

Returns dictionary ID.

Returns:

  • (Integer, nil)

    dictionary ID



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zrip/frame_codec.rb', line 64

class FrameCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

#levelInteger

Returns compression level.

Returns:

  • (Integer)

    compression level



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zrip/frame_codec.rb', line 64

class FrameCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end

#sizeInteger

Returns dictionary size in bytes.

Returns:

  • (Integer)

    dictionary size in bytes



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zrip/frame_codec.rb', line 64

class FrameCodec
  def self.new(dict: nil, level: DEFAULT_LEVEL)
    case dict
    when nil
      _native_new(nil, 0, Integer(level))
    when Dictionary
      _native_new(dict.bytes, dict.id, Integer(level))
    when String
      d = Dictionary.new(bytes: dict)
      _native_new(d.bytes, d.id, Integer(level))
    else
      raise TypeError, "expected Zrip::Dictionary, String, or nil; got #{dict.class}"
    end
  end


  def decompress(bytes, max_output_size: nil)
    _native_decompress(bytes, Integer(max_output_size || 0))
  end
end