Stream.st
branchjv
changeset 19412 1e842c25e51e
parent 19410 f9d7cb8bd74c
parent 19396 f2f97c9c3e72
child 19478 1f5aa87f6170
--- a/Stream.st	Tue Mar 22 08:03:27 2016 +0000
+++ b/Stream.st	Wed Mar 23 07:50:28 2016 +0000
@@ -1080,6 +1080,36 @@
     "Created: 10.1.1996 / 19:49:41 / cg"
 !
 
+nextInt24MSB:msbFlag
+    "return a signed 3 byte integer 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."
+
+    |uval "{ Class: SmallInteger }"|
+
+    uval := self nextUnsignedInt24MSB:msbFlag.
+    "change from unsigned 0..FFFFFF to signed -800000..7FFFFF"
+    uval >= 16r800000 ifTrue:[
+        ^ uval - 16r1000000 
+    ].
+    ^ uval
+
+    "
+     ((ReadStream on:#[16r10 16r20 16rFF]) nextInt24MSB:true) hexPrintString
+     ((ReadStream on:#[16rFF 16r20 16r30]) nextInt24MSB:false) hexPrintString
+
+     ((ReadStream on:#[16rFF 16r20 16r30]) nextInt24MSB:true) hexPrintString
+     ((ReadStream on:#[16r10 16r20 16rFF]) nextInt24MSB:false) hexPrintString
+    "
+!
+
 nextInt32MSB:msbFlag
     "return a signed long (4 bytes) from the stream.
      The receiver must support reading of binary bytes.
@@ -1259,15 +1289,14 @@
         ^ self nextUnsignedInt16MSB:msbFlag
     ].
     numBytes == 3 ifTrue:[
-        val := self nextUnsignedInt16MSB:msbFlag.
-        msbFlag ifTrue:[
-            ^ (val bitShift:8) + self nextByte
-        ].
-        ^ val + (self nextByte bitShift:16)
+        ^ self nextUnsignedInt24MSB:msbFlag.
     ].
     numBytes == 4 ifTrue:[
         ^ self nextUnsignedInt32MSB:msbFlag
     ].
+    numBytes == 8 ifTrue:[
+        ^ self nextUnsignedInt64MSB:msbFlag
+    ].
 
     val := 0.
     msbFlag ifTrue:[
@@ -1351,15 +1380,24 @@
      order, please use the corresponding xxxNet methods, which use a standard
      network byte order."
 
-    |b1 b2|
+    |b1 b2 bH bL|
 
     b1 := self nextByte.
     b2 := self nextByte.
 
     msbFlag ifTrue:[
-        ^ (b1 bitShift:8) bitOr:b2
-    ].
-    ^ (b2 bitShift:8) bitOr:b1
+        bH := b1.
+        bL := b2.
+    ] ifFalse:[
+        bL := b1.
+        bH := b2.
+    ].    
+    ^ (bH bitShift:8) bitOr:bL
+
+    "
+     ((ReadStream on:#[16r10 16r20 16r30]) nextUnsignedInt16MSB:true) hexPrintString
+     ((ReadStream on:#[16r10 16r20 16r30]) nextUnsignedInt16MSB:false) hexPrintString
+    "
 
     "Modified: 11.7.1996 / 10:07:20 / cg"
 !
@@ -1374,6 +1412,41 @@
     "Created: 10.1.1996 / 19:50:02 / cg"
 !
 
+nextUnsignedInt24MSB:msbFlag
+    "return an unsigned 3 byte integer 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 its known that the byte order is some definite one.
+     If you dont 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."
+
+    |b1 b2 b3 bL bM bH|
+
+    b1 := self nextByte.
+    b2 := self nextByte.
+    b3 := self nextByte.
+
+    msbFlag ifTrue:[
+        bH := b1.
+        bM := b2.
+        bL := b3.
+    ] ifFalse:[
+        bH := b3.
+        bM := b2.
+        bL := b1.
+    ].    
+    ^ (((bH bitShift:8) bitOr:bM) bitShift:8) bitOr:bL
+
+    "
+     ((ReadStream on:#[16r10 16r20 16r30]) nextUnsignedInt24MSB:true) hexPrintString
+     ((ReadStream on:#[16r10 16r20 16r30]) nextUnsignedInt24MSB:false) hexPrintString
+    "
+!
+
 nextUnsignedInt32MSB:msbFlag
     "return an unsigned long (4 bytes) from the stream.
      The receiver must support reading of binary bytes.
@@ -3351,7 +3424,8 @@
 nextDecimalInteger:numChars
     "read and return the next integer of numChars size from the receiver stream.
      Does NOT skip separators.
-     Leaves the stream positioned after the digits"
+     Leaves the stream positioned after the digits.
+     Raises an error, if the characters cannot be converted to an integer"
 
     |chars|
 
@@ -3384,7 +3458,8 @@
 nextDecimalNumber:numChars
     "read and return the next number of numChars size from the receiver stream.
      Does NOT skip separators.
-     Leaves the stream positioned after the digits"
+     Leaves the stream positioned after the digits.
+     Raises an error, if the characters cannot be converted to a number"
 
     |chars|