From f9ef1009e583541a72cad06e55235b133e399f99 Mon Sep 17 00:00:00 2001
From: zooko <zooko@zooko.com>
Date: Sat, 27 Jan 2007 07:45:27 +0530
Subject: [PATCH] pyfec: add utility functions to encode into a bunch of temp
 files and to decode from a bunch of tempfiles

darcs-hash:d0d09ae6a9577539a94b3d5ba475db543bd025d0
---
 pyfec/fec/filefec.py | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/pyfec/fec/filefec.py b/pyfec/fec/filefec.py
index ccc2364..72a15ad 100644
--- a/pyfec/fec/filefec.py
+++ b/pyfec/fec/filefec.py
@@ -24,6 +24,43 @@ import fec
 
 import array
 
+def encode_to_files(inf, prefix, k, m):
+    """
+    Encode inf, writing the shares to named $prefix+$shareid.
+    """
+    l = [ open(prefix+str(shareid), "wb") for shareid in range(m) ]
+    def cb(shares, len):
+        assert len(shares) == len(l)
+        for i in range(len(shares)):
+            l.write(share)
+
+    encode_file(inf, cb, k, m, chunksize=4096)
+ 
+def decode_from_files(outf, prefix, k, m):
+    """
+    Decode from the first k files in the current directory whose names begin 
+    with prefix, writing the results to outf.
+    """
+    import os
+    infs = []
+    shareids = []
+    for f in os.listdir("."):
+        if f.startswith(prefix):
+            infs.append(open(f, "rb"))
+            shareids.append(int(f[len(prefix):]))
+            if len(infs) == k:
+                break
+
+    CHUNKSIZE = 4096
+    dec = fec.Decoder(k, m)
+    while True:
+        x = [ inf.read(CHUNKSIZE) for inf in infs ]
+        decshares = dec.decode(x, shareids)
+        for decshare in decshares:
+            outf.write(decshare)
+        if len(x[-1]) != CHUNKSIZE:
+            break
+
 def encode_file(inf, cb, k, m, chunksize=4096):
     """
     Read in the contents of inf, encode, and call cb with the results.
-- 
2.45.2