Skip to content

Instantly share code, notes, and snippets.

@dsturnbull
Created September 7, 2012 01:49
Show Gist options
  • Save dsturnbull/3662504 to your computer and use it in GitHub Desktop.
Save dsturnbull/3662504 to your computer and use it in GitHub Desktop.
Multiple file buffer
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