|
Class: BaseNCoder
Object
|
+--Visitor
|
+--AspectVisitor
|
+--ObjectCoder
|
+--BaseNCoder
|
+--Base32Coder
|
+--Base64Coder
- Package:
- stx:libbasic2
- Category:
- System-Storage
- Version:
- rev:
1.27
date: 2023/11/06 10:25:52
- user: stefan
- file: BaseNCoder.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
Abstract superclass of Base64Coder and Base32Coder
Their main entry point API is
<BaseNCoder> encode:aStringOrBytes
and
<BaseNCoder> decode:aString
If the decoder should return a string, use
<BaseNCoder> decodeAsString:aString.
API wise, I behave most like a stream.
[examples:]
Base64Coder encode:'helloWorld'
Base64Coder decode:'aGVsbG9Xb3JsZA=='
Base64Coder decodeAsString:'aGVsbG9Xb3JsZA=='
[instance variables:]
buffer SmallInteger buffered data
bits SmallInteger Number of valid bits in buffer
charCount SmallInteger Number of characters since last cr
atEnd Boolean true if end of Base64 string reached
[class variables:]
copyrightCOPYRIGHT (c) 2002 by eXept Software AG
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
constants
-
lineLimit
-
RFC 2045 says: max 76 characters in one line
decoding
-
decodeAsString: encodedString
-
decode a base-n encoded string.
We already expect a string instead of a ByteArray
initialization
-
initializeMappings
-
self withAllSubclassesDo:#initialize
** This method must be redefined in concrete classes (subclassResponsibility) **
-
mapping
-
** This method must be redefined in concrete classes (subclassResponsibility) **
-
reverseMapping
-
** This method must be redefined in concrete classes (subclassResponsibility) **
-
reverseMappingFor: mapping
-
initialize class variables
instance creation
-
new
-
(comment from inherited method)
return an instance of myself without indexed variables
queries
-
isAbstract
-
Return if this class is an abstract class.
True is returned here for myself only; false for subclasses.
Abstract subclasses must redefine this again.
accessing
-
lineLimit: something
-
set the line length of the encoded output.
Default is a line length of 76 characters.
If nil, no line breaks will be done.
-
mapping: aString
-
the default mapping is
0 -> A
1 -> B
...
63 -> =
(see initializeMapping on the class side)
This accessor allows for different mappings to be used.
Usage example(s):
|encoded decoded|
encoded := Base64Coder new
mapping:'A0B1C2D3E4F5G6H7I8J9K+L/M=NaObPcQdReSfTgUhViWjXkYlZmnopqrstuvwxyz';
encodingOf:'hello world'. 'ND+WaDvQbpwZaDIz'.
decoded := Base64Coder new
mapping:'A0B1C2D3E4F5G6H7I8J9K+L/M=NaObPcQdReSfTgUhViWjXkYlZmnopqrstuvwxyz';
decodingOf:encoded.
self assert:(decoded asString = 'hello world').
|
decoding
-
decodingOf: aStringOrStream
-
Modified (format): / 06-03-2022 / 20:44:11 / cg
-
next
-
answer the next decoded byte
-
next: count
-
return the next count bytes of the stream as ByteArray
-
peek
-
answer the next decoded byte. Do not consume this byte
encoding
-
encodingOf: anObject with: aParameter
-
(comment from inherited method)
answer the encoded argument anObject
-
visitByteArray: aByteArray with: aParameter
-
Base64Coder encodingOf:#[1 2 3 4 5 6 255]
-
visitObject: anObject with: aParameter
-
not defined. Use nextPut or nextPutAll:.
Could encode the printString here
-
visitStream: aStream with: aParameter
-
Base64Coder encodingOf:#[1 2 3 4 5 6 255]
Base64Coder encodingOf:#[1 2 3 4 5 6 255] readStream
-
visitString: aString with: aParameter
-
|encoded decoded decoder|
encoded := Base64Coder encode:'hello world'.
decoded := #[] writeStream.
decoder := Base64Coder on:encoded readStream.
[decoder atEnd] whileFalse:[
decoded nextPut:(decoder next).
].
decoded := decoded contents.
decoded asString.
initialization
-
emptyWriteStream
-
answer an empty stream. We encode as string
-
initialize
-
must be called if redefined
misc
-
reset
-
reset to initial state
private
-
basicNext
-
answer the next decoded byte.
No peekByte handling is done here.
queries
-
atEnd
-
answer true, if no more bytes can be read
-
binary
-
switch to binary mode - nothing is done here.
Defined for compatibility with ExternalStream.
-
binary: beBinaryBool
-
ExternalStream protocol compatibility
-
isStream
-
we simulate a stream
stream compatibility
-
bufferSizeForBulkCopy
-
-
copy: numBytes into: aWriteStream
-
copy bytes into aWriteStream.
Return the number of elements which have been transferred.
-
next: numBytes into: anObject startingAt: initialIndex
-
copy bytes into anObject starting at offset
-
nextByte
-
ExternalStream compatibility
-
nextBytes: count
-
read the next count bytes and return them as a bytearray.
On early EOF, a shorter ByteArray is returned.
If the receiver is some socket/pipe-like stream, an exception
is raised if the connection is broken.
-
nextBytes: count into: anObject
-
read the next count bytes into an object and return the number of
bytes read. On EOF, 0 is returned.
If the receiver is some socket/pipe-like stream, an exception
is raised if the connection is broken.
The object must have non-pointer indexed instvars (i.e. it must be
a ByteArray, String, Float- or DoubleArray).
If anObject is a string or byteArray and reused, this provides the
fastest possible physical I/O (since no new objects are allocated).
Use with care - non object oriented i/o.
Warning: in general, you cannot use this method to pass data from other
architectures since it does not care for byte order or float representation.
-
nextBytes: numBytes into: anObject startingAt: initialIndex
-
copy bytes into anObject starting at offset
-
nextBytesInto: anObject startingAt: initialIndex
-
copy bytes into anObject starting at offset
-
nextInt32MSB: msbFlag
-
return a signed long (4 bytes) from the stream.
The receiver must support reading of binary bytes.
The msbFlag argument controls if the integer is to be read with
most-significant-byte-first (true) or least-first (false).
This interface is provided to allow talking to external programs,
where it's known that the byte order is some definite one.
If you don't care (i.e. talk to other smalltalks) or you can control the
order, please use the corresponding xxxNet methods, which use a standard
network byte order.
1-to-1 copy from Stream
Usage example(s):
|bytes s|
bytes := #[16rFF 16rFF 16rFF 16rFF].
s := bytes readStream.
Transcript showCR:(s nextInt32MSB:true).
s reset.
Transcript showCR:(s nextInt32MSB:false).
bytes := #[16r12 16r34 16r56 16r78].
s := bytes readStream.
Transcript showCR:(s nextInt32MSB:true).
s reset.
Transcript showCR:(s nextInt32MSB:false).
bytes := #[16r89 16rab 16rcd 16ref].
s := bytes readStream.
Transcript showCR:(s nextInt32MSB:true).
s reset.
Transcript showCR:(s nextInt32MSB:false).
|
-
nextPut: aByte
-
encode aByte on the output stream.
Answer aByte
-
nextPutAll: aCollection startingAt: first to: last
-
append the elements with index from first to last
of the argument, aCollection onto the receiver.
-
nextPutBytes: aCollectionOfBytes
-
encode all objects from the argument
-
nextPutBytes: count from: anObject startingAt: start
-
write count bytes from an object starting at index start.
Return the number of bytes written.
The object must have non-pointer indexed instvars
(i.e. be a ByteArray, String, Float- or DoubleArray).
Use with care - non object oriented i/o.
This is provided for compatibility with externalStream;
to support binary storage
-
skip: numberToSkip
-
skip numberToSkip objects, return the receiver.
1-to-1 copy from Stream.
-
stringUpToEnd
-
return a collection of the elements up-to the end
-
upToEnd
-
return a collection of the elements up-to the end
subclass responsibility
-
fillBuffer
-
fill buffer with next n characters each representing m bits
** This method must be redefined in concrete classes (subclassResponsibility) **
-
flush
-
flush the remaining bits of buffer.
The number of bits in buffer is not a multiple of m, so we pad
the buffer and signal that padding has been done via $= characters.
** This method must be redefined in concrete classes (subclassResponsibility) **
-
nextPutByte: aByte
-
encode aByte on the output stream
** This method must be redefined in concrete classes (subclassResponsibility) **
|