author | Stefan Vogel <sv@exept.de> |
Tue, 22 Mar 2016 18:31:38 +0100 | |
changeset 3764 | 1352adead5b5 |
parent 3761 | 43af86dd75ca |
child 4278 | 939053637e6b |
permissions | -rw-r--r-- |
1965 | 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 |
"{ Package: 'stx:libbasic2' }" |
|
13 |
||
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
14 |
"{ NameSpace: Smalltalk }" |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
15 |
|
1965 | 16 |
ObjectCoder subclass:#BaseNCoder |
17 |
instanceVariableNames:'buffer bits charCount peekByte atEnd lineLimit' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'System-Storage' |
|
21 |
! |
|
22 |
||
23 |
!BaseNCoder class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
Abstract superclass of Base64Coder and Base32Coder |
|
42 |
||
43 |
[author:] |
|
44 |
Stefan Vogel (stefan@zwerg) |
|
45 |
||
46 |
[instance variables:] |
|
47 |
buffer SmallInteger buffered data |
|
48 |
bits SmallInteger Number of valid bits in buffer |
|
49 |
charCount SmallInteger Number of characters since last cr |
|
50 |
atEnd Boolean true if end of Base64 string reached |
|
51 |
||
52 |
[class variables:] |
|
53 |
||
54 |
[see also:] |
|
55 |
||
56 |
" |
|
57 |
! ! |
|
58 |
||
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
59 |
!BaseNCoder class methodsFor:'initialization'! |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
60 |
|
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
61 |
initializeMappings |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
62 |
self subclassResponsibility |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
63 |
! ! |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
64 |
|
1965 | 65 |
!BaseNCoder class methodsFor:'instance creation'! |
66 |
||
67 |
new |
|
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
68 |
self initializeMappings. |
1965 | 69 |
^ self basicNew initialize |
70 |
! ! |
|
71 |
||
2090 | 72 |
!BaseNCoder class methodsFor:'decoding'! |
73 |
||
74 |
decodeAsString:encodedString |
|
75 |
"decode a base-n encoded string. |
|
76 |
We already expect a string instead of a ByteArray" |
|
77 |
||
78 |
^ (self on:encodedString readStream) stringUpToEnd |
|
79 |
! ! |
|
80 |
||
1965 | 81 |
!BaseNCoder methodsFor:'accessing'! |
82 |
||
83 |
lineLimit:something |
|
84 |
"set the line length of the encoded output. |
|
85 |
Default is a line length of 76 characters. |
|
86 |
||
87 |
If nil, no line breaks will be done." |
|
88 |
||
89 |
lineLimit := something. |
|
90 |
! ! |
|
91 |
||
92 |
!BaseNCoder methodsFor:'decoding'! |
|
93 |
||
94 |
next |
|
95 |
"answer the next decoded byte" |
|
96 |
||
97 |
|b| |
|
98 |
||
99 |
peekByte notNil ifTrue:[ |
|
100 |
b := peekByte. |
|
101 |
peekByte := nil. |
|
102 |
^ b |
|
103 |
]. |
|
104 |
^ self basicNext. |
|
105 |
! |
|
106 |
||
2638 | 107 |
next:count |
108 |
"return the next count bytes of the stream as ByteArray" |
|
109 |
||
110 |
|answerStream |
|
111 |
cnt "{ Class: SmallInteger }" | |
|
112 |
||
113 |
cnt := count. |
|
114 |
answerStream := WriteStream on:(ByteArray new:cnt). |
|
115 |
answerStream signalAtEnd:true. |
|
116 |
1 to:cnt do:[:index | |
|
117 |
|next| |
|
118 |
||
119 |
next := self next. |
|
120 |
next isNil ifTrue:[ |
|
121 |
"if next did not raise EndOfStreamError, we have to do it" |
|
122 |
EndOfStreamError raiseRequestFrom:self. |
|
123 |
"if you proceed, you get what we have already collected" |
|
124 |
^ answerStream contents |
|
125 |
]. |
|
126 |
answerStream nextPut:next. |
|
127 |
]. |
|
128 |
^ answerStream contents |
|
129 |
! |
|
130 |
||
1965 | 131 |
peek |
132 |
"answer the next decoded byte. Do not consume this byte" |
|
133 |
||
134 |
peekByte isNil ifTrue:[ |
|
135 |
peekByte := self basicNext. |
|
136 |
]. |
|
137 |
^ peekByte |
|
138 |
! ! |
|
139 |
||
140 |
!BaseNCoder methodsFor:'encoding'! |
|
141 |
||
142 |
visitByteArray:aByteArray with:aParameter |
|
143 |
^ self |
|
3764 | 144 |
nextPutBytes:aByteArray; |
1965 | 145 |
flush. |
146 |
||
147 |
" |
|
3764 | 148 |
Base64Coder encodingOf:#[1 2 3 4 5 6 255] |
1965 | 149 |
" |
150 |
! |
|
151 |
||
152 |
visitObject:anObject with:aParameter |
|
153 |
"not defined. Use nextPut or nextPutAll:. |
|
154 |
Could encode the printString here" |
|
155 |
||
156 |
^ self shouldNotImplement |
|
157 |
! |
|
158 |
||
2160 | 159 |
visitStream:aStream with:aParameter |
3764 | 160 |
aStream copyToEndInto:self. |
2160 | 161 |
self flush. |
162 |
||
163 |
" |
|
164 |
Base64Coder encodingOf:#[1 2 3 4 5 6 255] |
|
165 |
Base64Coder encodingOf:#[1 2 3 4 5 6 255] readStream |
|
166 |
" |
|
167 |
! |
|
168 |
||
1965 | 169 |
visitString:aString with:aParameter |
170 |
^ self |
|
3764 | 171 |
nextPutAll:aString; |
1965 | 172 |
flush. |
173 |
||
174 |
" |
|
175 |
|encoded decoded decoder| |
|
176 |
||
3764 | 177 |
encoded := Base64Coder encode:'hello world'. |
1965 | 178 |
decoded := #[] writeStream. |
179 |
decoder := Base64Coder on:encoded readStream. |
|
180 |
[decoder atEnd] whileFalse:[ |
|
181 |
decoded nextPut:(decoder next). |
|
182 |
]. |
|
183 |
decoded := decoded contents. |
|
184 |
decoded asString. |
|
185 |
" |
|
186 |
! ! |
|
187 |
||
188 |
!BaseNCoder methodsFor:'initialization'! |
|
189 |
||
190 |
emptyWriteStream |
|
191 |
"answer an empty stream. We encode as string" |
|
192 |
||
193 |
^ WriteStream on:(String new:64) |
|
194 |
! |
|
195 |
||
196 |
initialize |
|
197 |
||
198 |
buffer := bits := charCount := 0. |
|
199 |
lineLimit := 76. "RFC 2045 says: max 76 characters in one line" |
|
200 |
atEnd := false. |
|
201 |
! ! |
|
202 |
||
203 |
!BaseNCoder methodsFor:'misc'! |
|
204 |
||
205 |
reset |
|
206 |
"reset to initial state" |
|
207 |
||
208 |
super reset. |
|
209 |
buffer := bits := charCount := 0. |
|
210 |
atEnd := false. |
|
211 |
peekByte := nil. |
|
212 |
! ! |
|
213 |
||
214 |
!BaseNCoder methodsFor:'private'! |
|
215 |
||
216 |
basicNext |
|
217 |
"answer the next decoded byte. |
|
218 |
No peekByte handling is done here." |
|
219 |
||
220 |
|b| |
|
221 |
||
222 |
bits == 0 ifTrue:[ |
|
223 |
self fillBuffer. |
|
224 |
bits == 0 ifTrue:[ |
|
225 |
^ stream pastEndRead. |
|
226 |
] |
|
227 |
]. |
|
228 |
b := (buffer bitShift:(8 - bits)) bitAnd:16rFF. |
|
229 |
bits := bits - 8. |
|
230 |
||
231 |
^ b. |
|
232 |
! ! |
|
233 |
||
234 |
!BaseNCoder methodsFor:'queries'! |
|
235 |
||
236 |
atEnd |
|
237 |
"answer true, if no more bytes can be read" |
|
238 |
||
239 |
bits == 0 ifTrue:[ |
|
240 |
atEnd ifTrue:[^ true]. |
|
241 |
self fillBuffer. |
|
242 |
bits == 0 ifTrue:[^ true]. |
|
243 |
]. |
|
244 |
^ false. |
|
245 |
! |
|
246 |
||
247 |
binary |
|
248 |
"switch to binary mode - nothing is done here. |
|
249 |
Defined for compatibility with ExternalStream." |
|
250 |
||
251 |
^ self |
|
252 |
! |
|
253 |
||
254 |
isStream |
|
255 |
"we simulate a stream" |
|
256 |
||
257 |
^ true |
|
258 |
! ! |
|
259 |
||
260 |
!BaseNCoder methodsFor:'stream compatibility'! |
|
261 |
||
262 |
nextBytesInto:anObject startingAt:offset |
|
263 |
"copy bytes into anObject starting at offset" |
|
264 |
||
265 |
|off| |
|
266 |
||
267 |
off := offset. |
|
268 |
[self atEnd] whileFalse:[ |
|
269 |
anObject at:off put:self next. |
|
270 |
off := off + 1. |
|
271 |
]. |
|
272 |
^ off - offset |
|
273 |
! |
|
274 |
||
3764 | 275 |
nextPut:aByte |
276 |
"encode aByte on the output stream" |
|
277 |
||
278 |
^ self nextPutByte:aByte asInteger. |
|
279 |
! |
|
280 |
||
3760 | 281 |
nextPutAll:aCollection startingAt:first to:last |
282 |
"append the elements with index from first to last |
|
3761 | 283 |
of the argument, aCollection onto the receiver." |
3760 | 284 |
|
285 |
aCollection from:first to:last do:[:element | |
|
3764 | 286 |
self nextPutByte:element |
3760 | 287 |
]. |
288 |
^ aCollection |
|
289 |
! |
|
290 |
||
3764 | 291 |
nextPutBytes:aCollectionOfBytes |
292 |
"encode all objects from the argument" |
|
293 |
||
294 |
aCollectionOfBytes do:[:o | |
|
295 |
self nextPutByte:o |
|
296 |
] |
|
297 |
! |
|
298 |
||
2090 | 299 |
stringUpToEnd |
300 |
"return a collection of the elements up-to the end" |
|
301 |
||
302 |
|answerStream| |
|
303 |
||
304 |
answerStream := WriteStream on:(String new:128). |
|
305 |
peekByte notNil ifTrue:[ |
|
306 |
answerStream nextPut:(Character codePoint:peekByte). |
|
307 |
peekByte := nil. |
|
308 |
]. |
|
309 |
[ |
|
310 |
[bits >= 8] whileTrue:[ |
|
311 |
answerStream nextPut:(Character codePoint:((buffer bitShift:(8 - bits)) bitAnd:16rFF)). |
|
312 |
bits := bits - 8. |
|
313 |
]. |
|
314 |
atEnd ifTrue:[ |
|
315 |
bits ~~ 0 ifTrue:[ |
|
316 |
answerStream nextPut:(Character codePoint:(buffer bitAnd:16rFF)). |
|
317 |
bits := 0. |
|
318 |
] |
|
319 |
] ifFalse:[ |
|
320 |
self fillBuffer. |
|
321 |
]. |
|
322 |
] doWhile:[bits > 0]. |
|
323 |
||
324 |
^ answerStream contents |
|
325 |
! |
|
326 |
||
1965 | 327 |
upToEnd |
328 |
"return a collection of the elements up-to the end" |
|
329 |
||
330 |
|answerStream| |
|
331 |
||
332 |
answerStream := WriteStream on:(ByteArray new:128). |
|
333 |
peekByte notNil ifTrue:[ |
|
334 |
answerStream nextPut:peekByte. |
|
335 |
peekByte := nil. |
|
336 |
]. |
|
337 |
[ |
|
338 |
[bits >= 8] whileTrue:[ |
|
339 |
answerStream nextPut:((buffer bitShift:(8 - bits)) bitAnd:16rFF). |
|
340 |
bits := bits - 8. |
|
341 |
]. |
|
342 |
atEnd ifTrue:[ |
|
343 |
bits ~~ 0 ifTrue:[ |
|
344 |
answerStream nextPut:(buffer bitAnd:16rFF). |
|
345 |
bits := 0. |
|
346 |
] |
|
347 |
] ifFalse:[ |
|
348 |
self fillBuffer. |
|
349 |
]. |
|
350 |
] doWhile:[bits > 0]. |
|
351 |
||
352 |
^ answerStream contents |
|
353 |
! ! |
|
354 |
||
355 |
!BaseNCoder methodsFor:'subclass responsibility'! |
|
356 |
||
357 |
fillBuffer |
|
358 |
"fill buffer with next n characters each representing m bits" |
|
359 |
||
360 |
^ self subclassResponsibility. |
|
361 |
! |
|
362 |
||
363 |
flush |
|
364 |
"flush the remaining bits of buffer. |
|
365 |
The number of bits in buffer is not a multiple of m, so we pad |
|
366 |
the buffer and signal that padding has been done via $= characters." |
|
367 |
||
368 |
^ self subclassResponsibility. |
|
369 |
! |
|
370 |
||
3764 | 371 |
nextPutByte:aByte |
1965 | 372 |
"encode aByte on the output stream" |
373 |
||
374 |
^ self subclassResponsibility. |
|
375 |
! ! |
|
376 |
||
377 |
!BaseNCoder class methodsFor:'documentation'! |
|
378 |
||
379 |
version |
|
3760 | 380 |
^ '$Header$' |
3761 | 381 |
! |
382 |
||
383 |
version_CVS |
|
384 |
^ '$Header$' |
|
1965 | 385 |
! ! |
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
386 |