author | Stefan Vogel <sv@exept.de> |
Fri, 05 Mar 2004 20:02:42 +0100 | |
changeset 1415 | 3ef6a2c42611 |
parent 1391 | 530a5924e319 |
child 1442 | 7a149950485d |
permissions | -rw-r--r-- |
1308 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
1027 | 13 |
"{ Package: 'stx:libbasic2' }" |
14 |
||
15 |
ObjectCoder subclass:#Base64Coder |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
16 |
instanceVariableNames:'buffer bits charCount peekByte atEnd lineLimit' |
1027 | 17 |
classVariableNames:'Base64Mapping Base64ReverseMapping' |
18 |
poolDictionaries:'' |
|
19 |
category:'System-Storage' |
|
20 |
! |
|
21 |
||
22 |
!Base64Coder class methodsFor:'documentation'! |
|
23 |
||
1308 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
1027 | 38 |
documentation |
39 |
" |
|
40 |
Instances of this class perform Base64 en- and decoding as defined in RFC 2045 |
|
41 |
3 bytes are mapped to 4 characters, representing 6 bits each. |
|
42 |
||
43 |
[author:] |
|
44 |
Stefan Vogel |
|
45 |
||
46 |
[see also:] |
|
47 |
||
48 |
[instance variables:] |
|
49 |
buffer SmallInteger Up to 24 bits of data |
|
50 |
bits SmallInteger Number of valid bits in buffer |
|
51 |
charCount SmallInteger Number of characters since last cr |
|
52 |
atEnd Boolean true if end of Base64 string reached |
|
53 |
||
54 |
[class variables:] |
|
55 |
Base64Mapping String Mapping from bytes (with 6 valid bits) |
|
56 |
to Base64 characters |
|
57 |
Base64ReverseMapping Array Mapping from Base64 characters to 6-bit-Bytes |
|
58 |
" |
|
59 |
! |
|
60 |
||
61 |
examples |
|
62 |
" |
|
1122 | 63 |
[exBegin] |
1391 | 64 |
|coder decoder data encoding decoded| |
65 |
||
66 |
0 to:16 do:[:l | |
|
67 |
data := (0 to:l) asByteArray copyTo:l. |
|
68 |
coder := Base64Coder on:'' writeStream. |
|
69 |
coder nextPutAll:data. |
|
70 |
coder flush. |
|
71 |
||
72 |
encoding := coder contents. |
|
73 |
||
74 |
decoder := Base64Coder on:encoding readStream. |
|
75 |
decoded := decoder upToEnd. |
|
76 |
Transcript showCR:(data printString). |
|
77 |
Transcript show:' -> '; showCR:encoding. |
|
78 |
Transcript show:' ---> '; showCR:(decoded printString). |
|
79 |
self assert:(data = decoded). |
|
80 |
]. |
|
81 |
[exEnd] |
|
82 |
[exBegin] |
|
1122 | 83 |
|data1 text data2| |
84 |
||
85 |
data1 := #[0 1 16r7F 16r80 16r81 16rFE 16rFF]. |
|
86 |
text := Base64Coder encodingOf:data1. |
|
87 |
data2 := Base64Coder decodingOf:text. |
|
88 |
data2 |
|
89 |
[exEnd] |
|
1027 | 90 |
|
91 |
[exBegin] |
|
92 |
|coder| |
|
93 |
||
94 |
coder := Base64Coder on:'' writeStream. |
|
95 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]. |
|
96 |
coder flush. |
|
97 |
coder contents inspect. |
|
98 |
coder reset. |
|
99 |
coder nextPut:254. |
|
100 |
coder contents inspect. |
|
101 |
[exEnd] |
|
102 |
||
103 |
[exBegin] |
|
1391 | 104 |
|coder decoder| |
1027 | 105 |
|
106 |
coder := Base64Coder on:'' writeStream. |
|
107 |
coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]. |
|
108 |
coder flush. |
|
109 |
||
1391 | 110 |
decoder := Base64Coder on:(coder contents readStream). |
111 |
[decoder atEnd] whileFalse:[ |
|
112 |
Transcript show:decoder next |
|
1027 | 113 |
]. |
114 |
Transcript cr. |
|
115 |
[exEnd] |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
116 |
[exBegin] |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
117 |
|coder| |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
118 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
119 |
coder := Base64Coder on:'' writeStream. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
120 |
coder nextPutAll:(0 to:200) asByteArray. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
121 |
coder flush. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
122 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
123 |
Transcript showCR:(coder contents). |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
124 |
[exEnd] |
1027 | 125 |
" |
126 |
! ! |
|
127 |
||
128 |
!Base64Coder class methodsFor:'initialization'! |
|
129 |
||
130 |
initialize |
|
131 |
"initialize class variables" |
|
132 |
||
133 |
"64 characters representing the 6-bit values from 0-63 and one pad character" |
|
134 |
Base64Mapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='. |
|
135 |
Base64ReverseMapping := ByteArray new:256 withAll:255. |
|
136 |
Base64Mapping keysAndValuesDo:[:idx :char| |
|
1415
3ef6a2c42611
Use #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
1391
diff
changeset
|
137 |
Base64ReverseMapping at:char codePoint put:idx-1. |
1027 | 138 |
]. |
139 |
||
140 |
" |
|
141 |
self initialize |
|
142 |
" |
|
143 |
! ! |
|
144 |
||
145 |
!Base64Coder class methodsFor:'instance creation'! |
|
146 |
||
147 |
new |
|
148 |
^ self basicNew initialize |
|
149 |
! ! |
|
150 |
||
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
151 |
!Base64Coder methodsFor:'accessing'! |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
152 |
|
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
153 |
lineLimit:something |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
154 |
lineLimit := something. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
155 |
! ! |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
156 |
|
1063 | 157 |
!Base64Coder methodsFor:'decoding'! |
1027 | 158 |
|
1111 | 159 |
basicNext |
160 |
"answer the next decoded byte" |
|
161 |
||
162 |
|b| |
|
163 |
||
164 |
bits == 0 ifTrue:[ |
|
165 |
self fillBuffer. |
|
166 |
bits == 0 ifTrue:[ |
|
167 |
^ stream class endOfStreamSignal raiseRequest. |
|
168 |
] |
|
169 |
]. |
|
170 |
||
171 |
b := (buffer bitShift:(8 - bits)) bitAnd:16rFF. |
|
172 |
bits := bits - 8. |
|
173 |
||
174 |
^ b. |
|
175 |
! |
|
176 |
||
1084
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
177 |
fillBuffer |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
178 |
"fill buffer with next 4 characters each representing 6 bits" |
1027 | 179 |
|
180 |
|b shift| |
|
181 |
||
1084
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
182 |
buffer := 0. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
183 |
bits := 0. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
184 |
[ |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
185 |
"read next valid Base64 character, skip invalid characters" |
1027 | 186 |
[ |
1084
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
187 |
b := stream next. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
188 |
b isNil ifTrue:[ "end of stream" |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
189 |
atEnd := true. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
190 |
^ self. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
191 |
]. |
1415
3ef6a2c42611
Use #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
1391
diff
changeset
|
192 |
b := Base64ReverseMapping at:b codePoint. |
1084
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
193 |
] doWhile:[b == 255]. |
1027 | 194 |
|
1084
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
195 |
b == 64 ifTrue:[ |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
196 |
"got #=, end of Base64 string has been reached" |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
197 |
atEnd := true. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
198 |
bits == 12 ifTrue:[ |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
199 |
"data has been padded to 12, skip 4 bits" |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
200 |
shift := -4. |
1027 | 201 |
] ifFalse:[ |
1084
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
202 |
"data has been padded to 18, skip 2 bits" |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
203 |
shift := -2. |
1027 | 204 |
]. |
1084
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
205 |
bits := bits + shift. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
206 |
buffer := buffer bitShift:shift. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
207 |
] ifFalse:[ |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
208 |
"got valid Base64 character, append to buffer" |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
209 |
buffer := (buffer bitShift:6) bitOr:b. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
210 |
bits := bits + 6. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
211 |
]. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
212 |
] doWhile:[bits ~~ 24 and:[atEnd not]]. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
213 |
! |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
214 |
|
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
215 |
next |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
216 |
"answer the next decoded byte" |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
217 |
|
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
218 |
|b| |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
219 |
|
1111 | 220 |
peekByte notNil ifTrue:[ |
221 |
b := peekByte. |
|
222 |
peekByte := nil. |
|
223 |
^ b |
|
1027 | 224 |
]. |
1111 | 225 |
^ self basicNext. |
226 |
! |
|
1027 | 227 |
|
1111 | 228 |
peek |
229 |
"answer the next decoded byte" |
|
1027 | 230 |
|
1111 | 231 |
peekByte isNil ifTrue:[ |
232 |
peekByte := self basicNext. |
|
233 |
]. |
|
234 |
^ peekByte |
|
1063 | 235 |
! ! |
236 |
||
237 |
!Base64Coder methodsFor:'encoding'! |
|
238 |
||
1390 | 239 |
encodeByteArray:aByteArray with:aParameter |
240 |
^ self |
|
241 |
nextPutAll:aByteArray; |
|
242 |
flush. |
|
1063 | 243 |
|
244 |
" |
|
245 |
self encodingOf:#[1 2 3 4 5 6 255] |
|
246 |
" |
|
247 |
! |
|
248 |
||
249 |
encodeObject:anObject with:aParameter |
|
250 |
"not defined. Use nextPut or nextPutAll:. |
|
251 |
Could encode the printString here" |
|
252 |
||
253 |
^ self shouldNotImplement |
|
254 |
! |
|
255 |
||
1390 | 256 |
encodeString:aString with:aParameter |
257 |
^ self |
|
258 |
nextPutAll:aString asByteArray; |
|
259 |
flush. |
|
1063 | 260 |
|
261 |
" |
|
1111 | 262 |
|encoded decoded decoder| |
263 |
||
264 |
encoded := self encodingOf:'hello world'. |
|
265 |
decoded := #[] writeStream. |
|
266 |
decoder := Base64Coder on:encoded readStream. |
|
267 |
[decoder atEnd] whileFalse:[ |
|
268 |
decoded nextPut:(decoder next). |
|
269 |
]. |
|
270 |
decoded := decoded contents. |
|
271 |
decoded asString. |
|
1063 | 272 |
" |
1027 | 273 |
! |
274 |
||
275 |
nextPut:aByte |
|
276 |
"encode aByte on the output stream" |
|
277 |
||
278 |
|b1 b2 b3 b4| |
|
279 |
||
280 |
buffer := (buffer bitShift:8) bitOr:aByte. |
|
281 |
bits := bits + 8. |
|
282 |
bits == 24 ifTrue:[ |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
283 |
"RFC 2045 says: max 76 characters in one line" |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
284 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
285 |
stream cr. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
286 |
charCount := 0. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
287 |
]. |
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
288 |
|
1027 | 289 |
b4 := buffer bitAnd:16r3F. |
290 |
b3 := (buffer bitShift:-6) bitAnd:16r3F. |
|
291 |
b2 := (buffer bitShift:-12) bitAnd:16r3F. |
|
292 |
b1 := (buffer bitShift:-18) bitAnd:16r3F. |
|
293 |
buffer := bits := 0. |
|
294 |
stream nextPut:(Base64Mapping at:b1+1); |
|
295 |
nextPut:(Base64Mapping at:b2+1); |
|
296 |
nextPut:(Base64Mapping at:b3+1); |
|
297 |
nextPut:(Base64Mapping at:b4+1). |
|
298 |
||
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
299 |
charCount := charCount + 4. |
1027 | 300 |
]. |
301 |
! ! |
|
302 |
||
303 |
!Base64Coder methodsFor:'initialization'! |
|
304 |
||
1078 | 305 |
emptyWriteStream |
1390 | 306 |
"answer an empty stream. We encode as string" |
307 |
||
308 |
^ WriteStream on:(String new:64) |
|
1078 | 309 |
! |
310 |
||
1027 | 311 |
initialize |
312 |
||
313 |
buffer := bits := charCount := 0. |
|
1389
8eb6ec86c0c7
allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents:
1315
diff
changeset
|
314 |
lineLimit := 76. "RFC 2045 says: max 76 characters in one line" |
1027 | 315 |
atEnd := false. |
316 |
! ! |
|
317 |
||
318 |
!Base64Coder methodsFor:'misc'! |
|
319 |
||
320 |
flush |
|
1390 | 321 |
"flush the remaining bits of buffer. |
322 |
The number of bits in buffer is not a multiple of 6, so we pad |
|
323 |
the buffer and signal that padding has been done via $= characters." |
|
1027 | 324 |
|
1390 | 325 |
|b1 b2 b3 b4| |
326 |
||
327 |
bits == 0 ifTrue:[ |
|
1027 | 328 |
"buffer is empty, nothing to do" |
329 |
^ self. |
|
1390 | 330 |
]. |
331 |
||
332 |
bits == 8 ifTrue:[ |
|
1027 | 333 |
buffer := buffer bitShift:4. |
334 |
b4 := b3 := 64. "pad with '=='" |
|
335 |
b1 := (buffer bitShift:-6) bitAnd:16r3F. |
|
336 |
b2 := buffer bitAnd:16r3F. |
|
1390 | 337 |
] ifFalse:[ |
338 |
bits = 16 ifTrue:[ |
|
339 |
buffer := buffer bitShift:2. |
|
340 |
b4 := 64. "pad with '='" |
|
341 |
b3 := buffer bitAnd:16r3F. |
|
342 |
b2 := (buffer bitShift:-6) bitAnd:16r3F. |
|
343 |
b1 := (buffer bitShift:-12) bitAnd:16r3F. |
|
344 |
] |
|
345 |
]. |
|
346 |
bits := buffer := 0. |
|
1027 | 347 |
|
1391 | 348 |
"RFC 2045 says: max 76 characters in one line" |
349 |
(lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[ |
|
350 |
stream cr. |
|
351 |
charCount := 0. |
|
352 |
]. |
|
353 |
||
1390 | 354 |
stream nextPut:(Base64Mapping at:b1+1); |
355 |
nextPut:(Base64Mapping at:b2+1); |
|
356 |
nextPut:(Base64Mapping at:b3+1); |
|
357 |
nextPut:(Base64Mapping at:b4+1). |
|
1391 | 358 |
charCount := charCount + 4. |
1027 | 359 |
! |
360 |
||
361 |
reset |
|
362 |
"reset to initial state" |
|
363 |
||
364 |
super reset. |
|
365 |
buffer := bits := charCount := 0. |
|
366 |
atEnd := false. |
|
367 |
! ! |
|
368 |
||
369 |
!Base64Coder methodsFor:'queries'! |
|
370 |
||
371 |
atEnd |
|
372 |
"answer true, if no more bytes can be read" |
|
373 |
||
1084
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
374 |
bits == 0 ifTrue:[ |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
375 |
atEnd ifTrue:[^ true]. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
376 |
self fillBuffer. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
377 |
bits == 0 ifTrue:[^ true]. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
378 |
]. |
9d51967037ad
Fix for whitespace and end of line and multiple of 4 characters
Stefan Vogel <sv@exept.de>
parents:
1078
diff
changeset
|
379 |
^ false. |
1111 | 380 |
! |
381 |
||
382 |
binary |
|
383 |
^ self |
|
384 |
! |
|
385 |
||
386 |
isStream |
|
387 |
^ true |
|
1063 | 388 |
! ! |
389 |
||
390 |
!Base64Coder methodsFor:'stream compatibility'! |
|
391 |
||
1315 | 392 |
nextBytesInto:anObject startingAt:offset |
393 |
"copy bytes into anObject starting at offset" |
|
394 |
||
395 |
|off| |
|
396 |
||
397 |
off := offset. |
|
398 |
[self atEnd] whileFalse:[ |
|
399 |
anObject at:off put:self next. |
|
400 |
off := off + 1. |
|
401 |
]. |
|
402 |
^ off - offset |
|
403 |
! |
|
404 |
||
1063 | 405 |
upToEnd |
406 |
"return a collection of the elements up-to the end. |
|
407 |
Return nil if the stream-end is reached before." |
|
408 |
||
409 |
|answerStream| |
|
410 |
||
411 |
answerStream := WriteStream on:(ByteArray new:128). |
|
412 |
[self atEnd] whileFalse:[ |
|
413 |
answerStream nextPut:self next |
|
414 |
]. |
|
415 |
||
416 |
^ answerStream contents |
|
1027 | 417 |
! ! |
418 |
||
419 |
!Base64Coder class methodsFor:'documentation'! |
|
420 |
||
421 |
version |
|
1415
3ef6a2c42611
Use #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
1391
diff
changeset
|
422 |
^ '$Header: /cvs/stx/stx/libbasic2/Base64Coder.st,v 1.12 2004-03-05 19:02:42 stefan Exp $' |
1027 | 423 |
! ! |
1111 | 424 |
|
1027 | 425 |
Base64Coder initialize! |