ByteArray.st
changeset 19178 0c97c5cfb7df
parent 19134 eaa91cb0ef1b
child 19225 9e8abf62f932
child 19350 6eb57a6369be
equal deleted inserted replaced
19177:873c60e3a4e7 19178:0c97c5cfb7df
   162 %}.
   162 %}.
   163     ^ self basicNew:anInteger
   163     ^ self basicNew:anInteger
   164 ! !
   164 ! !
   165 
   165 
   166 
   166 
       
   167 
   167 !ByteArray class methodsFor:'queries'!
   168 !ByteArray class methodsFor:'queries'!
   168 
   169 
   169 elementByteSize
   170 elementByteSize
   170     "for bit-like containers, return the number of bytes stored per element.
   171     "for bit-like containers, return the number of bytes stored per element.
   171      Here, 1 is returned"
   172      Here, 1 is returned"
   181 
   182 
   182     ^ self == ByteArray
   183     ^ self == ByteArray
   183 
   184 
   184     "Modified: 23.4.1996 / 15:56:25 / cg"
   185     "Modified: 23.4.1996 / 15:56:25 / cg"
   185 ! !
   186 ! !
       
   187 
   186 
   188 
   187 !ByteArray methodsFor:'Compatibility-Squeak'!
   189 !ByteArray methodsFor:'Compatibility-Squeak'!
   188 
   190 
   189 bitXor:aByteArray
   191 bitXor:aByteArray
   190     "return a new byteArray containing the bitWise-xor of the receiver's and the
   192     "return a new byteArray containing the bitWise-xor of the receiver's and the
  1056 !
  1058 !
  1057 
  1059 
  1058 asImmutableByteArray
  1060 asImmutableByteArray
  1059     "return a write-protected copy of myself"
  1061     "return a write-protected copy of myself"
  1060 
  1062 
  1061     ^ self copy changeClassTo:ImmutableByteArray
  1063     |copy|
       
  1064 
       
  1065     copy := self copy.
       
  1066     copy class == ByteArray ifTrue:[
       
  1067         ^ copy changeClassTo:ImmutableByteArray.
       
  1068     ] ifFalse:[
       
  1069         ^ self shouldNotImplement.
       
  1070     ].
  1062 !
  1071 !
  1063 
  1072 
  1064 asInteger
  1073 asInteger
  1065     "convert myself to an integer - the first byte is most significant.
  1074     "convert myself to an integer - the first byte is most significant.
  1066      This is also in Squeak."
  1075      This is also in Squeak."
  1079      This is also in Squeak."
  1088      This is also in Squeak."
  1080 
  1089 
  1081     ^ (LargeInteger digitBytes:self MSB:isMSBFirst) compressed
  1090     ^ (LargeInteger digitBytes:self MSB:isMSBFirst) compressed
  1082 
  1091 
  1083     "
  1092     "
  1084 	(#[ 2 ] asIntegerMSB:true) hexPrintString
  1093         (#[ 2 ] asIntegerMSB:true) hexPrintString
  1085 	(#[ 16r1 16r2 ] asIntegerMSB:true) hexPrintString
  1094         (#[ 16r1 16r2 ] asIntegerMSB:true) hexPrintString
  1086 	(#[ 16r1 16r2 ] asIntegerMSB:false) hexPrintString
  1095         (#[ 16r1 16r2 ] asIntegerMSB:false) hexPrintString
  1087 	(#[4 0 0 0 0 0 0 0] asIntegerMSB:true) hexPrintString
  1096         (#[4 0 0 0 0 0 0 0 0 0 0 0] asIntegerMSB:true) hexPrintString
  1088     "
  1097     "
  1089 !
  1098 !
  1090 
  1099 
  1091 asPackedString
  1100 asPackedString
  1092     "ST-80 compatibility: encode the receiver into an ascii String
  1101     "ST-80 compatibility: encode the receiver into an ascii String
  1159     "
  1168     "
  1160 
  1169 
  1161     "Modified: 12.11.1996 / 15:45:02 / cg"
  1170     "Modified: 12.11.1996 / 15:45:02 / cg"
  1162 !
  1171 !
  1163 
  1172 
       
  1173 asSignedByteArray
       
  1174     "return the receiver as a signed byteArray.
       
  1175      elements > 127 are converted to negative numbers."
       
  1176 
       
  1177     |cls|
       
  1178 
       
  1179     cls := self class.
       
  1180     "could be an instance of a subclass..."
       
  1181     (cls == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[
       
  1182         ^ self copy changeClassTo:SignedByteArray.
       
  1183     ].
       
  1184     ^ super asSignedByteArray
       
  1185 
       
  1186     "
       
  1187      'hello world' asSignedByteArray
       
  1188      #[1 2 3 4 5 6 7] asSignedByteArray
       
  1189      #[1 2 129 4 5 6 7] asSignedByteArray
       
  1190     "
       
  1191 !
       
  1192 
  1164 beImmutable
  1193 beImmutable
  1165     "make myself write-protected"
  1194     "make myself write-protected"
  1166 
  1195 
  1167     self changeClassTo:ImmutableByteArray
  1196     self class == ByteArray ifTrue:[
       
  1197         self changeClassTo:ImmutableByteArray.
       
  1198     ] ifFalse:[
       
  1199         self shouldNotImplement.
       
  1200     ].
       
  1201 !
       
  1202 
       
  1203 beSigned
       
  1204     "make mayself signed.
       
  1205      elements > 127 are converted to negative numbers."
       
  1206 
       
  1207     self class == ByteArray ifTrue:[
       
  1208         self changeClassTo:SignedByteArray.
       
  1209     ] ifFalse:[
       
  1210         self shouldNotImplement.
       
  1211     ].
       
  1212 
       
  1213     "
       
  1214         #[ 1 2 3 128 255] copy beSigned
       
  1215     "
       
  1216 !
       
  1217 
       
  1218 beUnsigned
       
  1219     "thats what I am (but I don't know if this is true for subclasses."
       
  1220 
       
  1221     self class == ByteArray ifTrue:[
       
  1222         ^ self.
       
  1223     ] ifFalse:[
       
  1224         self shouldNotImplement.
       
  1225     ].
       
  1226 
       
  1227     "
       
  1228         #[ 1 2 3 128 255] copy beUnsigned
       
  1229     "
  1168 !
  1230 !
  1169 
  1231 
  1170 decodeAsLiteralArray
  1232 decodeAsLiteralArray
  1171     "given a literalEncoding in the receiver,
  1233     "given a literalEncoding in the receiver,
  1172      create & return the corresponding object.
  1234      create & return the corresponding object.
  2927     "
  2989     "
  2928      #[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues
  2990      #[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues
  2929     "
  2991     "
  2930 ! !
  2992 ! !
  2931 
  2993 
       
  2994 
  2932 !ByteArray methodsFor:'searching'!
  2995 !ByteArray methodsFor:'searching'!
  2933 
  2996 
  2934 indexOf:aByte startingAt:start
  2997 indexOf:aByte startingAt:start
  2935     "return the index of the first occurrence of the argument, aByte
  2998     "return the index of the first occurrence of the argument, aByte
  2936      in the receiver starting at start, anInteger; return 0 if not found.
  2999      in the receiver starting at start, anInteger; return 0 if not found.
  2992     "
  3055     "
  2993      #[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 startingAt:1
  3056      #[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 startingAt:1
  2994     "
  3057     "
  2995 ! !
  3058 ! !
  2996 
  3059 
       
  3060 
  2997 !ByteArray methodsFor:'testing'!
  3061 !ByteArray methodsFor:'testing'!
  2998 
  3062 
  2999 isByteArray
  3063 isByteArray
  3000     "return true, if the receiver is some kind of bytearray;
  3064     "return true, if the receiver is some kind of bytearray;
  3001      true is returned here - the method is redefined from Object."
  3065      true is returned here - the method is redefined from Object."