-
Notifications
You must be signed in to change notification settings - Fork 4
/
DataBuffer.py
33 lines (28 loc) · 926 Bytes
/
DataBuffer.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
#MineBot
#GPL and all that
# - espes
import struct
from StringIO import StringIO
class IncompleteDataError(Exception):
pass
class DataBuffer(StringIO):
def lenLeft(self):
return len(self.getvalue())-self.tell()
def read(self, size=None):
if size is None:
return StringIO.read(self)
if self.lenLeft() < size:
raise IncompleteDataError
return StringIO.read(self, size)
def readStruct(self, formatString):
length = struct.calcsize(formatString)
try:
return struct.unpack(formatString, self.read(length))
except struct.error:
raise IncompleteDataError
def peek(self, size=None):
if size is None:
return self.getvalue()[self.tell():]
if self.lenLeft() < size:
raise IncompleteDataError
return self.getvalue()[self.tell():self.tell()+size]