author | Claus Gittinger <cg@exept.de> |
Mon, 27 May 2019 16:53:37 +0200 | |
changeset 24192 | 18512bf68422 |
parent 23951 | 1cbf730208f8 |
child 24637 | 9d7318805f06 |
permissions | -rw-r--r-- |
23951 | 1 |
"{ Encoding: utf8 }" |
2 |
||
5477 | 3 |
" |
4 |
COPYRIGHT (c) 2000 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 |
" |
|
5543
02ae5b02191a
re-checkin to force packageID change
Claus Gittinger <cg@exept.de>
parents:
5477
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
5477 | 15 |
|
18552 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
8402 | 18 |
AspectVisitor subclass:#ObjectCoder |
5477 | 19 |
instanceVariableNames:'stream' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'System-Storage' |
|
23 |
! |
|
24 |
||
25 |
!ObjectCoder class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 2000 by eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
||
40 |
||
41 |
! |
|
42 |
||
43 |
documentation |
|
44 |
" |
|
45 |
This is an abstract class. Subclasses implement encoding and decoding |
|
46 |
of Objects onto resp. from a stream. Possible coders are ASN.1/BER, |
|
47 |
CORBA/CDR, BOSS, RMI ... |
|
48 |
||
49 |
Classes which implement basic types (Boolean, Integer, Float, ...) |
|
8402 | 50 |
implement the visitor methods #acceptVisitor:with:, which dispatches onto an ObjectCoder. |
5477 | 51 |
|
52 |
[author:] |
|
53 |
Stefan Vogel |
|
54 |
||
55 |
[see also:] |
|
56 |
OSI::ASN1_Coder |
|
57 |
||
58 |
[instance variables:] |
|
59 |
stream <Stream> the stream we read/write the encodings from/to |
|
60 |
||
61 |
[class variables:] |
|
62 |
" |
|
63 |
! ! |
|
64 |
||
65 |
!ObjectCoder class methodsFor:'instance creation'! |
|
66 |
||
67 |
on:aStream |
|
68 |
"return an encoder/decoder for a stream" |
|
69 |
||
70 |
^ self new stream:aStream |
|
71 |
||
72 |
||
73 |
! ! |
|
74 |
||
21347 | 75 |
!ObjectCoder class methodsFor:'encoding / decoding'! |
5477 | 76 |
|
21344 | 77 |
decode:aByteArrayOrString |
78 |
"decode aByteArrayOrString" |
|
6896 | 79 |
|
21344 | 80 |
^ (self on:aByteArrayOrString readStream) upToEnd |
6896 | 81 |
|
82 |
" |
|
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
83 |
Base64Coder encode:#[1 2 16rFe 16rFF] |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
84 |
Base64Coder decode:'AQL+/w==' |
19588 | 85 |
(Base64Coder decode:(Base64Coder encode:#[1 2 16rFe 16rFF])) = #[1 2 16rFe 16rFF] |
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
86 |
" |
21344 | 87 |
|
88 |
"Modified (comment): / 05-02-2017 / 17:00:49 / cg" |
|
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
89 |
! |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
90 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
91 |
decodingOf:anObject |
21344 | 92 |
"use decode" |
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
93 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
94 |
<resource: #obsolete> |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
95 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
96 |
^ self decode:anObject |
21344 | 97 |
|
98 |
"Modified (comment): / 05-02-2017 / 17:01:03 / cg" |
|
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
99 |
! |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
100 |
|
21346 | 101 |
encode:anObject |
102 |
"encode anObject. |
|
23951 | 103 |
Some subclasses (eg. XMLCoder) allow arbitrary objects to be encoded/decoded, |
21346 | 104 |
others don't (eg. Base64Coder)" |
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
105 |
|
21346 | 106 |
^ self new encodingOf:anObject with:nil |
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
107 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
108 |
" |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
109 |
Base64Coder encode:#[1 2 16rFe 16rFF] |
13720 | 110 |
Base64Coder encode:'hello' |
13931 | 111 |
OSI::BERCoder encode:'hello' |
6896 | 112 |
" |
13720 | 113 |
|
21346 | 114 |
"Modified (comment): / 06-02-2017 / 09:24:40 / cg" |
23951 | 115 |
"Modified (comment): / 21-03-2019 / 21:57:50 / Claus Gittinger" |
6896 | 116 |
! |
117 |
||
21346 | 118 |
encode:anObject on:aStream |
119 |
"encode anObject onto a stream |
|
23951 | 120 |
Some subclasses (eg. XMLCoder) allow arbitrary objects to be encoded/decoded, |
21346 | 121 |
others don't (eg. Base64Coder)" |
21344 | 122 |
|
23951 | 123 |
^ self new encode:anObject on:aStream with:nil |
21344 | 124 |
|
21346 | 125 |
"Modified (format): / 06-02-2017 / 09:25:31 / cg" |
23951 | 126 |
"Modified (comment): / 21-03-2019 / 21:57:47 / Claus Gittinger" |
6705 | 127 |
! |
128 |
||
21346 | 129 |
encode:anObject on:aStream with:info |
130 |
"encode anObject onto a stream |
|
23951 | 131 |
Some subclasses (eg. XMLCoder) allow arbitrary objects to be encoded/decoded, |
21346 | 132 |
others don't (eg. Base64Coder)" |
21344 | 133 |
|
23951 | 134 |
^ self new encode:anObject on:aStream with:info |
21344 | 135 |
|
21346 | 136 |
"Modified (format): / 06-02-2017 / 09:25:27 / cg" |
23951 | 137 |
"Modified (comment): / 21-03-2019 / 21:57:44 / Claus Gittinger" |
6705 | 138 |
! |
139 |
||
21346 | 140 |
encode:anObject with:aParameter |
141 |
"encode anObject onto a stream |
|
23951 | 142 |
Some subclasses (eg. XMLCoder) allow arbitrary objects to be encoded/decoded, |
21346 | 143 |
others don't (eg. Base64Coder)" |
5477 | 144 |
|
21346 | 145 |
^ self new encodingOf:anObject with:aParameter |
21344 | 146 |
|
21346 | 147 |
"Modified (format): / 06-02-2017 / 09:25:20 / cg" |
23951 | 148 |
"Modified (comment): / 21-03-2019 / 21:57:41 / Claus Gittinger" |
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
149 |
! |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
150 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
151 |
encodingOf:anObject |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
152 |
"use #encode:" |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
153 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
154 |
<resource: #obsolete> |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
155 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
156 |
^ self encode:anObject |
6896 | 157 |
|
158 |
" |
|
159 |
Base64Coder encodingOf:#[1 2 16rFe 16rFF] |
|
160 |
" |
|
5477 | 161 |
! |
162 |
||
163 |
encodingOf:anObject with:aParameter |
|
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
164 |
"use #encode:with:" |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
165 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
166 |
<resource: #obsolete> |
5477 | 167 |
|
18552 | 168 |
^ self encode:anObject with:aParameter |
5477 | 169 |
! ! |
170 |
||
6479 | 171 |
!ObjectCoder methodsFor:'accessing'! |
172 |
||
173 |
contents |
|
174 |
||
175 |
self flush. |
|
176 |
^ stream contents |
|
177 |
! ! |
|
178 |
||
5477 | 179 |
!ObjectCoder methodsFor:'decoding'! |
180 |
||
181 |
next |
|
182 |
"read, decode and return the next object" |
|
183 |
||
184 |
^ self subclassResponsibility |
|
185 |
||
186 |
||
187 |
! ! |
|
188 |
||
189 |
!ObjectCoder methodsFor:'encoding'! |
|
190 |
||
18553 | 191 |
encode:anObject on:aStream |
192 |
self encode:anObject on:aStream with:nil |
|
193 |
! |
|
194 |
||
18552 | 195 |
encode:anObject on:aStream with:info |
196 |
self stream:aStream. |
|
197 |
self startEncoding. |
|
198 |
anObject acceptVisitor:self with:info. |
|
199 |
self endEncoding. |
|
200 |
! |
|
201 |
||
5477 | 202 |
encodingOf:anObject |
203 |
"answer the encoded argument anObject" |
|
204 |
||
205 |
^ self encodingOf:anObject with:nil |
|
206 |
||
207 |
||
208 |
! |
|
209 |
||
210 |
encodingOf:anObject with:aParameter |
|
211 |
"answer the encoded argument anObject" |
|
212 |
||
213 |
stream isNil ifTrue:[ |
|
6595
94466b4392d4
#emptyWriteStream and #encodeSeqenceableCollection
Stefan Vogel <sv@exept.de>
parents:
6594
diff
changeset
|
214 |
stream := self emptyWriteStream. |
5477 | 215 |
] ifFalse:[ |
216 |
stream reset. |
|
217 |
]. |
|
18552 | 218 |
self startEncoding. |
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
219 |
anObject acceptVisitor:self with:aParameter. |
18552 | 220 |
self endEncoding. |
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
221 |
|
5477 | 222 |
^ stream contents. |
223 |
! |
|
224 |
||
18552 | 225 |
endEncoding |
226 |
"redefinable - allows subclass to create a file trailer or similar stuff" |
|
227 |
||
228 |
"/ intentionally left blank here |
|
229 |
! |
|
230 |
||
5477 | 231 |
nextPut:anObject |
232 |
"encode anObject onto my stream" |
|
233 |
||
19588 | 234 |
self nextPut:anObject with:nil |
8369
4ed7a6453f44
only one method sends encodeOn (to allow for easier redefinition)
Claus Gittinger <cg@exept.de>
parents:
7257
diff
changeset
|
235 |
! |
5477 | 236 |
|
8369
4ed7a6453f44
only one method sends encodeOn (to allow for easier redefinition)
Claus Gittinger <cg@exept.de>
parents:
7257
diff
changeset
|
237 |
nextPut:anObject with:aParameter |
4ed7a6453f44
only one method sends encodeOn (to allow for easier redefinition)
Claus Gittinger <cg@exept.de>
parents:
7257
diff
changeset
|
238 |
"encode anObject onto my stream" |
4ed7a6453f44
only one method sends encodeOn (to allow for easier redefinition)
Claus Gittinger <cg@exept.de>
parents:
7257
diff
changeset
|
239 |
|
8402 | 240 |
anObject acceptVisitor:self with:aParameter. |
5477 | 241 |
! |
242 |
||
243 |
nextPutAll:aCollectionOfObjects |
|
244 |
"encode all objects from the argument" |
|
245 |
||
246 |
aCollectionOfObjects do:[:o | |
|
247 |
self nextPut:o |
|
248 |
] |
|
249 |
||
18552 | 250 |
! |
251 |
||
252 |
startEncoding |
|
253 |
"redefinable - allows subclass to create a file header or similar stuff" |
|
254 |
||
255 |
"/ intentionally left blank here |
|
5477 | 256 |
! ! |
257 |
||
8402 | 258 |
!ObjectCoder methodsFor:'encoding-smalltalk types'! |
259 |
||
260 |
visitBlock:aBlock with:aParameter |
|
261 |
"encoding of blocks is rather difficult and an error by default. |
|
262 |
If your encoder supports this, redefine it there" |
|
263 |
||
264 |
self error:'encoding of blocks is not supported' |
|
265 |
! ! |
|
266 |
||
5477 | 267 |
!ObjectCoder methodsFor:'initialization'! |
268 |
||
269 |
close |
|
270 |
"close the underlying stream" |
|
271 |
||
272 |
stream notNil ifTrue:[ |
|
273 |
stream close. |
|
274 |
]. |
|
275 |
||
276 |
||
277 |
! |
|
278 |
||
6595
94466b4392d4
#emptyWriteStream and #encodeSeqenceableCollection
Stefan Vogel <sv@exept.de>
parents:
6594
diff
changeset
|
279 |
emptyWriteStream |
94466b4392d4
#emptyWriteStream and #encodeSeqenceableCollection
Stefan Vogel <sv@exept.de>
parents:
6594
diff
changeset
|
280 |
"answer an empty stream for writing the encoded object" |
6594 | 281 |
|
6595
94466b4392d4
#emptyWriteStream and #encodeSeqenceableCollection
Stefan Vogel <sv@exept.de>
parents:
6594
diff
changeset
|
282 |
^ self subclassResponsibility |
6594 | 283 |
! |
284 |
||
6479 | 285 |
flush |
286 |
"flush possibly internally buffered data. |
|
287 |
Nothing is done by default. Subclasses may redefine this" |
|
288 |
||
289 |
^ self |
|
290 |
! |
|
291 |
||
5477 | 292 |
reset |
293 |
"reset the coder" |
|
294 |
||
295 |
stream notNil ifTrue:[ |
|
296 |
stream reset. |
|
297 |
]. |
|
298 |
||
299 |
||
300 |
! ! |
|
301 |
||
7257 | 302 |
!ObjectCoder methodsFor:'private-accessing'! |
5477 | 303 |
|
19598 | 304 |
stream |
305 |
"return my input or output stream" |
|
306 |
||
307 |
^ stream |
|
308 |
! |
|
309 |
||
5477 | 310 |
stream:aStream |
19598 | 311 |
"set my input or output stream" |
312 |
||
5617 | 313 |
stream := aStream. |
5477 | 314 |
! ! |
315 |
||
316 |
!ObjectCoder class methodsFor:'documentation'! |
|
317 |
||
318 |
version |
|
18552 | 319 |
^ '$Header$' |
18553 | 320 |
! |
321 |
||
322 |
version_CVS |
|
323 |
^ '$Header$' |
|
5477 | 324 |
! ! |
18552 | 325 |