added: #nextUnsigned:MSB:
authorClaus Gittinger <cg@exept.de>
Mon, 05 Oct 2009 11:09:31 +0200
changeset 12129 cc5fccca0451
parent 12128 2f4168c4e5de
child 12130 5af9bdd94754
added: #nextUnsigned:MSB: changed: #nextNumber:
Stream.st
--- a/Stream.st	Mon Oct 05 09:53:51 2009 +0200
+++ b/Stream.st	Mon Oct 05 11:09:31 2009 +0200
@@ -1031,54 +1031,10 @@
     "Created: 10.1.1996 / 19:49:28 / cg"
 !
 
-nextNumber:n 
+nextNumber:numBytes 
     "Return the next n bytes as a positive Integer; bytes are taken msb-first."
 
-    |s 
-     val "{ Class: SmallInteger }"
-     rep "{ Class: SmallInteger }"|
-
-    "claus: this method is central in binaryStorage -
-     therefore it has been tuned a bit (and needs even more tuning)"
-
-    n == 1 ifTrue:[
-        ^ self nextByte
-    ].
-    n == 2 ifTrue:[
-        val := self nextByte.
-        val := (val bitShift:8) + self nextByte.
-        ^ val
-    ].
-    n == 3 ifTrue:[
-        val := self nextByte.
-        val := (val bitShift:8) + self nextByte.
-        val := (val bitShift:8) + self nextByte.
-        ^ val
-    ].
-    n == 4 ifTrue:[
-        val := self nextByte.
-        val <= 16r3F ifTrue:[
-            val := (val bitShift:8) + self nextByte.
-            val := (val bitShift:8) + self nextByte.
-            val := (val bitShift:8) + self nextByte.
-            ^ val
-        ].
-        "sorry, but need a largeInteger"
-        val := (val bitShift:8) + self nextByte.
-        s := (val bitShift:8) + self nextByte.
-        s := (s * 256) + self nextByte.
-        ^ s
-    ].
-
-    "
-     arbitrary long
-    "
-    s := 0.
-    rep := n.
-    rep timesRepeat:[ 
-        s := s * 256 + self nextByte
-    ].
-    ^ s truncated
+    ^ self nextUnsigned:numBytes MSB:true
 !
 
 nextShortMSB:msbFlag
@@ -1144,6 +1100,65 @@
     "
 !
 
+nextUnsigned:numBytes MSB:msbFlag
+    "return a numBytes-sized unsigned (numBytes bytes) from the stream as an Integer.
+     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."
+
+    |val shift|
+
+    "claus: this method is central in binaryStorage -
+     therefore it has been tuned a bit (and needs even more tuning)"
+
+    numBytes == 1 ifTrue:[
+        ^ self nextByte
+    ].
+    numBytes == 2 ifTrue:[
+        ^ self nextUnsignedShortMSB:msbFlag
+    ].
+    numBytes == 3 ifTrue:[
+        val := self nextUnsignedShortMSB:msbFlag.
+        msbFlag ifTrue:[
+            ^ (val bitShift:8) + self nextByte
+        ].
+        ^ val + (self nextByte bitShift:16)
+    ].
+    numBytes == 4 ifTrue:[
+        ^ self nextUnsignedLongMSB:msbFlag
+    ].
+
+    val := 0.
+    msbFlag ifTrue:[
+        numBytes timesRepeat:[
+            val := (val bitShift:8) + self nextByte
+        ].
+    ] ifFalse:[
+        shift := 0.
+        numBytes timesRepeat:[
+            val := val + (self nextByte bitShift:shift).
+            shift := shift + 8.
+        ].
+    ].
+    ^ val
+
+    "
+     |s|
+
+     s := #[ 16r01 16r02 16r03 16r04 16r05 ] readStream.
+     (s nextUnsigned:3 MSB:true) hexPrintString.           
+
+     s := #[ 16r01 16r02 16r03 16r04 16r05 ] readStream.
+     (s nextUnsigned:3 MSB:false) hexPrintString.      
+    "
+!
+
 nextUnsignedHyperMSB:msbFlag
     "return an unsigned hyper (8 bytes) from the stream.
      The receiver must support reading of binary bytes.
@@ -2994,11 +3009,11 @@
 !Stream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.172 2009-10-02 06:51:28 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.173 2009-10-05 09:09:31 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.172 2009-10-02 06:51:28 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.173 2009-10-05 09:09:31 cg Exp $'
 ! !
 
 Stream initialize!