author | Claus Gittinger <cg@exept.de> |
Thu, 01 Oct 2015 17:18:31 +0200 | |
changeset 18794 | d9c30cfc3959 |
parent 18553 | 6a1c042d2aab |
child 18556 | 2c67b0fa6ff5 |
child 19588 | 26236b5c057d |
permissions | -rw-r--r-- |
18552 | 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 |
||
75 |
!ObjectCoder class methodsFor:'encoding'! |
|
76 |
||
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
77 |
decode:anObject |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
78 |
"decode anObject" |
6896 | 79 |
|
80 |
^ (self on:anObject readStream) upToEnd |
|
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==' |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
85 |
" |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
86 |
! |
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 |
decodingOf:anObject |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
89 |
"use encode" |
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 |
<resource: #obsolete> |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
92 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
93 |
^ self decode:anObject |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
94 |
! |
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 |
encode:anObject |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
97 |
"encode of anObject" |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
98 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
99 |
^ self new encodingOf:anObject with:nil |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
100 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
101 |
" |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
102 |
Base64Coder encode:#[1 2 16rFe 16rFF] |
13720 | 103 |
Base64Coder encode:'hello' |
13931 | 104 |
OSI::BERCoder encode:'hello' |
6896 | 105 |
" |
13720 | 106 |
|
13931 | 107 |
"Modified (comment): / 12-01-2012 / 13:51:23 / cg" |
6896 | 108 |
! |
109 |
||
6705 | 110 |
encode:anObject on:aStream |
18552 | 111 |
self encode:anObject on:aStream with:nil |
6705 | 112 |
! |
113 |
||
114 |
encode:anObject on:aStream with:info |
|
18552 | 115 |
self new encode:anObject on:aStream with:info |
6705 | 116 |
! |
117 |
||
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
118 |
encode:anObject with:aParameter |
5477 | 119 |
"return the encoding of anObject" |
120 |
||
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
121 |
^ self new encodingOf:anObject with:aParameter |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
122 |
! |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
123 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
124 |
encodingOf:anObject |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
125 |
"use #encode:" |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
126 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
127 |
<resource: #obsolete> |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
128 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
129 |
^ self encode:anObject |
6896 | 130 |
|
131 |
" |
|
132 |
Base64Coder encodingOf:#[1 2 16rFe 16rFF] |
|
133 |
" |
|
5477 | 134 |
! |
135 |
||
136 |
encodingOf:anObject with:aParameter |
|
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
137 |
"use #encode:with:" |
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
138 |
|
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
139 |
<resource: #obsolete> |
5477 | 140 |
|
18552 | 141 |
^ self encode:anObject with:aParameter |
5477 | 142 |
! ! |
143 |
||
6479 | 144 |
!ObjectCoder methodsFor:'accessing'! |
145 |
||
146 |
contents |
|
147 |
||
148 |
self flush. |
|
149 |
^ stream contents |
|
150 |
! ! |
|
151 |
||
5477 | 152 |
!ObjectCoder methodsFor:'decoding'! |
153 |
||
154 |
next |
|
155 |
"read, decode and return the next object" |
|
156 |
||
157 |
^ self subclassResponsibility |
|
158 |
||
159 |
||
160 |
! ! |
|
161 |
||
162 |
!ObjectCoder methodsFor:'encoding'! |
|
163 |
||
18553 | 164 |
encode:anObject on:aStream |
165 |
self encode:anObject on:aStream with:nil |
|
166 |
! |
|
167 |
||
18552 | 168 |
encode:anObject on:aStream with:info |
169 |
self stream:aStream. |
|
170 |
self startEncoding. |
|
171 |
anObject acceptVisitor:self with:info. |
|
172 |
self endEncoding. |
|
173 |
! |
|
174 |
||
5477 | 175 |
encodingOf:anObject |
176 |
"answer the encoded argument anObject" |
|
177 |
||
178 |
^ self encodingOf:anObject with:nil |
|
179 |
||
180 |
||
181 |
! |
|
182 |
||
183 |
encodingOf:anObject with:aParameter |
|
184 |
"answer the encoded argument anObject" |
|
185 |
||
186 |
stream isNil ifTrue:[ |
|
6595
94466b4392d4
#emptyWriteStream and #encodeSeqenceableCollection
Stefan Vogel <sv@exept.de>
parents:
6594
diff
changeset
|
187 |
stream := self emptyWriteStream. |
5477 | 188 |
] ifFalse:[ |
189 |
stream reset. |
|
190 |
]. |
|
18552 | 191 |
self startEncoding. |
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
192 |
anObject acceptVisitor:self with:aParameter. |
18552 | 193 |
self endEncoding. |
8621
b57b1e5b2eca
Define #encode: and #decode: instead of #encodingOf: an #decodingOf:
Stefan Vogel <sv@exept.de>
parents:
8402
diff
changeset
|
194 |
|
5477 | 195 |
^ stream contents. |
196 |
! |
|
197 |
||
18552 | 198 |
endEncoding |
199 |
"redefinable - allows subclass to create a file trailer or similar stuff" |
|
200 |
||
201 |
"/ intentionally left blank here |
|
202 |
! |
|
203 |
||
5477 | 204 |
nextPut:anObject |
205 |
"encode anObject onto my stream" |
|
206 |
||
8402 | 207 |
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
|
208 |
! |
5477 | 209 |
|
8369
4ed7a6453f44
only one method sends encodeOn (to allow for easier redefinition)
Claus Gittinger <cg@exept.de>
parents:
7257
diff
changeset
|
210 |
nextPut:anObject with:aParameter |
4ed7a6453f44
only one method sends encodeOn (to allow for easier redefinition)
Claus Gittinger <cg@exept.de>
parents:
7257
diff
changeset
|
211 |
"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
|
212 |
|
8402 | 213 |
anObject acceptVisitor:self with:aParameter. |
5477 | 214 |
! |
215 |
||
216 |
nextPutAll:aCollectionOfObjects |
|
217 |
"encode all objects from the argument" |
|
218 |
||
219 |
aCollectionOfObjects do:[:o | |
|
220 |
self nextPut:o |
|
221 |
] |
|
222 |
||
18552 | 223 |
! |
224 |
||
225 |
startEncoding |
|
226 |
"redefinable - allows subclass to create a file header or similar stuff" |
|
227 |
||
228 |
"/ intentionally left blank here |
|
5477 | 229 |
! ! |
230 |
||
8402 | 231 |
!ObjectCoder methodsFor:'encoding-smalltalk types'! |
232 |
||
233 |
visitBlock:aBlock with:aParameter |
|
234 |
"encoding of blocks is rather difficult and an error by default. |
|
235 |
If your encoder supports this, redefine it there" |
|
236 |
||
237 |
self error:'encoding of blocks is not supported' |
|
238 |
! ! |
|
239 |
||
5477 | 240 |
!ObjectCoder methodsFor:'initialization'! |
241 |
||
242 |
close |
|
243 |
"close the underlying stream" |
|
244 |
||
245 |
stream notNil ifTrue:[ |
|
246 |
stream close. |
|
247 |
]. |
|
248 |
||
249 |
||
250 |
! |
|
251 |
||
6595
94466b4392d4
#emptyWriteStream and #encodeSeqenceableCollection
Stefan Vogel <sv@exept.de>
parents:
6594
diff
changeset
|
252 |
emptyWriteStream |
94466b4392d4
#emptyWriteStream and #encodeSeqenceableCollection
Stefan Vogel <sv@exept.de>
parents:
6594
diff
changeset
|
253 |
"answer an empty stream for writing the encoded object" |
6594 | 254 |
|
6595
94466b4392d4
#emptyWriteStream and #encodeSeqenceableCollection
Stefan Vogel <sv@exept.de>
parents:
6594
diff
changeset
|
255 |
^ self subclassResponsibility |
6594 | 256 |
! |
257 |
||
6479 | 258 |
flush |
259 |
"flush possibly internally buffered data. |
|
260 |
Nothing is done by default. Subclasses may redefine this" |
|
261 |
||
262 |
^ self |
|
263 |
! |
|
264 |
||
5477 | 265 |
reset |
266 |
"reset the coder" |
|
267 |
||
268 |
stream notNil ifTrue:[ |
|
269 |
stream reset. |
|
270 |
]. |
|
271 |
||
272 |
||
273 |
! ! |
|
274 |
||
7257 | 275 |
!ObjectCoder methodsFor:'private-accessing'! |
5477 | 276 |
|
277 |
stream:aStream |
|
5617 | 278 |
stream := aStream. |
5477 | 279 |
! ! |
280 |
||
281 |
!ObjectCoder class methodsFor:'documentation'! |
|
282 |
||
283 |
version |
|
18552 | 284 |
^ '$Header$' |
18553 | 285 |
! |
286 |
||
287 |
version_CVS |
|
288 |
^ '$Header$' |
|
5477 | 289 |
! ! |
18552 | 290 |