Stream.st
branchjv
changeset 17732 a1892eeca6c0
parent 17728 bbc5fa73dfab
child 17734 406b1590afe8
--- a/Stream.st	Fri Aug 28 12:38:51 2009 +0100
+++ b/Stream.st	Sat Oct 24 16:48:19 2009 +0100
@@ -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.
@@ -1390,13 +1405,28 @@
     "Modified: / 23-06-2006 / 12:19:47 / fm"
 !
 
+nextPutBytes:anObject
+    "write bytes from an object; the number of bytes is defined by
+     the objects size.
+     Return the number of bytes written or nil on error.
+     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.
+     Warning: in general, you cannot use this method to pass non-byte data to other 
+     architectures since it does not care for byte order or float representation."
+
+    ^ self nextPutBytes:(anObject size) from:anObject startingAt:1
+
+    "Created: 22.4.1997 / 10:44:18 / cg"
+!
+
 nextPutBytes:count from:anObject
     "write count bytes from an object.
      Return the number of bytes written or nil on error.
      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.
-     Warning: in general, you cannot use this method to pass data to other 
+     Warning: in general, you cannot use this method to pass non-byte data to other 
      architectures since it does not care for byte order or float representation."
 
     ^ self nextPutBytes:count from:anObject startingAt:1
@@ -1433,13 +1463,12 @@
 !
 
 nextPutBytesFrom:anObject
-    "write bytes from an object; the number of bytes is defined by
-     the objects size.
+    "write bytes from an object; the number of bytes is defined by the objects size.
      Return the number of bytes written or nil on error.
      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.
-     Warning: in general, you cannot use this method to pass data to other 
+     Warning: in general, you cannot use this method to pass non-byte data to other 
      architectures since it does not care for byte order or float representation."
 
     ^ self nextPutBytes:(anObject size) from:anObject startingAt:1
@@ -1802,6 +1831,13 @@
     "Modified: 15.5.1996 / 17:53:51 / cg"
 !
 
+isOpen
+    "for compatibility with externalStream:
+     return true, if this stream is open."
+
+    ^ true
+!
+
 isPositionable
     "return true, if the stream supports positioning (some do not).
      Since this is an abstract class, false is returned here - just to make certain."
@@ -2588,7 +2624,7 @@
 
     n := count.
     n timesRepeat:[self nextPut:anObject].
-    ^ anObject
+    "/ ^ anObject -- return self
 
     "
      |s|
@@ -2615,9 +2651,8 @@
      This is only allowed, if the receiver supports writing."
 
     aCollection do:[:element |
-	self nextPut:element
+        self nextPut:element
     ].
-    ^ aCollection
 
     "
      |s|
@@ -2751,14 +2786,35 @@
 !
 
 show:something with:arg
+    "append a printed representation of the argument to the stream, expanding
+     the placeHolder %1 with the printString of arg.
+     This makes streams somewhat compatible to TextCollectors and
+     allows you to say: 
+        Smalltalk at:#Transcript put:Stdout
+     or to use #show:/#showCR: with internal or external streams."
+
     (something bindWith:arg) printOn:self
 !
 
 show:something with:arg1 with:arg2
+    "append a printed representation of the argument to the stream, expanding
+     the placeHolders %1 and %2 with the printStrings of arg1 and arg2.
+     This makes streams somewhat compatible to TextCollectors and
+     allows you to say: 
+        Smalltalk at:#Transcript put:Stdout
+     or to use #show:/#showCR: with internal or external streams."
+
     (something bindWith:arg1 with:arg2) printOn:self
 !
 
 show:something with:arg1 with:arg2 with:arg3
+    "append a printed representation of the argument to the stream, expanding
+     the placeHolders %1,%2 and %3 with the printStrings of arg1, arg2 and arg3.
+     This makes streams somewhat compatible to TextCollectors and
+     allows you to say: 
+        Smalltalk at:#Transcript put:Stdout
+     or to use #show:/#showCR: with internal or external streams."
+
     (something bindWith:arg1 with:arg2 with:arg3) printOn:self
 !
 
@@ -2952,7 +3008,12 @@
 !Stream class methodsFor:'documentation'!
 
 version
-    ^ '$Id: Stream.st 10467 2009-08-19 16:14:36Z vranyj1 $'
+    ^ '$Id: Stream.st 10473 2009-10-24 15:48:19Z vranyj1 $'
+!
+
+version_CVS
+    ^ '§Header: /cvs/stx/stx/libbasic/Stream.st,v 1.175 2009/10/05 09:21:03 cg Exp §'
 ! !
 
 Stream initialize!
+