author | Stefan Vogel <sv@exept.de> |
Tue, 14 Apr 2020 11:36:26 +0200 | |
changeset 5474 | 99d731df2a80 |
parent 5459 | 70e5b29dd7d3 |
permissions | -rw-r--r-- |
4741 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1965 | 3 |
" |
4 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
14 |
"{ Package: 'stx:libbasic2' }" |
|
15 |
||
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
16 |
"{ NameSpace: Smalltalk }" |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
17 |
|
1965 | 18 |
ObjectCoder subclass:#BaseNCoder |
4741 | 19 |
instanceVariableNames:'buffer bits charCount peekByte atEnd lineLimit mapping |
20 |
reverseMapping' |
|
1965 | 21 |
classVariableNames:'' |
22 |
poolDictionaries:'' |
|
23 |
category:'System-Storage' |
|
24 |
! |
|
25 |
||
26 |
!BaseNCoder class methodsFor:'documentation'! |
|
27 |
||
28 |
copyright |
|
29 |
" |
|
30 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
31 |
All Rights Reserved |
|
32 |
||
33 |
This software is furnished under a license and may be used |
|
34 |
only in accordance with the terms of that license and with the |
|
35 |
inclusion of the above copyright notice. This software may not |
|
36 |
be provided or otherwise made available to, or used by, any |
|
37 |
other person. No title to or ownership of the software is |
|
38 |
hereby transferred. |
|
39 |
" |
|
40 |
! |
|
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
Abstract superclass of Base64Coder and Base32Coder |
|
4278 | 45 |
Their main entry point API is |
46 |
<BaseNCoder> encode:aStringOrBytes |
|
47 |
and |
|
48 |
<BaseNCoder> decode:aString |
|
1965 | 49 |
|
4278 | 50 |
If the decoder should return a string, use |
51 |
<BaseNCoder> decodeAsString:aString. |
|
52 |
||
53 |
[examples:] |
|
54 |
Base64Coder encode:'helloWorld' |
|
55 |
||
56 |
Base64Coder decode:'aGVsbG9Xb3JsZA==' |
|
57 |
||
58 |
Base64Coder decodeAsString:'aGVsbG9Xb3JsZA==' |
|
59 |
||
1965 | 60 |
[author:] |
61 |
Stefan Vogel (stefan@zwerg) |
|
62 |
||
63 |
[instance variables:] |
|
64 |
buffer SmallInteger buffered data |
|
65 |
bits SmallInteger Number of valid bits in buffer |
|
66 |
charCount SmallInteger Number of characters since last cr |
|
67 |
atEnd Boolean true if end of Base64 string reached |
|
68 |
||
69 |
[class variables:] |
|
70 |
||
71 |
[see also:] |
|
72 |
||
73 |
" |
|
74 |
! ! |
|
75 |
||
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
76 |
!BaseNCoder class methodsFor:'initialization'! |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
77 |
|
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
78 |
initializeMappings |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
79 |
self subclassResponsibility |
4741 | 80 |
|
81 |
" |
|
82 |
self withAllSubclassesDo:#initialize |
|
83 |
" |
|
84 |
||
85 |
"Modified (comment): / 30-09-2018 / 15:39:16 / Claus Gittinger" |
|
86 |
! |
|
87 |
||
88 |
mapping |
|
89 |
self subclassResponsibility |
|
90 |
||
91 |
"Created: / 30-09-2018 / 15:30:08 / Claus Gittinger" |
|
92 |
! |
|
93 |
||
94 |
reverseMapping |
|
95 |
self subclassResponsibility |
|
96 |
||
97 |
"Created: / 30-09-2018 / 15:30:11 / Claus Gittinger" |
|
98 |
! |
|
99 |
||
100 |
reverseMappingFor:mapping |
|
101 |
"initialize class variables" |
|
102 |
||
103 |
|revMapping| |
|
104 |
||
105 |
revMapping := ByteArray new:128 withAll:255. |
|
106 |
mapping keysAndValuesDo:[:idx :char| |
|
107 |
revMapping at:char codePoint put:idx-1. |
|
108 |
]. |
|
109 |
^ revMapping |
|
110 |
||
111 |
"Created: / 30-09-2018 / 15:34:37 / Claus Gittinger" |
|
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
112 |
! ! |
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
113 |
|
1965 | 114 |
!BaseNCoder class methodsFor:'instance creation'! |
115 |
||
116 |
new |
|
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
117 |
self initializeMappings. |
1965 | 118 |
^ self basicNew initialize |
119 |
! ! |
|
120 |
||
4893 | 121 |
!BaseNCoder class methodsFor:'constants'! |
122 |
||
123 |
lineLimit |
|
124 |
^ 76. "/ RFC 2045 says: max 76 characters in one line |
|
125 |
||
126 |
"Created: / 21-03-2019 / 21:54:41 / Claus Gittinger" |
|
127 |
! ! |
|
128 |
||
2090 | 129 |
!BaseNCoder class methodsFor:'decoding'! |
130 |
||
131 |
decodeAsString:encodedString |
|
132 |
"decode a base-n encoded string. |
|
133 |
We already expect a string instead of a ByteArray" |
|
134 |
||
135 |
^ (self on:encodedString readStream) stringUpToEnd |
|
136 |
! ! |
|
137 |
||
4310 | 138 |
!BaseNCoder class methodsFor:'queries'! |
139 |
||
140 |
isAbstract |
|
141 |
"Return if this class is an abstract class. |
|
142 |
True is returned here for myself only; false for subclasses. |
|
143 |
Abstract subclasses must redefine this again." |
|
144 |
||
145 |
^ self == BaseNCoder. |
|
146 |
! ! |
|
147 |
||
1965 | 148 |
!BaseNCoder methodsFor:'accessing'! |
149 |
||
150 |
lineLimit:something |
|
151 |
"set the line length of the encoded output. |
|
152 |
Default is a line length of 76 characters. |
|
153 |
||
154 |
If nil, no line breaks will be done." |
|
155 |
||
156 |
lineLimit := something. |
|
5459 | 157 |
! |
158 |
||
159 |
mapping:aString |
|
160 |
"the default mapping is |
|
161 |
0 -> A |
|
162 |
1 -> B |
|
163 |
... |
|
164 |
63 -> = |
|
165 |
(see initializeMapping on the class side) |
|
166 |
This accessor allows for different mappings to be used." |
|
167 |
||
168 |
mapping := aString. |
|
169 |
reverseMapping := self class reverseMappingFor:mapping. |
|
170 |
||
171 |
" |
|
172 |
|encoded decoded| |
|
173 |
||
174 |
encoded := Base64Coder encode:'hello world'. 'aGVsbG8gd29ybGQ='. |
|
175 |
decoded := Base64Coder decode:encoded. |
|
176 |
self assert:(decoded asString = 'hello world'). |
|
177 |
" |
|
178 |
||
179 |
" |
|
180 |
|encoded decoded| |
|
181 |
||
182 |
encoded := Base64Coder new |
|
183 |
mapping:'A0B1C2D3E4F5G6H7I8J9K+L/M=NaObPcQdReSfTgUhViWjXkYlZmnopqrstuvwxyz'; |
|
184 |
encodingOf:'hello world'. 'ND+WaDvQbpwZaDIz'. |
|
185 |
decoded := Base64Coder new |
|
186 |
mapping:'A0B1C2D3E4F5G6H7I8J9K+L/M=NaObPcQdReSfTgUhViWjXkYlZmnopqrstuvwxyz'; |
|
187 |
decodingOf:encoded. |
|
188 |
self assert:(decoded asString = 'hello world'). |
|
189 |
" |
|
1965 | 190 |
! ! |
191 |
||
192 |
!BaseNCoder methodsFor:'decoding'! |
|
193 |
||
5459 | 194 |
decodingOf:aString |
195 |
stream := aString readStream. |
|
196 |
^ self stringUpToEnd |
|
197 |
! |
|
198 |
||
1965 | 199 |
next |
200 |
"answer the next decoded byte" |
|
201 |
||
202 |
|b| |
|
203 |
||
204 |
peekByte notNil ifTrue:[ |
|
205 |
b := peekByte. |
|
206 |
peekByte := nil. |
|
207 |
^ b |
|
208 |
]. |
|
209 |
^ self basicNext. |
|
210 |
! |
|
211 |
||
2638 | 212 |
next:count |
213 |
"return the next count bytes of the stream as ByteArray" |
|
214 |
||
215 |
|answerStream |
|
216 |
cnt "{ Class: SmallInteger }" | |
|
217 |
||
218 |
cnt := count. |
|
219 |
answerStream := WriteStream on:(ByteArray new:cnt). |
|
220 |
answerStream signalAtEnd:true. |
|
221 |
1 to:cnt do:[:index | |
|
222 |
|next| |
|
223 |
||
224 |
next := self next. |
|
225 |
next isNil ifTrue:[ |
|
226 |
"if next did not raise EndOfStreamError, we have to do it" |
|
227 |
EndOfStreamError raiseRequestFrom:self. |
|
228 |
"if you proceed, you get what we have already collected" |
|
229 |
^ answerStream contents |
|
230 |
]. |
|
231 |
answerStream nextPut:next. |
|
232 |
]. |
|
233 |
^ answerStream contents |
|
234 |
! |
|
235 |
||
1965 | 236 |
peek |
237 |
"answer the next decoded byte. Do not consume this byte" |
|
238 |
||
239 |
peekByte isNil ifTrue:[ |
|
240 |
peekByte := self basicNext. |
|
241 |
]. |
|
242 |
^ peekByte |
|
243 |
! ! |
|
244 |
||
245 |
!BaseNCoder methodsFor:'encoding'! |
|
246 |
||
4893 | 247 |
encodingOf:anObject with:aParameter |
248 |
lineLimit := aParameter. |
|
249 |
^ super encodingOf:anObject with:aParameter |
|
250 |
||
251 |
"Created: / 21-03-2019 / 22:01:20 / Claus Gittinger" |
|
252 |
! |
|
253 |
||
1965 | 254 |
visitByteArray:aByteArray with:aParameter |
255 |
^ self |
|
3764 | 256 |
nextPutBytes:aByteArray; |
1965 | 257 |
flush. |
258 |
||
259 |
" |
|
3764 | 260 |
Base64Coder encodingOf:#[1 2 3 4 5 6 255] |
1965 | 261 |
" |
262 |
! |
|
263 |
||
264 |
visitObject:anObject with:aParameter |
|
265 |
"not defined. Use nextPut or nextPutAll:. |
|
266 |
Could encode the printString here" |
|
267 |
||
268 |
^ self shouldNotImplement |
|
269 |
! |
|
270 |
||
2160 | 271 |
visitStream:aStream with:aParameter |
3764 | 272 |
aStream copyToEndInto:self. |
2160 | 273 |
self flush. |
274 |
||
275 |
" |
|
276 |
Base64Coder encodingOf:#[1 2 3 4 5 6 255] |
|
277 |
Base64Coder encodingOf:#[1 2 3 4 5 6 255] readStream |
|
278 |
" |
|
279 |
! |
|
280 |
||
1965 | 281 |
visitString:aString with:aParameter |
282 |
^ self |
|
3764 | 283 |
nextPutAll:aString; |
1965 | 284 |
flush. |
285 |
||
286 |
" |
|
287 |
|encoded decoded decoder| |
|
288 |
||
3764 | 289 |
encoded := Base64Coder encode:'hello world'. |
1965 | 290 |
decoded := #[] writeStream. |
291 |
decoder := Base64Coder on:encoded readStream. |
|
292 |
[decoder atEnd] whileFalse:[ |
|
293 |
decoded nextPut:(decoder next). |
|
294 |
]. |
|
295 |
decoded := decoded contents. |
|
296 |
decoded asString. |
|
297 |
" |
|
298 |
! ! |
|
299 |
||
300 |
!BaseNCoder methodsFor:'initialization'! |
|
301 |
||
302 |
emptyWriteStream |
|
303 |
"answer an empty stream. We encode as string" |
|
304 |
||
305 |
^ WriteStream on:(String new:64) |
|
306 |
! |
|
307 |
||
308 |
initialize |
|
309 |
||
4305 | 310 |
<modifier: #super> "must be called if redefined" |
311 |
||
1965 | 312 |
buffer := bits := charCount := 0. |
313 |
lineLimit := 76. "RFC 2045 says: max 76 characters in one line" |
|
314 |
atEnd := false. |
|
4305 | 315 |
|
4741 | 316 |
mapping := self class mapping. |
317 |
reverseMapping := self class reverseMapping. |
|
318 |
||
4305 | 319 |
"Modified: / 08-02-2017 / 00:33:07 / cg" |
4741 | 320 |
"Modified: / 30-09-2018 / 15:29:41 / Claus Gittinger" |
1965 | 321 |
! ! |
322 |
||
323 |
!BaseNCoder methodsFor:'misc'! |
|
324 |
||
325 |
reset |
|
326 |
"reset to initial state" |
|
327 |
||
328 |
super reset. |
|
329 |
buffer := bits := charCount := 0. |
|
330 |
atEnd := false. |
|
331 |
peekByte := nil. |
|
332 |
! ! |
|
333 |
||
334 |
!BaseNCoder methodsFor:'private'! |
|
335 |
||
336 |
basicNext |
|
337 |
"answer the next decoded byte. |
|
338 |
No peekByte handling is done here." |
|
339 |
||
340 |
|b| |
|
341 |
||
342 |
bits == 0 ifTrue:[ |
|
343 |
self fillBuffer. |
|
344 |
bits == 0 ifTrue:[ |
|
345 |
^ stream pastEndRead. |
|
346 |
] |
|
347 |
]. |
|
348 |
b := (buffer bitShift:(8 - bits)) bitAnd:16rFF. |
|
349 |
bits := bits - 8. |
|
350 |
||
351 |
^ b. |
|
352 |
! ! |
|
353 |
||
354 |
!BaseNCoder methodsFor:'queries'! |
|
355 |
||
356 |
atEnd |
|
357 |
"answer true, if no more bytes can be read" |
|
358 |
||
359 |
bits == 0 ifTrue:[ |
|
360 |
atEnd ifTrue:[^ true]. |
|
361 |
self fillBuffer. |
|
362 |
bits == 0 ifTrue:[^ true]. |
|
363 |
]. |
|
364 |
^ false. |
|
365 |
! |
|
366 |
||
367 |
binary |
|
368 |
"switch to binary mode - nothing is done here. |
|
369 |
Defined for compatibility with ExternalStream." |
|
370 |
||
371 |
^ self |
|
372 |
! |
|
373 |
||
4839 | 374 |
binary:beBinaryBool |
375 |
"ExternalStream protocol compatibility" |
|
376 |
||
377 |
^ false "/ no-op, I am not in binary mode |
|
378 |
||
379 |
"Created: / 13-03-2019 / 19:15:17 / Stefan Vogel" |
|
380 |
! |
|
381 |
||
1965 | 382 |
isStream |
383 |
"we simulate a stream" |
|
384 |
||
385 |
^ true |
|
386 |
! ! |
|
387 |
||
388 |
!BaseNCoder methodsFor:'stream compatibility'! |
|
389 |
||
4842 | 390 |
bufferSizeForBulkCopy |
391 |
^ 1024 |
|
392 |
||
393 |
"Created: / 14-03-2019 / 12:58:47 / Stefan Vogel" |
|
394 |
! |
|
395 |
||
4928 | 396 |
nextByte |
397 |
"ExternalStream compatibility" |
|
398 |
||
399 |
^ self next |
|
400 |
||
401 |
"Created: / 27-03-2019 / 23:39:24 / stefan" |
|
402 |
! |
|
403 |
||
404 |
nextBytes:count into:anObject |
|
405 |
"read the next count bytes into an object and return the number of |
|
406 |
bytes read. On EOF, 0 is returned. |
|
407 |
If the receiver is some socket/pipe-like stream, an exception |
|
408 |
is raised if the connection is broken. |
|
409 |
||
410 |
The object must have non-pointer indexed instvars (i.e. it must be |
|
411 |
a ByteArray, String, Float- or DoubleArray). |
|
412 |
If anObject is a string or byteArray and reused, this provides the |
|
413 |
fastest possible physical I/O (since no new objects are allocated). |
|
414 |
||
415 |
Use with care - non object oriented i/o. |
|
416 |
Warning: in general, you cannot use this method to pass data from other |
|
417 |
architectures since it does not care for byte order or float representation." |
|
418 |
||
419 |
^ self nextBytes:count into:anObject startingAt:1 |
|
420 |
||
421 |
"Created: / 27-03-2019 / 23:37:31 / stefan" |
|
422 |
! |
|
423 |
||
424 |
nextBytes:numBytes into:anObject startingAt:initialIndex |
|
425 |
"copy bytes into anObject starting at offset" |
|
426 |
||
427 |
|n "{Class: SmallInteger }"| |
|
428 |
||
429 |
n := 0. |
|
430 |
||
431 |
[n ~= numBytes and:[self atEnd not]] whileTrue:[ |
|
432 |
anObject byteAt:initialIndex+n put:self next. |
|
433 |
n := n + 1. |
|
434 |
]. |
|
435 |
^ n |
|
436 |
||
437 |
"Created: / 27-03-2019 / 23:36:59 / stefan" |
|
438 |
! |
|
439 |
||
440 |
nextBytesInto:anObject startingAt:initialIndex |
|
1965 | 441 |
"copy bytes into anObject starting at offset" |
442 |
||
4928 | 443 |
|n "{Class: SmallInteger }"| |
1965 | 444 |
|
4928 | 445 |
n := 0. |
446 |
||
1965 | 447 |
[self atEnd] whileFalse:[ |
4928 | 448 |
anObject byteAt:initialIndex+n put:self next. |
449 |
n := n + 1. |
|
1965 | 450 |
]. |
4928 | 451 |
^ n |
452 |
||
453 |
"Modified: / 27-03-2019 / 23:48:16 / stefan" |
|
454 |
! |
|
455 |
||
456 |
nextInt32MSB:msbFlag |
|
457 |
"return a signed long (4 bytes) from the stream. |
|
458 |
The receiver must support reading of binary bytes. |
|
459 |
||
460 |
The msbFlag argument controls if the integer is to be read with |
|
461 |
most-significant-byte-first (true) or least-first (false). |
|
462 |
This interface is provided to allow talking to external programs, |
|
463 |
where it's known that the byte order is some definite one. |
|
464 |
If you don't care (i.e. talk to other smalltalks) or you can control the |
|
465 |
order, please use the corresponding xxxNet methods, which use a standard |
|
466 |
network byte order. |
|
467 |
||
468 |
1-to-1 copy from Stream" |
|
469 |
||
470 |
|b1 b2 b3 b4 uval "{ Class: SmallInteger }" val| |
|
471 |
||
472 |
b1 := self nextByte. |
|
473 |
b2 := self nextByte. |
|
474 |
b3 := self nextByte. |
|
475 |
b4 := self nextByte. |
|
476 |
||
477 |
msbFlag ifTrue:[ |
|
478 |
"most significant first" |
|
479 |
uval := (b1 bitShift:8) bitOr:b2. |
|
480 |
uval := (uval bitShift:8) bitOr:b3. |
|
481 |
val := (uval bitShift:8) bitOr:b4. |
|
482 |
] ifFalse:[ |
|
483 |
"least significant first" |
|
484 |
uval := (b4 bitShift:8) bitOr:b3. |
|
485 |
uval := (uval bitShift:8) bitOr:b2. |
|
486 |
val := (uval bitShift:8) bitOr:b1. |
|
487 |
]. |
|
488 |
"change from unsigned 0..FFFFFFFF to signed -80000000..7FFFFFFF" |
|
489 |
||
490 |
val >= 16r80000000 ifTrue:[ |
|
491 |
^ val - 16r100000000 |
|
492 |
]. |
|
493 |
^ val |
|
494 |
||
495 |
" |
|
496 |
|bytes s| |
|
497 |
||
498 |
bytes := #[16rFF 16rFF 16rFF 16rFF]. |
|
499 |
s := bytes readStream. |
|
500 |
Transcript showCR:(s nextInt32MSB:true). |
|
501 |
s reset. |
|
502 |
Transcript showCR:(s nextInt32MSB:false). |
|
503 |
||
504 |
bytes := #[16r12 16r34 16r56 16r78]. |
|
505 |
s := bytes readStream. |
|
506 |
Transcript showCR:(s nextInt32MSB:true). |
|
507 |
s reset. |
|
508 |
Transcript showCR:(s nextInt32MSB:false). |
|
509 |
||
510 |
bytes := #[16r89 16rab 16rcd 16ref]. |
|
511 |
s := bytes readStream. |
|
512 |
Transcript showCR:(s nextInt32MSB:true). |
|
513 |
s reset. |
|
514 |
Transcript showCR:(s nextInt32MSB:false). |
|
515 |
" |
|
516 |
||
517 |
"Created: / 27-03-2019 / 23:38:32 / stefan" |
|
1965 | 518 |
! |
519 |
||
3764 | 520 |
nextPut:aByte |
5168 | 521 |
"encode aByte on the output stream. |
522 |
Answer aByte" |
|
3764 | 523 |
|
5152 | 524 |
self nextPutByte:aByte asInteger. |
5168 | 525 |
^ aByte |
3764 | 526 |
! |
527 |
||
3760 | 528 |
nextPutAll:aCollection startingAt:first to:last |
529 |
"append the elements with index from first to last |
|
3761 | 530 |
of the argument, aCollection onto the receiver." |
3760 | 531 |
|
532 |
aCollection from:first to:last do:[:element | |
|
3764 | 533 |
self nextPutByte:element |
3760 | 534 |
]. |
5152 | 535 |
|
3760 | 536 |
! |
537 |
||
3764 | 538 |
nextPutBytes:aCollectionOfBytes |
539 |
"encode all objects from the argument" |
|
540 |
||
541 |
aCollectionOfBytes do:[:o | |
|
542 |
self nextPutByte:o |
|
543 |
] |
|
544 |
! |
|
545 |
||
4928 | 546 |
skip:numberToSkip |
547 |
"skip numberToSkip objects, return the receiver. |
|
548 |
1-to-1 copy from Stream." |
|
549 |
||
550 |
"don't know how to unread ..." |
|
551 |
numberToSkip < 0 ifTrue:[ |
|
552 |
PositionError raiseRequest. |
|
553 |
^ self |
|
554 |
]. |
|
555 |
numberToSkip timesRepeat:[self next] |
|
556 |
||
557 |
"Created: / 27-03-2019 / 23:43:26 / stefan" |
|
558 |
! |
|
559 |
||
2090 | 560 |
stringUpToEnd |
561 |
"return a collection of the elements up-to the end" |
|
562 |
||
563 |
|answerStream| |
|
564 |
||
565 |
answerStream := WriteStream on:(String new:128). |
|
566 |
peekByte notNil ifTrue:[ |
|
567 |
answerStream nextPut:(Character codePoint:peekByte). |
|
568 |
peekByte := nil. |
|
569 |
]. |
|
570 |
[ |
|
571 |
[bits >= 8] whileTrue:[ |
|
572 |
answerStream nextPut:(Character codePoint:((buffer bitShift:(8 - bits)) bitAnd:16rFF)). |
|
573 |
bits := bits - 8. |
|
574 |
]. |
|
575 |
atEnd ifTrue:[ |
|
576 |
bits ~~ 0 ifTrue:[ |
|
577 |
answerStream nextPut:(Character codePoint:(buffer bitAnd:16rFF)). |
|
578 |
bits := 0. |
|
579 |
] |
|
580 |
] ifFalse:[ |
|
581 |
self fillBuffer. |
|
582 |
]. |
|
583 |
] doWhile:[bits > 0]. |
|
584 |
||
585 |
^ answerStream contents |
|
586 |
! |
|
587 |
||
1965 | 588 |
upToEnd |
589 |
"return a collection of the elements up-to the end" |
|
590 |
||
591 |
|answerStream| |
|
592 |
||
593 |
answerStream := WriteStream on:(ByteArray new:128). |
|
594 |
peekByte notNil ifTrue:[ |
|
595 |
answerStream nextPut:peekByte. |
|
596 |
peekByte := nil. |
|
597 |
]. |
|
598 |
[ |
|
599 |
[bits >= 8] whileTrue:[ |
|
600 |
answerStream nextPut:((buffer bitShift:(8 - bits)) bitAnd:16rFF). |
|
601 |
bits := bits - 8. |
|
602 |
]. |
|
4752 | 603 |
atEnd ifFalse:[ |
1965 | 604 |
self fillBuffer. |
605 |
]. |
|
606 |
] doWhile:[bits > 0]. |
|
607 |
||
608 |
^ answerStream contents |
|
4752 | 609 |
|
610 |
"Modified: / 30-09-2018 / 16:37:03 / Claus Gittinger" |
|
1965 | 611 |
! ! |
612 |
||
613 |
!BaseNCoder methodsFor:'subclass responsibility'! |
|
614 |
||
615 |
fillBuffer |
|
616 |
"fill buffer with next n characters each representing m bits" |
|
617 |
||
618 |
^ self subclassResponsibility. |
|
619 |
! |
|
620 |
||
621 |
flush |
|
622 |
"flush the remaining bits of buffer. |
|
623 |
The number of bits in buffer is not a multiple of m, so we pad |
|
624 |
the buffer and signal that padding has been done via $= characters." |
|
625 |
||
626 |
^ self subclassResponsibility. |
|
627 |
! |
|
628 |
||
3764 | 629 |
nextPutByte:aByte |
1965 | 630 |
"encode aByte on the output stream" |
631 |
||
632 |
^ self subclassResponsibility. |
|
633 |
! ! |
|
634 |
||
635 |
!BaseNCoder class methodsFor:'documentation'! |
|
636 |
||
637 |
version |
|
3760 | 638 |
^ '$Header$' |
3761 | 639 |
! |
640 |
||
641 |
version_CVS |
|
642 |
^ '$Header$' |
|
1965 | 643 |
! ! |
3548
5424405a96ed
initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents:
2638
diff
changeset
|
644 |