Sha256: 08029e26401b6d2a996e2f8ccfc728b0a508bbb67c9e3dac7fadabb7b62a2b27
Contents?: true
Size: 1.29 KB
Versions: 18
Compression:
Stored size: 1.29 KB
Contents
# frozen_string_literal: true module GrpcKit module Transport module Packable # @param data [String] # @param compress [Boolean] # @return [String] packed value def pack(data, compress = false) c = compress ? 1 : 0 [c, data.bytesize, data].pack('CNa*') end # @param data [String] # @return [String] def unpack(data) unpacker.feed(data) if data unpacker.read end def unpacker @unpacker ||= Unpacker.new end class Unpacker # Compressed bytes(1 Byte) + length bytes(4 Bytes) METADATA_SIZE = 5 def initialize @data = +''.b end # @return [Boolean] def data_exist? !@data.empty? end # @param data [String] # @return [void] def feed(data) @data << data end # @return [nil, Array<Boolean, Integer, String>] def read return nil if @data.empty? d = @data.freeze metadata = d.byteslice(0, METADATA_SIZE) c, size = metadata.unpack('CN') data = @data.byteslice(METADATA_SIZE, size) @data = @data.byteslice(METADATA_SIZE + size, @data.bytesize) [c != 0, size, data] end end end end end
Version data entries
18 entries across 18 versions & 1 rubygems