-
Notifications
You must be signed in to change notification settings - Fork 47
/
bbuff.py
43 lines (30 loc) · 875 Bytes
/
bbuff.py
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
38
39
40
41
42
43
class BufferUnderflowException(Exception):
pass
class BoundBuffer(object):
buff = b''
cursor = 0
def __init__(self, data=b""):
self.write(data)
def read(self, length):
if length > len(self):
raise BufferUnderflowException()
out = self.buff[self.cursor:self.cursor+length]
self.cursor += length
return out
def write(self, data):
self.buff += data
def flush(self):
return self.read(len(self))
def save(self):
self.buff = self.buff[self.cursor:]
self.cursor = 0
def revert(self):
self.cursor = 0
def tell(self):
return self.cursor
def __len__(self):
return len(self.buff) - self.cursor
def __repr__(self):
return "<BoundBuffer '%s'>" % repr(self.buff[self.cursor:])
recv = read
append = write