def foo(stringio):
    datalist = []
while True:
    chunk = stringio.read(256)
i = chunk.find('Z')
if i == -1:
    datalist.append(chunk)
else:
    datalist.append(chunk[:i + 1])
break
if len(chunk) < 256:
    break
return ''.join(datalist)
fast way to read from StringIO until some byte is encountered