#FEATURE by stefan
class: BaseNCoder
added:
#nextByte
#nextBytes:into:
#nextBytes:into:startingAt:
#nextInt32MSB:
#skip:
changed: #nextBytesInto:startingAt:
--- a/BaseNCoder.st Thu Mar 28 11:57:05 2019 +0100
+++ b/BaseNCoder.st Thu Mar 28 18:08:06 2019 +0100
@@ -355,17 +355,128 @@
"Created: / 14-03-2019 / 12:58:47 / Stefan Vogel"
!
-nextBytesInto:anObject startingAt:offset
+nextByte
+ "ExternalStream compatibility"
+
+ ^ self next
+
+ "Created: / 27-03-2019 / 23:39:24 / stefan"
+!
+
+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."
+
+ ^ self nextBytes:count into:anObject startingAt:1
+
+ "Created: / 27-03-2019 / 23:37:31 / stefan"
+!
+
+nextBytes:numBytes into:anObject startingAt:initialIndex
+ "copy bytes into anObject starting at offset"
+
+ |n "{Class: SmallInteger }"|
+
+ n := 0.
+
+ [n ~= numBytes and:[self atEnd not]] whileTrue:[
+ anObject byteAt:initialIndex+n put:self next.
+ n := n + 1.
+ ].
+ ^ n
+
+ "Created: / 27-03-2019 / 23:36:59 / stefan"
+!
+
+nextBytesInto:anObject startingAt:initialIndex
"copy bytes into anObject starting at offset"
- |off|
+ |n "{Class: SmallInteger }"|
- off := offset.
+ n := 0.
+
[self atEnd] whileFalse:[
- anObject at:off put:self next.
- off := off + 1.
+ anObject byteAt:initialIndex+n put:self next.
+ n := n + 1.
].
- ^ off - offset
+ ^ n
+
+ "Modified: / 27-03-2019 / 23:48:16 / stefan"
+!
+
+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"
+
+ |b1 b2 b3 b4 uval "{ Class: SmallInteger }" val|
+
+ b1 := self nextByte.
+ b2 := self nextByte.
+ b3 := self nextByte.
+ b4 := self nextByte.
+
+ msbFlag ifTrue:[
+ "most significant first"
+ uval := (b1 bitShift:8) bitOr:b2.
+ uval := (uval bitShift:8) bitOr:b3.
+ val := (uval bitShift:8) bitOr:b4.
+ ] ifFalse:[
+ "least significant first"
+ uval := (b4 bitShift:8) bitOr:b3.
+ uval := (uval bitShift:8) bitOr:b2.
+ val := (uval bitShift:8) bitOr:b1.
+ ].
+ "change from unsigned 0..FFFFFFFF to signed -80000000..7FFFFFFF"
+
+ val >= 16r80000000 ifTrue:[
+ ^ val - 16r100000000
+ ].
+ ^ val
+
+ "
+ |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).
+ "
+
+ "Created: / 27-03-2019 / 23:38:32 / stefan"
!
nextPut:aByte
@@ -392,6 +503,20 @@
]
!
+skip:numberToSkip
+ "skip numberToSkip objects, return the receiver.
+ 1-to-1 copy from Stream."
+
+ "don't know how to unread ..."
+ numberToSkip < 0 ifTrue:[
+ PositionError raiseRequest.
+ ^ self
+ ].
+ numberToSkip timesRepeat:[self next]
+
+ "Created: / 27-03-2019 / 23:43:26 / stefan"
+!
+
stringUpToEnd
"return a collection of the elements up-to the end"