@@ -25,52 +25,55 @@ def truncate(self):
2525
2626 def _rtruncate (self , amt = None ):
2727 "[internal] remove the given amount of data"
28+ strio = self ._strio
2829 if amt is None :
2930 amt = self ._strio .tell ()
30- self . _strio .seek (0 , 2 )
31- size = self . _strio .tell ()
31+ strio .seek (0 , 2 )
32+ size = strio .tell ()
3233 # if the total size is equal to the amt,
3334 # then the whole thing is going to be truncated.
3435 if size == amt :
35- self . _strio .truncate (0 )
36+ strio .truncate (0 )
3637 return
3738
3839 copyto_pos = 0
3940 copyfrom_pos = amt
4041 while True :
41- self . _strio .seek (copyfrom_pos )
42- data = self . _strio .read (self ._block )
42+ strio .seek (copyfrom_pos )
43+ data = strio .read (self ._block )
4344 # Next copyfrom
44- copyfrom_pos = self . _strio .tell ()
45- self . _strio .seek (copyto_pos )
46- self . _strio .write (data )
45+ copyfrom_pos = strio .tell ()
46+ strio .seek (copyto_pos )
47+ strio .write (data )
4748 if len (data ) != self ._block :
4849 break
4950 # Next copyto
50- copyto_pos = self . _strio .tell ()
51+ copyto_pos = strio .tell ()
5152
52- self . _strio .truncate (size - amt )
53+ strio .truncate (size - amt )
5354
54- def has_message (self ):
55+ def has_message (self , xl_unpack = xl_unpack , len = len ):
5556 "if the buffer has a message available"
56- self ._strio .seek (self ._start )
57- header = self ._strio .read (5 )
57+ strio = self ._strio
58+ strio .seek (self ._start )
59+ header = strio .read (5 )
5860 if len (header ) < 5 :
5961 return False
6062 length , = xl_unpack (header )
6163 if length < 4 :
6264 raise ValueError ("invalid message size '%d'" % (length ,))
63- self . _strio .seek (0 , 2 )
64- return (self . _strio .tell () - self ._start ) >= length + 1
65+ strio .seek (0 , 2 )
66+ return (strio .tell () - self ._start ) >= length + 1
6567
66- def __len__ (self ):
68+ def __len__ (self , xl_unpack = xl_unpack , len = len ):
6769 "number of messages in buffer"
6870 count = 0
6971 rpos = self ._start
70- self ._strio .seek (self ._start )
72+ strio = self ._strio
73+ strio .seek (self ._start )
7174 while True :
7275 # get the message metadata
73- header = self . _strio .read (5 )
76+ header = strio .read (5 )
7477 rpos += 5
7578 if len (header ) < 5 :
7679 # not enough data for another message
@@ -81,15 +84,20 @@ def __len__(self):
8184
8285 if length < 4 :
8386 raise ValueError ("invalid message size '%d'" % (length ,))
84- self . _strio .seek (length - 4 - 1 , 1 )
87+ strio .seek (length - 4 - 1 , 1 )
8588
86- if len (self . _strio .read (1 )) != 1 :
89+ if len (strio .read (1 )) != 1 :
8790 break
8891 count += 1
8992 return count
9093
91- def _get_message (self , mtypes = message_types ):
92- header = self ._strio .read (5 )
94+ def _get_message (self ,
95+ mtypes = message_types ,
96+ len = len ,
97+ xl_unpack = xl_unpack ,
98+ ):
99+ strio = self ._strio
100+ header = strio .read (5 )
93101 if len (header ) < 5 :
94102 return
95103 length , = xl_unpack (header )
@@ -98,7 +106,7 @@ def _get_message(self, mtypes = message_types):
98106 if length < 4 :
99107 raise ValueError ("invalid message size '%d'" % (length ,))
100108 length -= 4
101- body = self . _strio .read (length )
109+ body = strio .read (length )
102110 if len (body ) < length :
103111 # Not enough data for message.
104112 return
@@ -127,7 +135,7 @@ def __next__(self):
127135 self ._start = self ._strio .tell ()
128136 return msg
129137
130- def read (self , num = 0xFFFFFFFF ):
138+ def read (self , num = 0xFFFFFFFF , len = len ):
131139 if self ._start > self ._limit :
132140 self ._rtruncate (self ._start )
133141 self ._start = 0
0 commit comments