@@ -73,6 +73,11 @@ cdef class OutputStream(object):
7373 if not v:
7474 break
7575
76+ cpdef write_var_int32(self , libc.stdint.int32_t signed_v):
77+ """ Encode an int using variable-length encoding to a stream."""
78+ cdef libc.stdint.int64_t v = signed_v & < libc.stdint.int64_t> (0xFFFFFFFF )
79+ self .write_var_int64(v)
80+
7681 cpdef write_bigendian_int64(self , libc.stdint.int64_t signed_v):
7782 self .write_bigendian_uint64(signed_v)
7883
@@ -91,7 +96,7 @@ cdef class OutputStream(object):
9196
9297 cpdef write_bigendian_int32(self , libc.stdint.int32_t signed_v):
9398 cdef libc.stdint.uint32_t v = signed_v
94- if self .buffer_size < self .pos + 4 :
99+ if self .buffer_size < self .pos + 4 :
95100 self .extend(4 )
96101 self .data[self .pos ] = < unsigned char > (v >> 24 )
97102 self .data[self .pos + 1 ] = < unsigned char > (v >> 16 )
@@ -151,6 +156,12 @@ cdef class ByteCountingOutputStream(OutputStream):
151156 cpdef write_var_int64(self , libc.stdint.int64_t signed_v):
152157 self .count += get_varint_size(signed_v)
153158
159+ cpdef write_var_int32(self , libc.stdint.int32_t signed_v):
160+ if signed_v < 0 :
161+ self .count += 5
162+ else :
163+ self .count += get_varint_size(signed_v)
164+
154165 cpdef write_byte(self , unsigned char _):
155166 self .count += 1
156167
@@ -225,6 +236,11 @@ cdef class InputStream(object):
225236
226237 return result
227238
239+ cpdef libc.stdint.int32_t read_var_int32(self ) except ? - 1 :
240+ """ Decode a variable-length encoded int32 from a stream."""
241+ cdef libc.stdint.int64_t v = self .read_var_int64()
242+ return < libc.stdint.int32_t> (v);
243+
228244 cpdef libc.stdint.int64_t read_bigendian_int64(self ) except ? - 1 :
229245 return self .read_bigendian_uint64()
230246
0 commit comments