File size: 817 Bytes
b2eb230
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import struct

from .text_data_pb2 import TextData


def read_pb_stream(f):
    while True:
        buf = f.read(4)
        if len(buf) == 0:
            break
        size = struct.unpack("I", buf)[0]
        buf = f.read(size)
        text_data = TextData()
        text_data.ParseFromString(buf)
        yield text_data


def write_pb_stream(f, text_data):
    buf = text_data.SerializeToString()
    f.write(struct.pack("I", len(buf)))
    f.write(buf)


def pack_pb_stream(text_data):
    buf = text_data.SerializeToString()
    return struct.pack("I", len(buf)) + buf


def split_pb_stream(f):
    while True:
        head = f.read(4)
        if len(head) == 0:
            break
        size = struct.unpack("I", head)[0]
        buf = f.read(size)
        yield head + buf