|
| 1 | +" |
| 2 | +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. |
| 3 | +" |
| 4 | +Class { |
| 5 | + #name : #GRCodec, |
| 6 | + #superclass : #GRObject, |
| 7 | + #category : #'Grease-Core-Text' |
| 8 | +} |
| 9 | + |
| 10 | +{ #category : #accessing } |
| 11 | +GRCodec class >> allCodecs [ |
| 12 | + "Answer all codecs supported in this system. This is a collection of codec instances." |
| 13 | + |
| 14 | + ^ self subclasses |
| 15 | + inject: self codecs asArray |
| 16 | + into: [ :result :each | result , each allCodecs ] |
| 17 | +] |
| 18 | + |
| 19 | +{ #category : #private } |
| 20 | +GRCodec class >> basicForEncoding: aString [ |
| 21 | + "Create the actual instance." |
| 22 | + self subclassResponsibility |
| 23 | +] |
| 24 | + |
| 25 | +{ #category : #accessing } |
| 26 | +GRCodec class >> codecs [ |
| 27 | + "Answer a collection of possible codecs of the receiver. To be overridden by concrete subclasses." |
| 28 | + |
| 29 | + ^ #() |
| 30 | +] |
| 31 | + |
| 32 | +{ #category : #'instance creation' } |
| 33 | +GRCodec class >> forEncoding: aString [ |
| 34 | + "Answer a new codec instance for the given encoding name. Raise an WAUnsupportedEncodingError if the encoding name is not supported by this image." |
| 35 | + |
| 36 | + self allSubclassesDo: [ :each | |
| 37 | + (each supportsEncoding: aString) |
| 38 | + ifTrue: [ ^ each basicForEncoding: aString ] ]. |
| 39 | + ^ self unsupportedEncoding: aString |
| 40 | +] |
| 41 | + |
| 42 | +{ #category : #testing } |
| 43 | +GRCodec class >> supportsEncoding: aString [ |
| 44 | + "Answer whether the the given encoding name is supported by this codec class." |
| 45 | + self subclassResponsibility |
| 46 | +] |
| 47 | + |
| 48 | +{ #category : #private } |
| 49 | +GRCodec class >> unsupportedEncoding: aString [ |
| 50 | + "Signal an unsupported encoding." |
| 51 | + |
| 52 | + ^ GRUnsupportedEncodingError signal: 'unsupported encoding: ' , aString |
| 53 | +] |
| 54 | + |
| 55 | +{ #category : #convenience } |
| 56 | +GRCodec >> decode: aString [ |
| 57 | + | readStream writeStream | |
| 58 | + readStream := self decoderFor: aString readStream. |
| 59 | + writeStream := GRPlatform current writeCharacterStreamOn: (String new: aString size). |
| 60 | + [ readStream atEnd ] |
| 61 | + whileFalse: [ writeStream nextPutAll: (readStream next: 1024) ]. |
| 62 | + ^ writeStream contents |
| 63 | +] |
| 64 | + |
| 65 | +{ #category : #conversion } |
| 66 | +GRCodec >> decoderFor: aReadStream [ |
| 67 | + "Wrap aReadStream with an decoder for the codec of the receiver. Answer a read stream that delegates to and shares the state of aReadStream." |
| 68 | + |
| 69 | + self subclassResponsibility |
| 70 | +] |
| 71 | + |
| 72 | +{ #category : #convenience } |
| 73 | +GRCodec >> encode: aString [ |
| 74 | + | writeStream | |
| 75 | + writeStream := self encoderFor: (GRPlatform current |
| 76 | + writeCharacterStreamOn: (self encodedStringClass new: aString size)). |
| 77 | + writeStream nextPutAll: aString. |
| 78 | + ^ writeStream contents |
| 79 | +] |
| 80 | + |
| 81 | +{ #category : #conversion } |
| 82 | +GRCodec >> encodedStringClass [ |
| 83 | + ^ String |
| 84 | +] |
| 85 | + |
| 86 | +{ #category : #conversion } |
| 87 | +GRCodec >> encoderFor: aWriteStream [ |
| 88 | + "Wrap aWriteStream with an encoder for the codec of the receiver. Answer a write stream that delegates to and shares the state of aWriteStream." |
| 89 | + |
| 90 | + self subclassResponsibility |
| 91 | +] |
| 92 | + |
| 93 | +{ #category : #accessing } |
| 94 | +GRCodec >> name [ |
| 95 | + "Answer a human readable string of the receivers encoding policy." |
| 96 | + |
| 97 | + self subclassResponsibility |
| 98 | +] |
| 99 | + |
| 100 | +{ #category : #printing } |
| 101 | +GRCodec >> printOn: aStream [ |
| 102 | + super printOn: aStream. |
| 103 | + aStream nextPutAll: ' name: '; print: self name |
| 104 | +] |
| 105 | + |
| 106 | +{ #category : #accessing } |
| 107 | +GRCodec >> url [ |
| 108 | + "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." |
| 109 | + |
| 110 | + self subclassResponsibility |
| 111 | +] |
0 commit comments