def __init__(self, read_func, increment=2048): """Generic implementation of StreamBuffer, don't expect performance marvels! :read_func: function reading from your stream with signature read(bytearray buffer, int offset, int count) -> int bytes_read with *buffer* the buffer to read to, *offset* the first index in buffer to read to and *count* the maximum number of bytes to read, but read **at least** one byte. :increment: amount of bytes to increase buffer size with when running out of space """ super(BufferOffsetReader, self).__init__() self._read = read_func self._incr = increment # buffer size increment self._size = 0 # size of buffer contents # buffer[0] == start of contents always self._buff = bytearray(increment)
def f(self, buff, offs, maxi): buf = bytearray(maxi) result = self._read_f(buf, maxi) if result > 0: buff[offs:offs + result] = buf[:result] # copy to offset return result
def _increase_buffer(self): inc_buf = bytearray(len(self._buff) + self._incr) inc_buf[0:len(self._buff)] = self._buff self._buff = inc_buf
def __init__(self, filename): file_p = open(filename, 'rb') self.file_bytes = bytearray(file_p.read()) file_p.close() self.encoded_bytes = bytearray() self.position = 0
def encode(self): encoded_bytes = bytearray() file_bytes = bytearray(self.file.read()) repeats = self.find_repeats(file_bytes) print "need to build the RLE'd data"