diff --git a/qpython/qreader.py b/qpython/qreader.py index 3a602e3..c43b193 100644 --- a/qpython/qreader.py +++ b/qpython/qreader.py @@ -167,7 +167,7 @@ def read_header(self, source = None): # skip 1 byte self._buffer.skip() - message_size = self._buffer.get_int() + message_size = self._buffer.get_uint() return QMessage(None, message_type, message_size, message_compressed) @@ -384,8 +384,28 @@ def _read_bytes(self, length): if length == 0: return b'' - else: + if length <= sys.maxint: data = self._stream.read(length) + else: + try: + from cStringIO import StringIO + except: + from StringIO import StringIO + + # for large messages, read from the stream in chunks + remaining = length + buff = StringIO() + + while remaining > 0: + chunk = self._stream.read(min(remaining, 2048)) + + if chunk: + remaining = remaining - len(chunk) + buff.write(chunk) + else: + break + + data = buff.getvalue() if len(data) == 0: raise QReaderException('Error while reading data') @@ -491,6 +511,15 @@ def get_byte(self): return self.get('b') + def get_uint(self): + ''' + Gets a single unsigned 32-bit integer from the buffer. + + :returns: single unsigned integer + ''' + return self.get('I') + + def get_int(self): ''' Gets a single 32-bit integer from the buffer.