Stream.st
changeset 275 a76029ddaa98
parent 216 a8abff749575
child 285 ad6dfa61182e
equal deleted inserted replaced
274:0c35c797ac6c 275:a76029ddaa98
    19 
    19 
    20 Stream comment:'
    20 Stream comment:'
    21 COPYRIGHT (c) 1989 by Claus Gittinger
    21 COPYRIGHT (c) 1989 by Claus Gittinger
    22 	      All Rights Reserved
    22 	      All Rights Reserved
    23 
    23 
    24 $Header: /cvs/stx/stx/libbasic/Stream.st,v 1.11 1995-02-02 12:22:45 claus Exp $
    24 $Header: /cvs/stx/stx/libbasic/Stream.st,v 1.12 1995-02-21 01:06:10 claus Exp $
    25 '!
    25 '!
    26 
    26 
    27 !Stream class methodsFor:'documentation'!
    27 !Stream class methodsFor:'documentation'!
    28 
    28 
    29 copyright
    29 copyright
    40 "
    40 "
    41 !
    41 !
    42 
    42 
    43 version
    43 version
    44 "
    44 "
    45 $Header: /cvs/stx/stx/libbasic/Stream.st,v 1.11 1995-02-02 12:22:45 claus Exp $
    45 $Header: /cvs/stx/stx/libbasic/Stream.st,v 1.12 1995-02-21 01:06:10 claus Exp $
    46 "
    46 "
    47 !
    47 !
    48 
    48 
    49 documentation
    49 documentation
    50 "
    50 "
    88     1 to:count do:[:index |
    88     1 to:count do:[:index |
    89 	coll at:index put:(self next)
    89 	coll at:index put:(self next)
    90     ].
    90     ].
    91     ^ coll
    91     ^ coll
    92 
    92 
    93     "(ReadStream on:#(1 2 3 4 5)) next:3"
    93     "
    94     "(ReadStream on:'hello') next:3"
    94      (ReadStream on:#(1 2 3 4 5)) next:3
       
    95      (ReadStream on:'hello') next:3
       
    96     "
    95 !
    97 !
    96 
    98 
    97 nextMatchFor:anObject
    99 nextMatchFor:anObject
    98     "read an element from the receiver, return true if it was equal to
   100     "read an element from the receiver, return true if it was equal to
    99      the argument, anObject"
   101      the argument, anObject"
   176     [self atEnd] whileFalse:[
   178     [self atEnd] whileFalse:[
   177 	answerStream nextPut:(self next)
   179 	answerStream nextPut:(self next)
   178     ].
   180     ].
   179     ^ answerStream contents
   181     ^ answerStream contents
   180 
   182 
   181     "(ReadStream on:'1234567890') upToEnd"
   183     "
   182     "((ReadStream on:'123456') next; next) upToEnd"
   184      (ReadStream on:'1234567890') upToEnd
       
   185      ('123456' readStream) next; next; upToEnd
       
   186     "
   183 !
   187 !
   184 
   188 
   185 through:anObject
   189 through:anObject
   186     "read a collection of all objects up-to and including anObject. 
   190     "read a collection of all objects up-to and including anObject. 
   187      The next read operation will return the element after anObject."
   191      The next read operation will return the element after anObject."
   394      first. Returns the receiver on ok, nil on error.
   398      first. Returns the receiver on ok, nil on error.
   395      The receiver must support writing of binary bytes."
   399      The receiver must support writing of binary bytes."
   396 
   400 
   397     msbFlag ifTrue:[
   401     msbFlag ifTrue:[
   398 	"most significant first"
   402 	"most significant first"
   399 	self nextPut:(aNumber // 256).
   403 	self nextPut:(aNumber bitShift:-8).
   400 	^ self nextPut:(aNumber \\ 256).
   404 	^ self nextPut:(aNumber bitAnd:16rFF).
   401     ].
   405     ].
   402     "least significant first"
   406     "least significant first"
   403     self nextPut:(aNumber \\ 256).
   407     self nextPut:(aNumber bitAnd:16rFF).
   404     self nextPut:(aNumber // 256).
   408     self nextPut:(aNumber bitShift:-8).
   405 !
   409 !
   406 
   410 
   407 nextPutLong:aNumber MSB:msbFlag
   411 nextPutLong:aNumber MSB:msbFlag
   408     "Write the argument, aNumber as a long (four bytes). If msbFlag is
   412     "Write the argument, aNumber as a long (four bytes). If msbFlag is
   409      true, data is written most-significant byte first; otherwise least
   413      true, data is written most-significant byte first; otherwise least
   424     "Append to the receiver the argument, v, which is a positive Integer,
   428     "Append to the receiver the argument, v, which is a positive Integer,
   425      as the next n bytes. Bytes are written msb first. 
   429      as the next n bytes. Bytes are written msb first. 
   426      Possibly pad with leading zeros.
   430      Possibly pad with leading zeros.
   427      The receiver must support writing of binary bytes."
   431      The receiver must support writing of binary bytes."
   428 
   432 
   429     | vlen i |
   433     |vlen i|
   430 
   434 
   431     "claus: this method is central in binaryStorage -
   435     "claus: this method is central in binaryStorage -
   432      therefore it has been tuned a bit (and needs even more tuning)"
   436      therefore it has been tuned a bit (and needs even more tuning)"
   433 
   437 
   434     v class == SmallInteger ifTrue:[  "- this is a hint to stc"
   438     v class == SmallInteger ifTrue:[  "- this is a hint to stc"
   462 		^ self
   466 		^ self
   463 	    ].
   467 	    ].
   464 	].
   468 	].
   465     ].
   469     ].
   466 
   470 
       
   471     "
       
   472      arbitrary long
       
   473     "
   467     n < (vlen := v digitLength) ifTrue: [
   474     n < (vlen := v digitLength) ifTrue: [
       
   475 	"
       
   476 	 the number is too big to be repesented in n bytes
       
   477 	"
   468 	self error: 'number too big'
   478 	self error: 'number too big'
   469     ].
   479     ].
   470 
   480 
   471     "pad with leading zeros"
   481     "pad with leading zeros"
   472     i := n.
   482     i := n.
   498     ] ifFalse:[
   508     ] ifFalse:[
   499 	"least significant first"
   509 	"least significant first"
   500 	uval := self next.
   510 	uval := self next.
   501 	uval := uval bitOr:(self next bitShift:8).
   511 	uval := uval bitOr:(self next bitShift:8).
   502     ].
   512     ].
   503     "change from unsigned 0..FFFF to signed"
   513     "change from unsigned 0..FFFF to signed -8000..7FFF"
   504     uval >= 16r8000 ifTrue:[
   514     uval >= 16r8000 ifTrue:[
   505 	^ uval - 16r10000 
   515 	^ uval - 16r10000 
   506     ].
   516     ].
   507     ^ uval
   517     ^ uval
   508 !
   518 !
   535 	uval := self next.
   545 	uval := self next.
   536 	uval := uval bitOr:(self next bitShift:8).
   546 	uval := uval bitOr:(self next bitShift:8).
   537 	uval := uval bitOr:(self next bitShift:16).
   547 	uval := uval bitOr:(self next bitShift:16).
   538 	uval := uval bitOr:(self next bitShift:24).
   548 	uval := uval bitOr:(self next bitShift:24).
   539     ].
   549     ].
   540     "change from unsigned 0..FFFFFFFF to signed"
   550     "change from unsigned 0..FFFFFFFF to signed -80000000..7FFFFFFF"
   541 
   551 
   542 "sorry- for now, stc cannot compile LargeInteger constants 
   552 "sorry- for now, stc cannot compile LargeInteger constants 
   543     uval >= 16r80000000 ifTrue:[
   553     uval >= 16r80000000 ifTrue:[
   544 	^ uval - 16r100000000 
   554 	^ uval - 16r100000000 
   545     ].
   555     ].
   571 !
   581 !
   572 
   582 
   573 nextNumber:n 
   583 nextNumber:n 
   574     "Return the next n bytes as a positive Integer; bytes are taken msb-first."
   584     "Return the next n bytes as a positive Integer; bytes are taken msb-first."
   575 
   585 
   576     | s val "{ Class: SmallInteger }" |
   586     |s val "{ Class: SmallInteger }"|
   577 
   587 
   578     "claus: this method is central in binaryStorage -
   588     "claus: this method is central in binaryStorage -
   579      therefore it has been tuned a bit (and needs even more tuning)"
   589      therefore it has been tuned a bit (and needs even more tuning)"
   580 
   590 
   581     n == 1 ifTrue:[
   591     n == 1 ifTrue:[
   599 	    val := (val bitShift:8) + self next.
   609 	    val := (val bitShift:8) + self next.
   600 	    val := (val bitShift:8) + self next.
   610 	    val := (val bitShift:8) + self next.
   601 	    ^ val
   611 	    ^ val
   602 	].
   612 	].
   603 	"sorry, but need a largeInteger"
   613 	"sorry, but need a largeInteger"
   604 	s := val.
   614 	s := (val bitShift:8) + self next.
   605 	s := (s bitShift:8) + self next.
       
   606 	s := (s bitShift:8) + self next.
   615 	s := (s bitShift:8) + self next.
   607 	"the multiplication below is a temporary workaround for the
   616 	"the multiplication below is a temporary workaround for the
   608 	 inability of smallintegers to handle shifts out of the 32bit range ..."
   617 	 inability of smallintegers to handle shifts out of the 32bit range ..."
   609 	s := (s * 256) + self next.
   618 	s := (s * 256) + self next.
   610 	^ s
   619 	^ s
   611     ].
   620     ].
   612 
   621 
       
   622     "
       
   623      arbitrary long
       
   624     "
   613     s := 0.
   625     s := 0.
   614     n timesRepeat:[ 
   626     n timesRepeat:[ 
   615 	s := s * 256 + self next
   627 	s := s * 256 + self next
   616     ].
   628     ].
   617     ^ s truncated
   629     ^ s truncated