[Commits] (vajda) - fixed another bug in blocked reading of appended compressed text

commits at osafoundation.org commits at osafoundation.org
Thu Jan 22 00:13:28 PST 2004


Commit by: vajda
Modified files:
osaf/chandler/Chandler/repository/tests/TestBZ2.py None 1.1
osaf/chandler/Chandler/repository/tests/TestText.py 1.9 1.10
osaf/chandler/Chandler/repository/util/Streams.py 1.7 1.8

Log message:
 - fixed another bug in blocked reading of appended compressed text
 - added TestBZ2 unit test


ViewCVS links:
http://cvs.osafoundation.org/index.cgi/osaf/chandler/Chandler/repository/tests/TestBZ2.py?rev=1.1&content-type=text/vnd.viewcvs-markup
http://cvs.osafoundation.org/index.cgi/osaf/chandler/Chandler/repository/tests/TestText.py.diff?r1=text&tr1=1.9&r2=text&tr2=1.10
http://cvs.osafoundation.org/index.cgi/osaf/chandler/Chandler/repository/util/Streams.py.diff?r1=text&tr1=1.7&r2=text&tr2=1.8

Index: osaf/chandler/Chandler/repository/tests/TestText.py
diff -u osaf/chandler/Chandler/repository/tests/TestText.py:1.9 osaf/chandler/Chandler/repository/tests/TestText.py:1.10
--- osaf/chandler/Chandler/repository/tests/TestText.py:1.9	Wed Jan 21 22:22:00 2004
+++ osaf/chandler/Chandler/repository/tests/TestText.py	Thu Jan 22 00:12:57 2004
@@ -2,8 +2,8 @@
 Text storage unit tests
 """
 
-__revision__  = "$Revision: 1.9 $"
-__date__      = "$Date: 2004/01/22 06:22:00 $"
+__revision__  = "$Revision: 1.10 $"
+__date__      = "$Date: 2004/01/22 08:12:57 $"
 __copyright__ = "Copyright (c) 2003 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -119,9 +119,9 @@
 
         self.assert_(data == string)
         
-#    def testAppendBZ2(self):
-#
-#        self.appended('bz2')
+    def testAppendBZ2(self):
+
+        self.appended('bz2')
 
     def testAppendZlib(self):
 

Index: osaf/chandler/Chandler/repository/util/Streams.py
diff -u osaf/chandler/Chandler/repository/util/Streams.py:1.7 osaf/chandler/Chandler/repository/util/Streams.py:1.8
--- osaf/chandler/Chandler/repository/util/Streams.py:1.7	Wed Jan 21 01:03:03 2004
+++ osaf/chandler/Chandler/repository/util/Streams.py	Thu Jan 22 00:12:57 2004
@@ -1,6 +1,6 @@
 
-__revision__  = "$Revision: 1.7 $"
-__date__      = "$Date: 2004/01/21 09:03:03 $"
+__revision__  = "$Revision: 1.8 $"
+__date__      = "$Date: 2004/01/22 08:12:57 $"
 __copyright__ = "Copyright (c) 2002 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -79,15 +79,19 @@
         self.buffer.close()
 
 
-class BZ2InputStream(object):
+class CompressedInputStream(object):
 
     def __init__(self, inputStream):
 
-        super(BZ2InputStream, self).__init__()
+        super(CompressedInputStream, self).__init__()
 
-        self.bz2 = BZ2Decompressor()
+        self.decompressor = self._decompressor()
         self.inputStream = inputStream
         self.extra_data = ''
+
+    def _decompressor(self):
+
+        raise NotImplementedError, "CompressedInputStream._decompressor"
         
     def read(self, length = -1):
 
@@ -96,30 +100,32 @@
             self.extra_data = ''
             
         else:
-            while True:
-                data = self.inputStream.read(length)
-                if len(data) == 0:
-                    return ''
-
-                data = self.bz2.decompress(data)
-                if len(data) > 0:
-                    break
+            data = ''
+            while len(data) == 0:
+                unused_data = ''
+                while len(data) == 0 and len(unused_data) == 0:
+                    data = self.inputStream.read(length)
+                    if len(data) == 0:
+                        return ''
+
+                    data = self.decompressor.decompress(data)
+                    unused_data = self.decompressor.unused_data
         
-            if len(self.bz2.unused_data) > 0:
-                buffer = StringIO()
-                buffer.write(data)
-
-                while len(self.bz2.unused_data) > 0:
-                    bz2 = BZ2Decompressor()
-                    buffer.write(bz2.decompress(self.bz2.unused_data))
-                    self.bz2 = bz2
-
-                data = buffer.getvalue()
-                buffer.close()
-
-        if length > 0 and len(data) > length:
-            self.extra_data = data[length:]
-            data = data[0:length]
+                if len(unused_data) > 0:
+                    buffer = StringIO()
+                    buffer.write(data)
+
+                    while len(unused_data) > 0:
+                        self.decompressor = self._decompressor()
+                        buffer.write(self.decompressor.decompress(unused_data))
+                        unused_data = self.decompressor.unused_data
+
+                    data = buffer.getvalue()
+                    buffer.close()
+
+            if length > 0 and len(data) > length:
+                self.extra_data = data[length:]
+                data = data[0:length]
 
         return data
 
@@ -128,53 +134,18 @@
         self.inputStream.close()
 
 
-class ZlibInputStream(object):
+class BZ2InputStream(CompressedInputStream):
 
-    def __init__(self, inputStream):
+    def _decompressor(self):
 
-        super(ZlibInputStream, self).__init__()
+        return BZ2Decompressor()
 
-        self.zlib = decompressobj()
-        self.inputStream = inputStream
-        self.extra_data = ''
 
-    def read(self, length = -1):
+class ZlibInputStream(CompressedInputStream):
 
-        if len(self.extra_data) > 0:
-            data = self.extra_data
-            self.extra_data = ''
-            
-        else:
-            while True:
-                data = self.inputStream.read(length)
-                if len(data) == 0:
-                    return ''
-        
-                data = self.zlib.decompress(data)
-                if len(data) > 0:
-                    break
-        
-            if len(self.zlib.unused_data) > 0:
-                buffer = StringIO()
-                buffer.write(data)
-
-                while len(self.zlib.unused_data) > 0:
-                    zlib = decompressobj()
-                    buffer.write(zlib.decompress(self.zlib.unused_data))
-                    self.zlib = zlib
-
-                data = buffer.getvalue()
-                buffer.close()
-            
-        if length > 0 and len(data) > length:
-            self.extra_data = data[length:]
-            data = data[0:length]
+    def _decompressor(self):
 
-        return data
-
-    def close(self):
-
-        self.inputStream.close()
+        return decompressobj()
 
 
 class BufferedInputStream(object):



More information about the Commits mailing list