*** empty log message ***
authorca
Wed, 26 Jun 2002 10:03:48 +0200
changeset 1062 211b3cb6d628
parent 1061 be824dc5e0e7
child 1063 144a9e513f3e
*** empty log message ***
CompressionStream.st
--- a/CompressionStream.st	Wed Jun 26 08:44:39 2002 +0200
+++ b/CompressionStream.st	Wed Jun 26 10:03:48 2002 +0200
@@ -278,6 +278,34 @@
     ^ hitEOF ~~ false
 !
 
+canReadWithoutBlocking
+    "returns true if data are available for reading;
+     false if the stream is at end.
+     updates the readLimit and position
+    "
+    mode == #readonly ifFalse:[
+        self errorReadOnly
+    ].
+    hitEOF == true ifTrue:[ ^ false ].
+
+    position >= readLimit ifTrue:[
+        [ (readLimit := self zinflate) == 0 ] whileTrue:[ |n|
+            n := onStream nextBytes:(inputBytes size) into:inputBytes startingAt:1.
+
+            (n notNil and:[n > 0]) ifFalse:[
+                self pastEnd
+            ].
+            self zset_avail_in:n.
+        ].
+        readLimit ifNil:[
+            hitEOF := true.
+          ^ false
+        ].
+        position := 0.
+    ].
+    ^ true
+!
+
 isBinary
     "return true, if the stream is in binary (as opposed to text-) mode.
      The default when created is false.
@@ -306,18 +334,33 @@
 !CompressionStream methodsFor:'reading'!
 
 contents
-    "return the entire contents of the stream
+    "return the entire contents of the stream;
+     after reading the stream is closed.
     "
-    |out b|
+    |stream bfsize|
 
     mode == #readonly ifFalse:[ self errorReadOnly ].
 
-    out := (String new:1024) writeStream.
+    binary ifTrue:[ stream := ByteArray new:1024 ]
+          ifFalse:[ stream := String    new:1024 ].
+
+    stream := stream writeStream.
+    bfsize := outputBytes size.
 
-    [ (b := self next) notNil ] whileTrue:[
-        out nextPut:b
+    [ self canReadWithoutBlocking ] whileTrue:[ |data|
+        readLimit == bfsize ifTrue:[
+            data := outputBytes.
+        ] ifFalse:[
+            data := outputBytes copyFrom:1 to:readLimit.
+        ].
+        binary ifFalse:[
+            data := data asString
+        ].
+        stream nextPutAll:data.
+        position := readLimit := 0.     "reset input data"
     ].
-    ^ out contents
+    self close.
+  ^ stream contents
 !
 
 next
@@ -326,24 +369,9 @@
     "
     |byte|
 
-    mode == #readonly ifFalse:[ self errorReadOnly ].
-
-    hitEOF == true ifTrue:[^ nil].
-
-    position == readLimit ifTrue:[
-        [ (readLimit := self zinflate) == 0 ] whileTrue:[ |n|
-            n := onStream nextBytes:(inputBytes size) into:inputBytes startingAt:1.
-
-            (n notNil and:[n > 0]) ifFalse:[
-                self pastEnd
-            ].
-            self zset_avail_in:n.
-        ].
-        readLimit ifNil:[
-            hitEOF := true.
-          ^ nil
-        ].
-        position := 0.
+    self canReadWithoutBlocking ifFalse:[
+        "there is no more element; the stream is at end"
+        ^ nil
     ].
     position := position + 1.
     byte := outputBytes at:position.
@@ -392,6 +420,14 @@
 
 !CompressionStream methodsFor:'writing'!
 
+contents:contents
+    "write the entire contents to the stream;
+     after writing the stream is closed.
+    "
+    contents do:[:c| self nextPut:c ].
+    self close.
+!
+
 flush
     "flush the input and output buffer
     "
@@ -446,8 +482,10 @@
 bad: ;
 %}.
     byte ifNil:[
-        mode == #readonly ifTrue:[self errorReadOnly].
-        zstream ifNil:[ self errorNotOpen ].
+        mode ~~ #writeonly ifTrue:[
+            zstream ifNil:[self errorNotOpen].
+            self errorWriteOnly.
+        ].
         self invalidArguments.
     ].
     position == inputBytes size ifTrue:[ self flush ].