Stream.st
changeset 12129 cc5fccca0451
parent 12093 3c1bef066cdb
child 12132 160c15005022
equal deleted inserted replaced
12128:2f4168c4e5de 12129:cc5fccca0451
  1029     ^ self nextLongMSB:true
  1029     ^ self nextLongMSB:true
  1030 
  1030 
  1031     "Created: 10.1.1996 / 19:49:28 / cg"
  1031     "Created: 10.1.1996 / 19:49:28 / cg"
  1032 !
  1032 !
  1033 
  1033 
  1034 nextNumber:n 
  1034 nextNumber:numBytes 
  1035     "Return the next n bytes as a positive Integer; bytes are taken msb-first."
  1035     "Return the next n bytes as a positive Integer; bytes are taken msb-first."
  1036 
  1036 
  1037     |s 
  1037     ^ self nextUnsigned:numBytes MSB:true
  1038      val "{ Class: SmallInteger }"
       
  1039      rep "{ Class: SmallInteger }"|
       
  1040 
       
  1041     "claus: this method is central in binaryStorage -
       
  1042      therefore it has been tuned a bit (and needs even more tuning)"
       
  1043 
       
  1044     n == 1 ifTrue:[
       
  1045         ^ self nextByte
       
  1046     ].
       
  1047     n == 2 ifTrue:[
       
  1048         val := self nextByte.
       
  1049         val := (val bitShift:8) + self nextByte.
       
  1050         ^ val
       
  1051     ].
       
  1052     n == 3 ifTrue:[
       
  1053         val := self nextByte.
       
  1054         val := (val bitShift:8) + self nextByte.
       
  1055         val := (val bitShift:8) + self nextByte.
       
  1056         ^ val
       
  1057     ].
       
  1058     n == 4 ifTrue:[
       
  1059         val := self nextByte.
       
  1060         val <= 16r3F ifTrue:[
       
  1061             val := (val bitShift:8) + self nextByte.
       
  1062             val := (val bitShift:8) + self nextByte.
       
  1063             val := (val bitShift:8) + self nextByte.
       
  1064             ^ val
       
  1065         ].
       
  1066         "sorry, but need a largeInteger"
       
  1067         val := (val bitShift:8) + self nextByte.
       
  1068         s := (val bitShift:8) + self nextByte.
       
  1069         s := (s * 256) + self nextByte.
       
  1070         ^ s
       
  1071     ].
       
  1072 
       
  1073     "
       
  1074      arbitrary long
       
  1075     "
       
  1076     s := 0.
       
  1077     rep := n.
       
  1078     rep timesRepeat:[ 
       
  1079         s := s * 256 + self nextByte
       
  1080     ].
       
  1081     ^ s truncated
       
  1082 !
  1038 !
  1083 
  1039 
  1084 nextShortMSB:msbFlag
  1040 nextShortMSB:msbFlag
  1085     "return a signed short (2 bytes) from the stream.
  1041     "return a signed short (2 bytes) from the stream.
  1086      The receiver must support reading of binary bytes.
  1042      The receiver must support reading of binary bytes.
  1139     ].
  1095     ].
  1140     ^ uval
  1096     ^ uval
  1141 
  1097 
  1142     "
  1098     "
  1143      #[16rFF 16r80 16r7F 16r01] readStream nextSignedByte
  1099      #[16rFF 16r80 16r7F 16r01] readStream nextSignedByte
       
  1100     "
       
  1101 !
       
  1102 
       
  1103 nextUnsigned:numBytes MSB:msbFlag
       
  1104     "return a numBytes-sized unsigned (numBytes bytes) from the stream as an Integer.
       
  1105      The receiver must support reading of binary bytes.
       
  1106 
       
  1107      The msbFlag argument controls if the integer is to be read with
       
  1108      most-significant-byte-first (true) or least-first (false).
       
  1109      This interface is provided to allow talking to external programs,
       
  1110      where its known that the byte order is some definite one.
       
  1111      If you dont care (i.e. talk to other smalltalks) or you can control the
       
  1112      order, please use the corresponding xxxNet methods, which use a standard
       
  1113      network byte order."
       
  1114 
       
  1115     |val shift|
       
  1116 
       
  1117     "claus: this method is central in binaryStorage -
       
  1118      therefore it has been tuned a bit (and needs even more tuning)"
       
  1119 
       
  1120     numBytes == 1 ifTrue:[
       
  1121         ^ self nextByte
       
  1122     ].
       
  1123     numBytes == 2 ifTrue:[
       
  1124         ^ self nextUnsignedShortMSB:msbFlag
       
  1125     ].
       
  1126     numBytes == 3 ifTrue:[
       
  1127         val := self nextUnsignedShortMSB:msbFlag.
       
  1128         msbFlag ifTrue:[
       
  1129             ^ (val bitShift:8) + self nextByte
       
  1130         ].
       
  1131         ^ val + (self nextByte bitShift:16)
       
  1132     ].
       
  1133     numBytes == 4 ifTrue:[
       
  1134         ^ self nextUnsignedLongMSB:msbFlag
       
  1135     ].
       
  1136 
       
  1137     val := 0.
       
  1138     msbFlag ifTrue:[
       
  1139         numBytes timesRepeat:[
       
  1140             val := (val bitShift:8) + self nextByte
       
  1141         ].
       
  1142     ] ifFalse:[
       
  1143         shift := 0.
       
  1144         numBytes timesRepeat:[
       
  1145             val := val + (self nextByte bitShift:shift).
       
  1146             shift := shift + 8.
       
  1147         ].
       
  1148     ].
       
  1149     ^ val
       
  1150 
       
  1151     "
       
  1152      |s|
       
  1153 
       
  1154      s := #[ 16r01 16r02 16r03 16r04 16r05 ] readStream.
       
  1155      (s nextUnsigned:3 MSB:true) hexPrintString.           
       
  1156 
       
  1157      s := #[ 16r01 16r02 16r03 16r04 16r05 ] readStream.
       
  1158      (s nextUnsigned:3 MSB:false) hexPrintString.      
  1144     "
  1159     "
  1145 !
  1160 !
  1146 
  1161 
  1147 nextUnsignedHyperMSB:msbFlag
  1162 nextUnsignedHyperMSB:msbFlag
  1148     "return an unsigned hyper (8 bytes) from the stream.
  1163     "return an unsigned hyper (8 bytes) from the stream.
  2992 ! !
  3007 ! !
  2993 
  3008 
  2994 !Stream class methodsFor:'documentation'!
  3009 !Stream class methodsFor:'documentation'!
  2995 
  3010 
  2996 version
  3011 version
  2997     ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.172 2009-10-02 06:51:28 cg Exp $'
  3012     ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.173 2009-10-05 09:09:31 cg Exp $'
  2998 !
  3013 !
  2999 
  3014 
  3000 version_CVS
  3015 version_CVS
  3001     ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.172 2009-10-02 06:51:28 cg Exp $'
  3016     ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.173 2009-10-05 09:09:31 cg Exp $'
  3002 ! !
  3017 ! !
  3003 
  3018 
  3004 Stream initialize!
  3019 Stream initialize!