Created
September 7, 2012 01:49
-
-
Save dsturnbull/3662504 to your computer and use it in GitHub Desktop.
Multiple file buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from cStringIO import StringIO | |
class MultiBuffer(): | |
def __init__(self, *files): | |
self.files = files | |
self.i = 0 | |
def read(self, length): | |
buf = StringIO() | |
self._read(length, buf) | |
return buf.getvalue() | |
def _read(self, length, buf): | |
data = self.files[self.i].read(length) | |
n = len(data) | |
buf.write(data) | |
if n < length: | |
self.i += 1 | |
if self.i < len(self.files): | |
self._read(length - n, buf) | |
mbuf = MultiBuffer(StringIO('hihihi'), open('Rakefile')) | |
sys.stdout.write(mbuf.read(30)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment