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