forked from SeasideSt/Grease
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGRCodec.class.st
More file actions
111 lines (89 loc) · 3.32 KB
/
GRCodec.class.st
File metadata and controls
111 lines (89 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"
A codec defines how Seaside communicates without the outside world and how outside data is converted into the image (decoding) and back outside the image (encoding). The codec is essentially a stream factory that provides wrappers around standard streams. All streams do support binary mode for non-converted transfer.
"
Class {
#name : #GRCodec,
#superclass : #GRObject,
#category : 'Grease-Core-Text'
}
{ #category : #accessing }
GRCodec class >> allCodecs [
"Answer all codecs supported in this system. This is a collection of codec instances."
^ self subclasses
inject: self codecs asArray
into: [ :result :each | result , each allCodecs ]
]
{ #category : #private }
GRCodec class >> basicForEncoding: aString [
"Create the actual instance."
self subclassResponsibility
]
{ #category : #accessing }
GRCodec class >> codecs [
"Answer a collection of possible codecs of the receiver. To be overridden by concrete subclasses."
^ #()
]
{ #category : #'instance creation' }
GRCodec class >> forEncoding: aString [
"Answer a new codec instance for the given encoding name. Raise an WAUnsupportedEncodingError if the encoding name is not supported by this image."
self allSubclassesDo: [ :each |
(each supportsEncoding: aString)
ifTrue: [ ^ each basicForEncoding: aString ] ].
^ self unsupportedEncoding: aString
]
{ #category : #testing }
GRCodec class >> supportsEncoding: aString [
"Answer whether the the given encoding name is supported by this codec class."
self subclassResponsibility
]
{ #category : #private }
GRCodec class >> unsupportedEncoding: aString [
"Signal an unsupported encoding."
^ GRUnsupportedEncodingError signal: 'unsupported encoding: ' , aString
]
{ #category : #convenience }
GRCodec >> decode: aString [
| readStream writeStream |
readStream := self decoderFor: aString readStream.
writeStream := GRPlatform current writeCharacterStreamOn: (String new: aString size).
[ readStream atEnd ]
whileFalse: [ writeStream nextPutAll: (readStream next: 1024) ].
^ writeStream contents
]
{ #category : #conversion }
GRCodec >> decoderFor: aReadStream [
"Wrap aReadStream with an decoder for the codec of the receiver. Answer a read stream that delegates to and shares the state of aReadStream."
self subclassResponsibility
]
{ #category : #convenience }
GRCodec >> encode: aString [
| writeStream |
writeStream := self encoderFor: (GRPlatform current
writeCharacterStreamOn: (self encodedStringClass new: aString size)).
writeStream nextPutAll: aString.
^ writeStream contents
]
{ #category : #conversion }
GRCodec >> encodedStringClass [
^ String
]
{ #category : #conversion }
GRCodec >> encoderFor: aWriteStream [
"Wrap aWriteStream with an encoder for the codec of the receiver. Answer a write stream that delegates to and shares the state of aWriteStream."
self subclassResponsibility
]
{ #category : #accessing }
GRCodec >> name [
"Answer a human readable string of the receivers encoding policy."
self subclassResponsibility
]
{ #category : #printing }
GRCodec >> printOn: aStream [
super printOn: aStream.
aStream nextPutAll: ' name: '; print: self name
]
{ #category : #accessing }
GRCodec >> url [
"Answer a codec that is responsible to encode and decode URLs. In most cases an UTF-8 codec is the only valid choice, but subclasses might decide to do something else."
self subclassResponsibility
]