BaseNCoder.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Jun 2019 11:07:59 +0200
changeset 5047 f6ccfe8f2ddb
parent 4928 30c9cf7ae5fd
child 5152 8834593e27e1
permissions -rw-r--r--
#DOCUMENTATION by cg class: DelayedValue comment/format in: #displayOn: #displayString class: DelayedValue class comment/format in: #documentation
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.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   157
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   158
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   159
!BaseNCoder methodsFor:'decoding'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   160
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   161
next
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   162
    "answer the next decoded byte"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   163
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   164
    |b|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   165
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   166
    peekByte notNil ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   167
        b := peekByte.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   168
        peekByte := nil.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   169
        ^ b
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   170
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   171
    ^ self basicNext.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   172
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   173
2638
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   174
next:count
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   175
    "return the next count bytes of the stream as ByteArray"
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   176
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   177
    |answerStream 
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   178
     cnt  "{ Class: SmallInteger }" |
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   179
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   180
    cnt := count.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   181
    answerStream := WriteStream on:(ByteArray new:cnt).
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   182
    answerStream signalAtEnd:true.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   183
    1 to:cnt do:[:index |
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   184
        |next|
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   185
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   186
        next := self next.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   187
        next isNil ifTrue:[
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   188
            "if next did not raise EndOfStreamError, we have to do it"
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   189
            EndOfStreamError raiseRequestFrom:self.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   190
            "if you proceed, you get what we have already collected"
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   191
            ^ answerStream contents
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   192
        ].
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   193
        answerStream nextPut:next.
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   194
    ].
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   195
    ^ answerStream contents
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   196
!
cde8f8841e60 added: #next:
Stefan Vogel <sv@exept.de>
parents: 2160
diff changeset
   197
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   198
peek
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   199
    "answer the next decoded byte. Do not consume this byte"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   200
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   201
    peekByte isNil ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   202
        peekByte := self basicNext.
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
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   205
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   206
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   207
!BaseNCoder methodsFor:'encoding'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   208
4893
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   209
encodingOf:anObject with:aParameter
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   210
    lineLimit := aParameter.
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   211
    ^ super encodingOf:anObject with:aParameter
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   212
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   213
    "Created: / 21-03-2019 / 22:01:20 / Claus Gittinger"
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   214
!
8c2146846b19 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4842
diff changeset
   215
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   216
visitByteArray:aByteArray with:aParameter 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   217
    ^ self
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   218
        nextPutBytes:aByteArray;
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   219
        flush.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   220
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   221
    "
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   222
      Base64Coder encodingOf:#[1 2 3 4 5 6 255]
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   223
    "
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   224
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   225
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   226
visitObject:anObject with:aParameter
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   227
    "not defined. Use nextPut or nextPutAll:.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   228
     Could encode the printString here"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   229
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   230
    ^ self shouldNotImplement
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   231
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   232
2160
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   233
visitStream:aStream with:aParameter
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   234
    aStream copyToEndInto:self.
2160
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   235
    self flush.
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   236
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   237
    "
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   238
      Base64Coder encodingOf:#[1 2 3 4 5 6 255]
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   239
      Base64Coder encodingOf:#[1 2 3 4 5 6 255] readStream
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   240
    "
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   241
!
554dbbd8cfce new: #visitStream:with:
Stefan Vogel <sv@exept.de>
parents: 2090
diff changeset
   242
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   243
visitString:aString with:aParameter 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   244
    ^ self
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   245
        nextPutAll:aString;
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   246
        flush.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   247
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   248
    "
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   249
      |encoded decoded decoder|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   250
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   251
      encoded := Base64Coder encode:'hello world'.  
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   252
      decoded := #[] writeStream.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   253
      decoder := Base64Coder on:encoded readStream.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   254
      [decoder atEnd] whileFalse:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   255
          decoded nextPut:(decoder next).
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   256
      ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   257
      decoded := decoded contents.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   258
      decoded asString.    
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   259
    "
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:'initialization'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   263
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   264
emptyWriteStream
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   265
    "answer an empty stream. We encode as string"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   266
    
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   267
    ^ WriteStream on:(String new:64)
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   268
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   269
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   270
initialize
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   271
4305
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   272
    <modifier: #super> "must be called if redefined"
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   273
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   274
    buffer := bits := charCount := 0.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   275
    lineLimit := 76.   "RFC 2045 says: max 76 characters in one line"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   276
    atEnd := false.
4305
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   277
4741
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   278
    mapping := self class mapping.
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   279
    reverseMapping := self class reverseMapping.
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   280
4305
2e04f1ed00e8 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4278
diff changeset
   281
    "Modified: / 08-02-2017 / 00:33:07 / cg"
4741
bfb8e4d34f3e #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4310
diff changeset
   282
    "Modified: / 30-09-2018 / 15:29:41 / Claus Gittinger"
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   283
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   284
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   285
!BaseNCoder methodsFor:'misc'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   286
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   287
reset
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   288
    "reset to initial state"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   289
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   290
    super reset.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   291
    buffer := bits := charCount := 0.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   292
    atEnd := false.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   293
    peekByte := nil.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   294
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   295
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   296
!BaseNCoder methodsFor:'private'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   297
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   298
basicNext
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   299
    "answer the next decoded byte. 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   300
     No peekByte handling is done here."
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   301
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   302
    |b|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   303
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   304
    bits == 0 ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   305
        self fillBuffer.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   306
        bits == 0 ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   307
            ^ stream pastEndRead.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   308
        ]
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   309
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   310
    b := (buffer bitShift:(8 - bits)) bitAnd:16rFF.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   311
    bits := bits - 8.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   312
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   313
    ^ b.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   314
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   315
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   316
!BaseNCoder methodsFor:'queries'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   317
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   318
atEnd
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   319
    "answer true, if no more bytes can be read"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   320
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   321
    bits == 0 ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   322
        atEnd ifTrue:[^ true].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   323
        self fillBuffer.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   324
        bits == 0 ifTrue:[^ true].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   325
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   326
    ^ false.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   327
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   328
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   329
binary
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   330
    "switch to binary mode - nothing is done here.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   331
     Defined for compatibility with ExternalStream."
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   332
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   333
    ^ self
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   334
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   335
4839
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   336
binary:beBinaryBool
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   337
    "ExternalStream protocol compatibility"
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   338
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   339
    ^ false         "/ no-op, I am not in binary mode
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   340
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   341
    "Created: / 13-03-2019 / 19:15:17 / Stefan Vogel"
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   342
!
dfb7f7db7b54 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4752
diff changeset
   343
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   344
isStream
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   345
    "we simulate a stream"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   346
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   347
    ^ true
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   348
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   349
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   350
!BaseNCoder methodsFor:'stream compatibility'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   351
4842
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   352
bufferSizeForBulkCopy
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   353
    ^ 1024
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   354
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   355
    "Created: / 14-03-2019 / 12:58:47 / Stefan Vogel"
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   356
!
6926d1490469 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4839
diff changeset
   357
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   358
nextByte
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   359
    "ExternalStream compatibility"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   360
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   361
    ^ self next
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   362
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   363
    "Created: / 27-03-2019 / 23:39:24 / stefan"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   364
!
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   365
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   366
nextBytes:count into:anObject
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   367
    "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
   368
     bytes read. On EOF, 0 is returned.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   369
     If the receiver is some socket/pipe-like stream, an exception
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   370
     is raised if the connection is broken.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   371
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   372
     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
   373
     a ByteArray, String, Float- or DoubleArray).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   374
     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
   375
     fastest possible physical I/O (since no new objects are allocated).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   376
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   377
     Use with care - non object oriented i/o.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   378
     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
   379
     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
   380
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   381
    ^ self nextBytes:count into:anObject startingAt:1
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   382
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   383
    "Created: / 27-03-2019 / 23:37:31 / stefan"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   384
!
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   385
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   386
nextBytes:numBytes into:anObject startingAt:initialIndex
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   387
    "copy bytes into anObject starting at offset"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   388
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   389
    |n "{Class: SmallInteger }"|
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   390
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   391
    n := 0.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   392
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   393
    [n ~= numBytes and:[self atEnd not]] whileTrue:[
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   394
        anObject byteAt:initialIndex+n put:self next.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   395
        n := n + 1.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   396
    ].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   397
    ^ n
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
    "Created: / 27-03-2019 / 23:36:59 / stefan"
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
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   402
nextBytesInto:anObject startingAt:initialIndex
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   403
    "copy bytes into anObject starting at offset"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   404
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   405
    |n "{Class: SmallInteger }"|
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   406
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   407
    n := 0.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   408
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   409
    [self atEnd] whileFalse:[
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   410
        anObject byteAt:initialIndex+n put:self next.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   411
        n := n + 1.
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   412
    ].
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   413
    ^ n
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
    "Modified: / 27-03-2019 / 23:48:16 / stefan"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   416
!
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   417
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   418
nextInt32MSB:msbFlag
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   419
    "return a signed long (4 bytes) from the stream.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   420
     The receiver must support reading of binary bytes.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   421
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   422
     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
   423
     most-significant-byte-first (true) or least-first (false).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   424
     This interface is provided to allow talking to external programs,
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   425
     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
   426
     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
   427
     order, please use the corresponding xxxNet methods, which use a standard
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   428
     network byte order.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   429
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   430
     1-to-1 copy from Stream"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   431
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   432
    |b1 b2 b3 b4 uval "{ Class: SmallInteger }" val|
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   433
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   434
    b1 := self nextByte.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   435
    b2 := self nextByte.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   436
    b3 := self nextByte.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   437
    b4 := self nextByte.
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
    msbFlag ifTrue:[
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   440
        "most significant first"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   441
        uval := (b1 bitShift:8) bitOr:b2.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   442
        uval := (uval bitShift:8) bitOr:b3.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   443
        val := (uval bitShift:8) bitOr:b4.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   444
    ] ifFalse:[
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   445
        "least significant first"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   446
        uval := (b4 bitShift:8) bitOr:b3.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   447
        uval := (uval bitShift:8) bitOr:b2.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   448
        val := (uval bitShift:8) bitOr:b1.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   449
    ].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   450
    "change from unsigned 0..FFFFFFFF to signed -80000000..7FFFFFFF"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   451
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   452
    val >= 16r80000000 ifTrue:[
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   453
      ^ val - 16r100000000
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
    ^ val
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   456
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   457
    "
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   458
     |bytes s|
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
     bytes := #[16rFF 16rFF 16rFF 16rFF].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   461
     s := bytes readStream.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   462
     Transcript showCR:(s nextInt32MSB:true).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   463
     s reset.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   464
     Transcript showCR:(s nextInt32MSB:false).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   465
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   466
     bytes := #[16r12 16r34 16r56 16r78].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   467
     s := bytes readStream.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   468
     Transcript showCR:(s nextInt32MSB:true).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   469
     s reset.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   470
     Transcript showCR:(s nextInt32MSB:false).
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
     bytes := #[16r89 16rab 16rcd 16ref].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   473
     s := bytes readStream.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   474
     Transcript showCR:(s nextInt32MSB:true).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   475
     s reset.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   476
     Transcript showCR:(s nextInt32MSB:false).
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   477
    "
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   478
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   479
    "Created: / 27-03-2019 / 23:38:32 / stefan"
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   480
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   481
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   482
nextPut:aByte
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   483
    "encode aByte on the output stream"
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   484
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   485
    ^ self nextPutByte:aByte asInteger.
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   486
!
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   487
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   488
nextPutAll:aCollection startingAt:first to:last
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   489
    "append the elements with index from first to last
3761
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   490
     of the argument, aCollection onto the receiver."
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   491
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   492
    aCollection from:first to:last do:[:element |
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   493
        self nextPutByte:element
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   494
    ].
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   495
    ^ aCollection
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   496
!
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   497
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   498
nextPutBytes:aCollectionOfBytes
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   499
    "encode all objects from the argument"
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   500
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   501
    aCollectionOfBytes do:[:o |
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   502
        self nextPutByte:o
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   503
    ]
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   504
!
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   505
4928
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   506
skip:numberToSkip
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   507
    "skip numberToSkip objects, return the receiver.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   508
     1-to-1 copy from Stream."
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
    "don't know how to unread ..."
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   511
    numberToSkip < 0 ifTrue:[
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   512
        PositionError raiseRequest.
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   513
        ^ self
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   514
    ].
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   515
    numberToSkip timesRepeat:[self next]
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:43:26 / stefan"
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   518
!
30c9cf7ae5fd #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4893
diff changeset
   519
2090
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   520
stringUpToEnd
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   521
    "return a collection of the elements up-to the end"
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   522
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   523
    |answerStream|
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   524
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   525
    answerStream := WriteStream on:(String new:128).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   526
    peekByte notNil ifTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   527
        answerStream nextPut:(Character codePoint:peekByte).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   528
        peekByte := nil.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   529
    ].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   530
    [
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   531
        [bits >= 8] whileTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   532
            answerStream nextPut:(Character codePoint:((buffer bitShift:(8 - bits)) bitAnd:16rFF)).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   533
            bits := bits - 8.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   534
        ].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   535
        atEnd ifTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   536
            bits ~~ 0 ifTrue:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   537
                answerStream nextPut:(Character codePoint:(buffer bitAnd:16rFF)).
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   538
                bits := 0.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   539
            ]
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   540
        ] ifFalse:[
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   541
            self fillBuffer.
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   542
        ].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   543
    ] doWhile:[bits > 0].
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   544
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   545
    ^ answerStream contents
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   546
!
4b87ce97bb6a New: #decodeAsString:
Stefan Vogel <sv@exept.de>
parents: 1965
diff changeset
   547
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   548
upToEnd
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   549
    "return a collection of the elements up-to the end"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   550
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   551
    |answerStream|
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   552
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   553
    answerStream := WriteStream on:(ByteArray new:128).
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   554
    peekByte notNil ifTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   555
        answerStream nextPut:peekByte.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   556
        peekByte := nil.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   557
    ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   558
    [
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   559
        [bits >= 8] whileTrue:[
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   560
            answerStream nextPut:((buffer bitShift:(8 - bits)) bitAnd:16rFF).
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   561
            bits := bits - 8.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   562
        ].
4752
5392065bde5a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4741
diff changeset
   563
        atEnd ifFalse:[
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   564
            self fillBuffer.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   565
        ].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   566
    ] doWhile:[bits > 0].
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   567
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   568
    ^ answerStream contents
4752
5392065bde5a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4741
diff changeset
   569
5392065bde5a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4741
diff changeset
   570
    "Modified: / 30-09-2018 / 16:37:03 / Claus Gittinger"
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   571
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   572
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   573
!BaseNCoder methodsFor:'subclass responsibility'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   574
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   575
fillBuffer
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   576
    "fill buffer with next n characters each representing m bits"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   577
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   578
    ^ self subclassResponsibility.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   579
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   580
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   581
flush
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   582
    "flush the remaining bits of buffer. 
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   583
     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
   584
     the buffer and signal that padding has been done via $= characters."
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   585
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   586
    ^ self subclassResponsibility.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   587
!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   588
3764
1352adead5b5 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3761
diff changeset
   589
nextPutByte:aByte
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   590
    "encode aByte on the output stream"
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   591
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   592
    ^ self subclassResponsibility.
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   593
! !
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   594
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   595
!BaseNCoder class methodsFor:'documentation'!
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   596
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   597
version
3760
bc6b4bb97572 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3548
diff changeset
   598
    ^ '$Header$'
3761
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   599
!
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   600
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   601
version_CVS
43af86dd75ca #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3760
diff changeset
   602
    ^ '$Header$'
1965
a0fd03c22adb initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   603
! !
3548
5424405a96ed initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 2638
diff changeset
   604