BaseNCoder.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 5459 70e5b29dd7d3
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4741
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
     1
"{ Encoding: utf8 }"
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
     2
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     3
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     4
 COPYRIGHT (c) 2002 by eXept Software AG
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     5
              All Rights Reserved
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     6
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     7
 This software is furnished under a license and may be used
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    12
 hereby transferred.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    13
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    14
"{ Package: 'stx:libbasic2' }"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    15
3548
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    16
"{ NameSpace: Smalltalk }"
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    17
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    18
ObjectCoder subclass:#BaseNCoder
4741
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    19
	instanceVariableNames:'buffer bits charCount peekByte atEnd lineLimit mapping
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    20
		reverseMapping'
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    21
	classVariableNames:''
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    22
	poolDictionaries:''
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    23
	category:'System-Storage'
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    24
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    25
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    26
!BaseNCoder class methodsFor:'documentation'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    27
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    28
copyright
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    29
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    30
 COPYRIGHT (c) 2002 by eXept Software AG
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    31
              All Rights Reserved
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    32
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    33
 This software is furnished under a license and may be used
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    34
 only in accordance with the terms of that license and with the
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    35
 inclusion of the above copyright notice.   This software may not
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    36
 be provided or otherwise made available to, or used by, any
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    37
 other person.  No title to or ownership of the software is
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    38
 hereby transferred.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    39
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    40
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    41
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    42
documentation
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    43
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    44
    Abstract superclass of Base64Coder and Base32Coder
4278
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    45
    Their main entry point API is 
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    46
        <BaseNCoder> encode:aStringOrBytes
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    47
    and
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    48
        <BaseNCoder> decode:aString
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    49
4278
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    50
    If the decoder should return a string, use
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    51
        <BaseNCoder> decodeAsString:aString.
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
    [examples:]
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    54
        Base64Coder encode:'helloWorld'
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    55
        
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    56
        Base64Coder decode:'aGVsbG9Xb3JsZA=='
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    57
        
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    58
        Base64Coder decodeAsString:'aGVsbG9Xb3JsZA=='
939053637e6b #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3764
diff changeset
    59
        
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    60
    [author:]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    61
        Stefan Vogel (stefan@zwerg)
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    62
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    63
    [instance variables:]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    64
        buffer          SmallInteger   buffered data
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    65
        bits            SmallInteger   Number of valid bits in buffer
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    66
        charCount       SmallInteger   Number of characters since last cr
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    67
        atEnd           Boolean        true if end of Base64 string reached
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    68
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    69
    [class variables:]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    70
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    71
    [see also:]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    72
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    73
"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    74
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    75
3548
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    76
!BaseNCoder class methodsFor:'initialization'!
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
initializeMappings
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
    79
    self subclassResponsibility
4741
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    80
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    81
    "
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    82
     self withAllSubclassesDo:#initialize
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    83
    "
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    84
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    85
    "Modified (comment): / 30-09-2018 / 15:39:16 / Claus Gittinger"
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    86
!
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    87
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    88
mapping
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    89
    self subclassResponsibility
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    90
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    91
    "Created: / 30-09-2018 / 15:30:08 / Claus Gittinger"
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    92
!
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    93
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    94
reverseMapping
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    95
    self subclassResponsibility
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    96
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    97
    "Created: / 30-09-2018 / 15:30:11 / Claus Gittinger"
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    98
!
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
    99
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   100
reverseMappingFor:mapping
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   101
    "initialize class variables"
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   102
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   103
    |revMapping|
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   104
    
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   105
    revMapping := ByteArray new:128 withAll:255.
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   106
    mapping keysAndValuesDo:[:idx :char|
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   107
        revMapping at:char codePoint put:idx-1.
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   108
    ].
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   109
    ^ revMapping
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   110
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   111
    "Created: / 30-09-2018 / 15:34:37 / Claus Gittinger"
3548
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
   112
! !
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
   113
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   114
!BaseNCoder class methodsFor:'instance creation'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   115
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   116
new
3548
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
   117
   self initializeMappings.
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   118
   ^ self basicNew initialize
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   119
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   120
4893
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   121
!BaseNCoder class methodsFor:'constants'!
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   122
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   123
lineLimit
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   124
    ^ 76. "/ RFC 2045 says: max 76 characters in one line
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   125
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   126
    "Created: / 21-03-2019 / 21:54:41 / Claus Gittinger"
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   127
! !
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   128
2090
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   129
!BaseNCoder class methodsFor:'decoding'!
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   130
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   131
decodeAsString:encodedString
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   132
    "decode a base-n encoded string.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   133
     We already expect a string instead of a ByteArray"
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   134
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   135
    ^ (self on:encodedString readStream) stringUpToEnd
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   136
! !
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   137
4310
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   138
!BaseNCoder class methodsFor:'queries'!
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   139
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   140
isAbstract
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   141
    "Return if this class is an abstract class.
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   142
     True is returned here for myself only; false for subclasses.
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   143
     Abstract subclasses must redefine this again."
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   144
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   145
    ^ self == BaseNCoder.
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   146
! !
5f509afc6907 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4305
diff changeset
   147
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   148
!BaseNCoder methodsFor:'accessing'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   149
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   150
lineLimit:something
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   151
    "set the line length of the encoded output.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   152
     Default is a line length of 76 characters.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   153
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   154
     If nil, no line breaks will be done."
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   155
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   156
    lineLimit := something.
5459
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   157
!
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   158
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   159
mapping:aString
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   160
    "the default mapping is
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   161
        0 -> A
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   162
        1 -> B
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   163
        ...
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   164
        63 -> =
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   165
     (see initializeMapping on the class side)
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   166
     This accessor allows for different mappings to be used."
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   167
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   168
    mapping := aString.
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   169
    reverseMapping := self class reverseMappingFor:mapping.
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   170
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   171
    "
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   172
     |encoded decoded|
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   173
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   174
     encoded := Base64Coder encode:'hello world'.    'aGVsbG8gd29ybGQ='.
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   175
     decoded := Base64Coder decode:encoded.    
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   176
     self assert:(decoded asString = 'hello world').
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   177
    "
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   178
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   179
    "
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   180
     |encoded decoded|
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   181
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   182
     encoded := Base64Coder new 
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   183
                    mapping:'A0B1C2D3E4F5G6H7I8J9K+L/M=NaObPcQdReSfTgUhViWjXkYlZmnopqrstuvwxyz';
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   184
                    encodingOf:'hello world'.    'ND+WaDvQbpwZaDIz'.
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   185
     decoded := Base64Coder new
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   186
                    mapping:'A0B1C2D3E4F5G6H7I8J9K+L/M=NaObPcQdReSfTgUhViWjXkYlZmnopqrstuvwxyz'; 
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   187
                    decodingOf:encoded.    
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   188
     self assert:(decoded asString = 'hello world').
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   189
    "
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   190
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   191
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   192
!BaseNCoder methodsFor:'decoding'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   193
5459
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   194
decodingOf:aString
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   195
    stream := aString readStream.
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   196
    ^ self stringUpToEnd 
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   197
!
70e5b29dd7d3 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 5168
diff changeset
   198
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   199
next
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   200
    "answer the next decoded byte"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   201
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   202
    |b|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   203
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   204
    peekByte notNil ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   205
        b := peekByte.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   206
        peekByte := nil.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   207
        ^ b
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   208
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   209
    ^ self basicNext.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   210
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   211
2638
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   212
next:count
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   213
    "return the next count bytes of the stream as ByteArray"
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   214
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   215
    |answerStream 
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   216
     cnt  "{ Class: SmallInteger }" |
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   217
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   218
    cnt := count.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   219
    answerStream := WriteStream on:(ByteArray new:cnt).
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   220
    answerStream signalAtEnd:true.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   221
    1 to:cnt do:[:index |
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   222
        |next|
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   223
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   224
        next := self next.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   225
        next isNil ifTrue:[
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   226
            "if next did not raise EndOfStreamError, we have to do it"
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   227
            EndOfStreamError raiseRequestFrom:self.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   228
            "if you proceed, you get what we have already collected"
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   229
            ^ answerStream contents
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   230
        ].
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   231
        answerStream nextPut:next.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   232
    ].
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   233
    ^ answerStream contents
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   234
!
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   235
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   236
peek
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   237
    "answer the next decoded byte. Do not consume this byte"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   238
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   239
    peekByte isNil ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   240
        peekByte := self basicNext.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   241
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   242
    ^ peekByte
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   243
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   244
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   245
!BaseNCoder methodsFor:'encoding'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   246
4893
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   247
encodingOf:anObject with:aParameter
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   248
    lineLimit := aParameter.
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   249
    ^ super encodingOf:anObject with:aParameter
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   250
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   251
    "Created: / 21-03-2019 / 22:01:20 / Claus Gittinger"
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   252
!
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   253
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   254
visitByteArray:aByteArray with:aParameter 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   255
    ^ self
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   256
        nextPutBytes:aByteArray;
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   257
        flush.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   258
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   259
    "
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   260
      Base64Coder encodingOf:#[1 2 3 4 5 6 255]
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   261
    "
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   262
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   263
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   264
visitObject:anObject with:aParameter
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   265
    "not defined. Use nextPut or nextPutAll:.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   266
     Could encode the printString here"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   267
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   268
    ^ self shouldNotImplement
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   269
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   270
2160
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   271
visitStream:aStream with:aParameter
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   272
    aStream copyToEndInto:self.
2160
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   273
    self flush.
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   274
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   275
    "
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   276
      Base64Coder encodingOf:#[1 2 3 4 5 6 255]
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   277
      Base64Coder encodingOf:#[1 2 3 4 5 6 255] readStream
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   278
    "
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   279
!
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   280
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   281
visitString:aString with:aParameter 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   282
    ^ self
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   283
        nextPutAll:aString;
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   284
        flush.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   285
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   286
    "
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   287
      |encoded decoded decoder|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   288
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   289
      encoded := Base64Coder encode:'hello world'.  
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   290
      decoded := #[] writeStream.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   291
      decoder := Base64Coder on:encoded readStream.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   292
      [decoder atEnd] whileFalse:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   293
          decoded nextPut:(decoder next).
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   294
      ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   295
      decoded := decoded contents.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   296
      decoded asString.    
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   297
    "
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   298
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   299
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   300
!BaseNCoder methodsFor:'initialization'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   301
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   302
emptyWriteStream
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   303
    "answer an empty stream. We encode as string"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   304
    
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   305
    ^ WriteStream on:(String new:64)
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   306
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   307
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   308
initialize
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   309
4305
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   310
    <modifier: #super> "must be called if redefined"
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   311
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   312
    buffer := bits := charCount := 0.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   313
    lineLimit := 76.   "RFC 2045 says: max 76 characters in one line"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   314
    atEnd := false.
4305
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   315
4741
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   316
    mapping := self class mapping.
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   317
    reverseMapping := self class reverseMapping.
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   318
4305
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   319
    "Modified: / 08-02-2017 / 00:33:07 / cg"
4741
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   320
    "Modified: / 30-09-2018 / 15:29:41 / Claus Gittinger"
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   321
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   322
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   323
!BaseNCoder methodsFor:'misc'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   324
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   325
reset
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   326
    "reset to initial state"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   327
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   328
    super reset.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   329
    buffer := bits := charCount := 0.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   330
    atEnd := false.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   331
    peekByte := nil.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   332
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   333
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   334
!BaseNCoder methodsFor:'private'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   335
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   336
basicNext
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   337
    "answer the next decoded byte. 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   338
     No peekByte handling is done here."
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   339
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   340
    |b|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   341
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   342
    bits == 0 ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   343
        self fillBuffer.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   344
        bits == 0 ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   345
            ^ stream pastEndRead.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   346
        ]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   347
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   348
    b := (buffer bitShift:(8 - bits)) bitAnd:16rFF.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   349
    bits := bits - 8.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   350
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   351
    ^ b.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   352
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   353
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   354
!BaseNCoder methodsFor:'queries'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   355
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   356
atEnd
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   357
    "answer true, if no more bytes can be read"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   358
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   359
    bits == 0 ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   360
        atEnd ifTrue:[^ true].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   361
        self fillBuffer.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   362
        bits == 0 ifTrue:[^ true].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   363
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   364
    ^ false.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   365
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   366
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   367
binary
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   368
    "switch to binary mode - nothing is done here.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   369
     Defined for compatibility with ExternalStream."
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   370
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   371
    ^ self
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   372
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   373
4839
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   374
binary:beBinaryBool
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   375
    "ExternalStream protocol compatibility"
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   376
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   377
    ^ false         "/ no-op, I am not in binary mode
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   378
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   379
    "Created: / 13-03-2019 / 19:15:17 / Stefan Vogel"
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   380
!
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   381
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   382
isStream
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   383
    "we simulate a stream"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   384
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   385
    ^ true
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   386
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   387
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   388
!BaseNCoder methodsFor:'stream compatibility'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   389
4842
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   390
bufferSizeForBulkCopy
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   391
    ^ 1024
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   392
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   393
    "Created: / 14-03-2019 / 12:58:47 / Stefan Vogel"
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   394
!
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   395
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   396
nextByte
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   397
    "ExternalStream compatibility"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   398
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   399
    ^ self next
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   400
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   401
    "Created: / 27-03-2019 / 23:39:24 / stefan"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   402
!
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   403
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   404
nextBytes:count into:anObject
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   405
    "read the next count bytes into an object and return the number of
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   406
     bytes read. On EOF, 0 is returned.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   407
     If the receiver is some socket/pipe-like stream, an exception
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   408
     is raised if the connection is broken.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   409
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   410
     The object must have non-pointer indexed instvars (i.e. it must be
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   411
     a ByteArray, String, Float- or DoubleArray).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   412
     If anObject is a string or byteArray and reused, this provides the
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   413
     fastest possible physical I/O (since no new objects are allocated).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   414
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   415
     Use with care - non object oriented i/o.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   416
     Warning: in general, you cannot use this method to pass data from other
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   417
     architectures since it does not care for byte order or float representation."
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   418
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   419
    ^ self nextBytes:count into:anObject startingAt:1
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   420
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   421
    "Created: / 27-03-2019 / 23:37:31 / stefan"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   422
!
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   423
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   424
nextBytes:numBytes into:anObject startingAt:initialIndex
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   425
    "copy bytes into anObject starting at offset"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   426
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   427
    |n "{Class: SmallInteger }"|
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   428
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   429
    n := 0.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   430
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   431
    [n ~= numBytes and:[self atEnd not]] whileTrue:[
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   432
        anObject byteAt:initialIndex+n put:self next.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   433
        n := n + 1.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   434
    ].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   435
    ^ n
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   436
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   437
    "Created: / 27-03-2019 / 23:36:59 / stefan"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   438
!
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   439
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   440
nextBytesInto:anObject startingAt:initialIndex
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   441
    "copy bytes into anObject starting at offset"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   442
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   443
    |n "{Class: SmallInteger }"|
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   444
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   445
    n := 0.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   446
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   447
    [self atEnd] whileFalse:[
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   448
        anObject byteAt:initialIndex+n put:self next.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   449
        n := n + 1.
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   450
    ].
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   451
    ^ n
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   452
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   453
    "Modified: / 27-03-2019 / 23:48:16 / stefan"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   454
!
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   455
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   456
nextInt32MSB:msbFlag
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   457
    "return a signed long (4 bytes) from the stream.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   458
     The receiver must support reading of binary bytes.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   459
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   460
     The msbFlag argument controls if the integer is to be read with
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   461
     most-significant-byte-first (true) or least-first (false).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   462
     This interface is provided to allow talking to external programs,
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   463
     where it's known that the byte order is some definite one.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   464
     If you don't care (i.e. talk to other smalltalks) or you can control the
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   465
     order, please use the corresponding xxxNet methods, which use a standard
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   466
     network byte order.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   467
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   468
     1-to-1 copy from Stream"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   469
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   470
    |b1 b2 b3 b4 uval "{ Class: SmallInteger }" val|
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   471
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   472
    b1 := self nextByte.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   473
    b2 := self nextByte.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   474
    b3 := self nextByte.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   475
    b4 := self nextByte.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   476
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   477
    msbFlag ifTrue:[
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   478
        "most significant first"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   479
        uval := (b1 bitShift:8) bitOr:b2.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   480
        uval := (uval bitShift:8) bitOr:b3.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   481
        val := (uval bitShift:8) bitOr:b4.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   482
    ] ifFalse:[
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   483
        "least significant first"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   484
        uval := (b4 bitShift:8) bitOr:b3.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   485
        uval := (uval bitShift:8) bitOr:b2.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   486
        val := (uval bitShift:8) bitOr:b1.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   487
    ].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   488
    "change from unsigned 0..FFFFFFFF to signed -80000000..7FFFFFFF"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   489
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   490
    val >= 16r80000000 ifTrue:[
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   491
      ^ val - 16r100000000
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   492
    ].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   493
    ^ val
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   494
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   495
    "
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   496
     |bytes s|
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   497
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   498
     bytes := #[16rFF 16rFF 16rFF 16rFF].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   499
     s := bytes readStream.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   500
     Transcript showCR:(s nextInt32MSB:true).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   501
     s reset.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   502
     Transcript showCR:(s nextInt32MSB:false).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   503
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   504
     bytes := #[16r12 16r34 16r56 16r78].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   505
     s := bytes readStream.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   506
     Transcript showCR:(s nextInt32MSB:true).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   507
     s reset.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   508
     Transcript showCR:(s nextInt32MSB:false).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   509
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   510
     bytes := #[16r89 16rab 16rcd 16ref].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   511
     s := bytes readStream.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   512
     Transcript showCR:(s nextInt32MSB:true).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   513
     s reset.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   514
     Transcript showCR:(s nextInt32MSB:false).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   515
    "
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   516
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   517
    "Created: / 27-03-2019 / 23:38:32 / stefan"
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   518
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   519
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   520
nextPut:aByte
5168
fee2c272b167 #OTHER by exept
Claus Gittinger <cg@exept.de>
parents: 5152
diff changeset
   521
    "encode aByte on the output stream.
fee2c272b167 #OTHER by exept
Claus Gittinger <cg@exept.de>
parents: 5152
diff changeset
   522
     Answer aByte"
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   523
5152
8834593e27e1 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 4928
diff changeset
   524
    self nextPutByte:aByte asInteger.
5168
fee2c272b167 #OTHER by exept
Claus Gittinger <cg@exept.de>
parents: 5152
diff changeset
   525
    ^ aByte
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   526
!
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   527
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   528
nextPutAll:aCollection startingAt:first to:last
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   529
    "append the elements with index from first to last
3761
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   530
     of the argument, aCollection onto the receiver."
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   531
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   532
    aCollection from:first to:last do:[:element |
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   533
        self nextPutByte:element
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   534
    ].
5152
8834593e27e1 #QUALITY by exept
Claus Gittinger <cg@exept.de>
parents: 4928
diff changeset
   535
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   536
!
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   537
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   538
nextPutBytes:aCollectionOfBytes
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   539
    "encode all objects from the argument"
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   540
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   541
    aCollectionOfBytes do:[:o |
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   542
        self nextPutByte:o
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   543
    ]
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   544
!
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   545
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   546
skip:numberToSkip
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   547
    "skip numberToSkip objects, return the receiver.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   548
     1-to-1 copy from Stream."
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   549
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   550
    "don't know how to unread ..."
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   551
    numberToSkip < 0 ifTrue:[
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   552
        PositionError raiseRequest.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   553
        ^ self
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   554
    ].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   555
    numberToSkip timesRepeat:[self next]
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   556
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   557
    "Created: / 27-03-2019 / 23:43:26 / stefan"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   558
!
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   559
2090
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   560
stringUpToEnd
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   561
    "return a collection of the elements up-to the end"
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   562
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   563
    |answerStream|
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   564
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   565
    answerStream := WriteStream on:(String new:128).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   566
    peekByte notNil ifTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   567
        answerStream nextPut:(Character codePoint:peekByte).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   568
        peekByte := nil.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   569
    ].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   570
    [
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   571
        [bits >= 8] whileTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   572
            answerStream nextPut:(Character codePoint:((buffer bitShift:(8 - bits)) bitAnd:16rFF)).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   573
            bits := bits - 8.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   574
        ].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   575
        atEnd ifTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   576
            bits ~~ 0 ifTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   577
                answerStream nextPut:(Character codePoint:(buffer bitAnd:16rFF)).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   578
                bits := 0.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   579
            ]
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   580
        ] ifFalse:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   581
            self fillBuffer.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   582
        ].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   583
    ] doWhile:[bits > 0].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   584
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   585
    ^ answerStream contents
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   586
!
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   587
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   588
upToEnd
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   589
    "return a collection of the elements up-to the end"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   590
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   591
    |answerStream|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   592
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   593
    answerStream := WriteStream on:(ByteArray new:128).
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   594
    peekByte notNil ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   595
        answerStream nextPut:peekByte.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   596
        peekByte := nil.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   597
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   598
    [
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   599
        [bits >= 8] whileTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   600
            answerStream nextPut:((buffer bitShift:(8 - bits)) bitAnd:16rFF).
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   601
            bits := bits - 8.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   602
        ].
4752
5392065bde5a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4741
diff changeset
   603
        atEnd ifFalse:[
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   604
            self fillBuffer.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   605
        ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   606
    ] doWhile:[bits > 0].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   607
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   608
    ^ answerStream contents
4752
5392065bde5a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4741
diff changeset
   609
5392065bde5a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4741
diff changeset
   610
    "Modified: / 30-09-2018 / 16:37:03 / Claus Gittinger"
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   611
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   612
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   613
!BaseNCoder methodsFor:'subclass responsibility'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   614
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   615
fillBuffer
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   616
    "fill buffer with next n characters each representing m bits"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   617
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   618
    ^ self subclassResponsibility.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   619
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   620
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   621
flush
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   622
    "flush the remaining bits of buffer. 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   623
     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
   624
     the buffer and signal that padding has been done via $= characters."
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   625
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   626
    ^ self subclassResponsibility.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   627
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   628
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   629
nextPutByte:aByte
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   630
    "encode aByte on the output stream"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   631
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   632
    ^ self subclassResponsibility.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   633
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   634
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   635
!BaseNCoder class methodsFor:'documentation'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   636
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   637
version
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   638
    ^ '$Header$'
3761
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   639
!
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   640
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   641
version_CVS
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   642
    ^ '$Header$'
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   643
! !
3548
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
   644