BaseNCoder.st
author Stefan Vogel <sv@exept.de>
Wed, 01 Aug 2018 15:12:02 +0200
changeset 4708 713e81a051ec
parent 4310 5f509afc6907
child 4741 bfb8e4d34f3e
permissions -rw-r--r--
#BUGFIX by stefan class: List removed: #synchronizationSemaphore: changed: #asSharedCollection #beSynchronized Do not make me implicitly #beSynchronized, if someone sends a #synchronized: message to myself.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     1
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 2002 by eXept Software AG
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     3
              All Rights Reserved
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     4
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    10
 hereby transferred.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    11
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    13
3548
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    14
"{ NameSpace: Smalltalk }"
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    15
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    16
ObjectCoder subclass:#BaseNCoder
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:'buffer bits charCount peekByte atEnd lineLimit'
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    20
	category:'System-Storage'
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    21
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    22
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    23
!BaseNCoder class methodsFor:'documentation'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    24
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    25
copyright
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    26
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    27
 COPYRIGHT (c) 2002 by eXept Software AG
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    28
              All Rights Reserved
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    29
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    30
 This software is furnished under a license and may be used
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    31
 only in accordance with the terms of that license and with the
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    33
 be provided or otherwise made available to, or used by, any
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    34
 other person.  No title to or ownership of the software is
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    35
 hereby transferred.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    36
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    37
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    38
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    39
documentation
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    40
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    41
    Abstract superclass of Base64Coder and Base32Coder
4278
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    42
    Their main entry point API is 
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    43
        <BaseNCoder> encode:aStringOrBytes
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    44
    and
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    45
        <BaseNCoder> decode:aString
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    46
4278
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    47
    If the decoder should return a string, use
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    48
        <BaseNCoder> decodeAsString:aString.
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    49
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    50
    [examples:]
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    51
        Base64Coder encode:'helloWorld'
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    52
        
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    53
        Base64Coder decode:'aGVsbG9Xb3JsZA=='
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    54
        
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    55
        Base64Coder decodeAsString:'aGVsbG9Xb3JsZA=='
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    56
        
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    57
    [author:]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    58
        Stefan Vogel (stefan@zwerg)
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    59
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    60
    [instance variables:]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    61
        buffer          SmallInteger   buffered data
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    62
        bits            SmallInteger   Number of valid bits in buffer
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    63
        charCount       SmallInteger   Number of characters since last cr
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    64
        atEnd           Boolean        true if end of Base64 string reached
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    65
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    66
    [class variables:]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    67
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    68
    [see also:]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    69
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    70
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    71
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    72
3548
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    73
!BaseNCoder class methodsFor:'initialization'!
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    74
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    75
initializeMappings
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    76
    self subclassResponsibility
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    77
! !
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    78
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    79
!BaseNCoder class methodsFor:'instance creation'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    80
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    81
new
3548
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    82
   self initializeMappings.
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    83
   ^ self basicNew initialize
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    84
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    85
2090
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    86
!BaseNCoder class methodsFor:'decoding'!
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    87
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    88
decodeAsString:encodedString
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    89
    "decode a base-n encoded string.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    90
     We already expect a string instead of a ByteArray"
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    91
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    92
    ^ (self on:encodedString readStream) stringUpToEnd
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    93
! !
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
    94
4310
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
    95
!BaseNCoder class methodsFor:'queries'!
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
    96
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
    97
isAbstract
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
    98
    "Return if this class is an abstract class.
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
    99
     True is returned here for myself only; false for subclasses.
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   100
     Abstract subclasses must redefine this again."
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   101
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   102
    ^ self == BaseNCoder.
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   103
! !
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   104
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   105
!BaseNCoder methodsFor:'accessing'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   106
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   107
lineLimit:something
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   108
    "set the line length of the encoded output.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   109
     Default is a line length of 76 characters.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   110
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   111
     If nil, no line breaks will be done."
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   112
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   113
    lineLimit := something.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   114
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   115
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   116
!BaseNCoder methodsFor:'decoding'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   117
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   118
next
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   119
    "answer the next decoded byte"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   120
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   121
    |b|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   122
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   123
    peekByte notNil ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   124
        b := peekByte.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   125
        peekByte := nil.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   126
        ^ b
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   127
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   128
    ^ self basicNext.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   129
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   130
2638
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   131
next:count
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   132
    "return the next count bytes of the stream as ByteArray"
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   133
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   134
    |answerStream 
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   135
     cnt  "{ Class: SmallInteger }" |
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   136
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   137
    cnt := count.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   138
    answerStream := WriteStream on:(ByteArray new:cnt).
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   139
    answerStream signalAtEnd:true.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   140
    1 to:cnt do:[:index |
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   141
        |next|
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   142
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   143
        next := self next.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   144
        next isNil ifTrue:[
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   145
            "if next did not raise EndOfStreamError, we have to do it"
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   146
            EndOfStreamError raiseRequestFrom:self.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   147
            "if you proceed, you get what we have already collected"
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   148
            ^ answerStream contents
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   149
        ].
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   150
        answerStream nextPut:next.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   151
    ].
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   152
    ^ answerStream contents
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   153
!
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   154
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   155
peek
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   156
    "answer the next decoded byte. Do not consume this byte"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   157
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   158
    peekByte isNil ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   159
        peekByte := self basicNext.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   160
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   161
    ^ peekByte
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   162
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   163
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   164
!BaseNCoder methodsFor:'encoding'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   165
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   166
visitByteArray:aByteArray with:aParameter 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   167
    ^ self
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   168
        nextPutBytes:aByteArray;
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   169
        flush.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   170
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   171
    "
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   172
      Base64Coder encodingOf:#[1 2 3 4 5 6 255]
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   173
    "
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   174
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   175
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   176
visitObject:anObject with:aParameter
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   177
    "not defined. Use nextPut or nextPutAll:.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   178
     Could encode the printString here"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   179
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   180
    ^ self shouldNotImplement
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   181
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   182
2160
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   183
visitStream:aStream with:aParameter
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   184
    aStream copyToEndInto:self.
2160
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   185
    self flush.
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   186
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   187
    "
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   188
      Base64Coder encodingOf:#[1 2 3 4 5 6 255]
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   189
      Base64Coder encodingOf:#[1 2 3 4 5 6 255] readStream
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   190
    "
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   191
!
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   192
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   193
visitString:aString with:aParameter 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   194
    ^ self
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   195
        nextPutAll:aString;
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   196
        flush.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   197
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   198
    "
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   199
      |encoded decoded decoder|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   200
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   201
      encoded := Base64Coder encode:'hello world'.  
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   202
      decoded := #[] writeStream.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   203
      decoder := Base64Coder on:encoded readStream.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   204
      [decoder atEnd] whileFalse:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   205
          decoded nextPut:(decoder next).
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   206
      ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   207
      decoded := decoded contents.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   208
      decoded asString.    
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   209
    "
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   210
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   211
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   212
!BaseNCoder methodsFor:'initialization'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   213
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   214
emptyWriteStream
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   215
    "answer an empty stream. We encode as string"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   216
    
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   217
    ^ WriteStream on:(String new:64)
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   218
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   219
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   220
initialize
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   221
4305
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   222
    <modifier: #super> "must be called if redefined"
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   223
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   224
    buffer := bits := charCount := 0.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   225
    lineLimit := 76.   "RFC 2045 says: max 76 characters in one line"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   226
    atEnd := false.
4305
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   227
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   228
    "Modified: / 08-02-2017 / 00:33:07 / cg"
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   229
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   230
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   231
!BaseNCoder methodsFor:'misc'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   232
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   233
reset
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   234
    "reset to initial state"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   235
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   236
    super reset.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   237
    buffer := bits := charCount := 0.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   238
    atEnd := false.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   239
    peekByte := nil.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   240
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   241
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   242
!BaseNCoder methodsFor:'private'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   243
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   244
basicNext
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   245
    "answer the next decoded byte. 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   246
     No peekByte handling is done here."
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   247
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   248
    |b|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   249
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   250
    bits == 0 ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   251
        self fillBuffer.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   252
        bits == 0 ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   253
            ^ stream pastEndRead.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   254
        ]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   255
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   256
    b := (buffer bitShift:(8 - bits)) bitAnd:16rFF.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   257
    bits := bits - 8.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   258
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   259
    ^ b.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   260
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   261
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   262
!BaseNCoder methodsFor:'queries'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   263
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   264
atEnd
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   265
    "answer true, if no more bytes can be read"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   266
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   267
    bits == 0 ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   268
        atEnd ifTrue:[^ true].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   269
        self fillBuffer.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   270
        bits == 0 ifTrue:[^ true].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   271
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   272
    ^ false.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   273
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   274
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   275
binary
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   276
    "switch to binary mode - nothing is done here.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   277
     Defined for compatibility with ExternalStream."
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   278
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   279
    ^ self
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   280
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   281
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   282
isStream
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   283
    "we simulate a stream"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   284
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   285
    ^ true
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   286
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   287
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   288
!BaseNCoder methodsFor:'stream compatibility'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   289
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   290
nextBytesInto:anObject startingAt:offset
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   291
    "copy bytes into anObject starting at offset"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   292
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   293
    |off|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   294
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   295
    off := offset.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   296
    [self atEnd] whileFalse:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   297
        anObject at:off put:self next.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   298
        off := off + 1.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   299
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   300
    ^ off - offset
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   301
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   302
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   303
nextPut:aByte
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   304
    "encode aByte on the output stream"
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   305
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   306
    ^ self nextPutByte:aByte asInteger.
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   307
!
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   308
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   309
nextPutAll:aCollection startingAt:first to:last
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   310
    "append the elements with index from first to last
3761
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   311
     of the argument, aCollection onto the receiver."
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   312
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   313
    aCollection from:first to:last do:[:element |
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   314
        self nextPutByte:element
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   315
    ].
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   316
    ^ aCollection
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   317
!
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   318
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   319
nextPutBytes:aCollectionOfBytes
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   320
    "encode all objects from the argument"
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   321
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   322
    aCollectionOfBytes do:[:o |
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   323
        self nextPutByte:o
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   324
    ]
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   325
!
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   326
2090
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   327
stringUpToEnd
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   328
    "return a collection of the elements up-to the end"
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   329
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   330
    |answerStream|
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   331
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   332
    answerStream := WriteStream on:(String new:128).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   333
    peekByte notNil ifTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   334
        answerStream nextPut:(Character codePoint:peekByte).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   335
        peekByte := nil.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   336
    ].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   337
    [
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   338
        [bits >= 8] whileTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   339
            answerStream nextPut:(Character codePoint:((buffer bitShift:(8 - bits)) bitAnd:16rFF)).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   340
            bits := bits - 8.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   341
        ].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   342
        atEnd ifTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   343
            bits ~~ 0 ifTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   344
                answerStream nextPut:(Character codePoint:(buffer bitAnd:16rFF)).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   345
                bits := 0.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   346
            ]
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   347
        ] ifFalse:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   348
            self fillBuffer.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   349
        ].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   350
    ] doWhile:[bits > 0].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   351
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   352
    ^ answerStream contents
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   353
!
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   354
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   355
upToEnd
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   356
    "return a collection of the elements up-to the end"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   357
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   358
    |answerStream|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   359
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   360
    answerStream := WriteStream on:(ByteArray new:128).
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   361
    peekByte notNil ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   362
        answerStream nextPut:peekByte.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   363
        peekByte := nil.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   364
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   365
    [
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   366
        [bits >= 8] whileTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   367
            answerStream nextPut:((buffer bitShift:(8 - bits)) bitAnd:16rFF).
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   368
            bits := bits - 8.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   369
        ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   370
        atEnd ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   371
            bits ~~ 0 ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   372
                answerStream nextPut:(buffer bitAnd:16rFF).
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   373
                bits := 0.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   374
            ]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   375
        ] ifFalse:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   376
            self fillBuffer.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   377
        ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   378
    ] doWhile:[bits > 0].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   379
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   380
    ^ answerStream contents
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   381
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   382
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   383
!BaseNCoder methodsFor:'subclass responsibility'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   384
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   385
fillBuffer
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   386
    "fill buffer with next n characters each representing m bits"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   387
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   388
    ^ self subclassResponsibility.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   389
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   390
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   391
flush
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   392
    "flush the remaining bits of buffer. 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   393
     The number of bits in buffer is not a multiple of m, so we pad
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   394
     the buffer and signal that padding has been done via $= characters."
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   395
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   396
    ^ self subclassResponsibility.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   397
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   398
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   399
nextPutByte:aByte
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   400
    "encode aByte on the output stream"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   401
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   402
    ^ self subclassResponsibility.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   403
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   404
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   405
!BaseNCoder class methodsFor:'documentation'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   406
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   407
version
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   408
    ^ '$Header$'
3761
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   409
!
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   410
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   411
version_CVS
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   412
    ^ '$Header$'
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   413
! !
3548
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
   414