Base64Coder.st
changeset 1084 9d51967037ad
parent 1078 698bd52be7d1
child 1111 2a64f0fe418a
--- a/Base64Coder.st	Fri Aug 23 08:08:01 2002 +0200
+++ b/Base64Coder.st	Thu Sep 05 15:23:53 2002 +0200
@@ -88,44 +88,54 @@
 
 !Base64Coder methodsFor:'decoding'!
 
-next
-    "answer the next decoded byte"
+fillBuffer
+    "fill buffer with next 4 characters each representing 6 bits"
 
     |b shift|
 
-    bits == 0 ifTrue:[
-        "no more bits in buffer
-         if end has been reached, raise signal"
-        atEnd ifTrue:[
-             ^ stream class endOfStreamSignal raiseRequest.
-        ].
-
-        "read next 4 characters each representing 6 bits"
-        buffer := 0.
+    buffer := 0.
+    bits := 0.
+    [
+        "read next valid Base64 character, skip invalid characters"
         [
-            "read next valid Base64 character, skip invalid characters"
-            [
-                b := Base64ReverseMapping at:stream next asciiValue.
-            ] doWhile:[b == 255].
+            b := stream next.
+            b isNil ifTrue:[ "end of stream"
+                atEnd := true.
+                ^ self.
+            ].
+            b := Base64ReverseMapping at:b asciiValue.
+        ] doWhile:[b == 255].
 
-            b == 64 ifTrue:[
-                "got #=, end of Base64 string has been reached"
-                atEnd := true.
-                bits == 12 ifTrue:[
-                    "data has been padded to 12, skip 4 bits"
-                    shift := -4.
-                ] ifFalse:[
-                    "data has been padded to 18, skip 2 bits"
-                    shift := -2.
-                ].
-                bits := bits + shift.
-                buffer := buffer bitShift:shift.
+        b == 64 ifTrue:[
+            "got #=, end of Base64 string has been reached"
+            atEnd := true.
+            bits == 12 ifTrue:[
+                "data has been padded to 12, skip 4 bits"
+                shift := -4.
             ] ifFalse:[
-                "got valid Base64 character, append to buffer"
-                buffer := (buffer bitShift:6) bitOr:b.
-                bits := bits + 6.
+                "data has been padded to 18, skip 2 bits"
+                shift := -2.
             ].
-        ] doWhile:[bits ~~ 24 and:[atEnd not]].
+            bits := bits + shift.
+            buffer := buffer bitShift:shift.
+        ] ifFalse:[
+            "got valid Base64 character, append to buffer"
+            buffer := (buffer bitShift:6) bitOr:b.
+            bits := bits + 6.
+        ].
+    ] doWhile:[bits ~~ 24 and:[atEnd not]].
+!
+
+next
+    "answer the next decoded byte"
+
+    |b|
+
+    bits == 0 ifTrue:[
+        self fillBuffer.
+        bits == 0 ifTrue:[
+            ^ stream class endOfStreamSignal raiseRequest.
+        ]
     ].
 
     b := (buffer bitShift:(8 - bits)) bitAnd:16rFF.
@@ -250,7 +260,12 @@
 atEnd
     "answer true, if no more bytes can be read"
 
-    ^ bits == 0 and:[atEnd or:[stream atEnd]]
+    bits == 0 ifTrue:[
+        atEnd ifTrue:[^ true].
+        self fillBuffer.
+        bits == 0 ifTrue:[^ true].
+    ].
+    ^ false.
 ! !
 
 !Base64Coder methodsFor:'stream compatibility'!
@@ -272,6 +287,6 @@
 !Base64Coder class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Base64Coder.st,v 1.3 2002-08-15 09:57:46 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Base64Coder.st,v 1.4 2002-09-05 13:23:53 stefan Exp $'
 ! !
 Base64Coder initialize!