#FEATURE
authorStefan Vogel <sv@exept.de>
Thu, 11 Feb 2016 16:51:59 +0100
changeset 19178 0c97c5cfb7df
parent 19177 873c60e3a4e7
child 19179 729739ca27df
#FEATURE class: ByteArray added: #asSignedByteArray #beSigned #beUnsigned changed: #asImmutableByteArray #beImmutable
ByteArray.st
--- a/ByteArray.st	Thu Feb 11 16:51:34 2016 +0100
+++ b/ByteArray.st	Thu Feb 11 16:51:59 2016 +0100
@@ -164,6 +164,7 @@
 ! !
 
 
+
 !ByteArray class methodsFor:'queries'!
 
 elementByteSize
@@ -184,6 +185,7 @@
     "Modified: 23.4.1996 / 15:56:25 / cg"
 ! !
 
+
 !ByteArray methodsFor:'Compatibility-Squeak'!
 
 bitXor:aByteArray
@@ -1058,7 +1060,14 @@
 asImmutableByteArray
     "return a write-protected copy of myself"
 
-    ^ self copy changeClassTo:ImmutableByteArray
+    |copy|
+
+    copy := self copy.
+    copy class == ByteArray ifTrue:[
+        ^ copy changeClassTo:ImmutableByteArray.
+    ] ifFalse:[
+        ^ self shouldNotImplement.
+    ].
 !
 
 asInteger
@@ -1081,10 +1090,10 @@
     ^ (LargeInteger digitBytes:self MSB:isMSBFirst) compressed
 
     "
-	(#[ 2 ] asIntegerMSB:true) hexPrintString
-	(#[ 16r1 16r2 ] asIntegerMSB:true) hexPrintString
-	(#[ 16r1 16r2 ] asIntegerMSB:false) hexPrintString
-	(#[4 0 0 0 0 0 0 0] asIntegerMSB:true) hexPrintString
+        (#[ 2 ] asIntegerMSB:true) hexPrintString
+        (#[ 16r1 16r2 ] asIntegerMSB:true) hexPrintString
+        (#[ 16r1 16r2 ] asIntegerMSB:false) hexPrintString
+        (#[4 0 0 0 0 0 0 0 0 0 0 0] asIntegerMSB:true) hexPrintString
     "
 !
 
@@ -1161,10 +1170,63 @@
     "Modified: 12.11.1996 / 15:45:02 / cg"
 !
 
+asSignedByteArray
+    "return the receiver as a signed byteArray.
+     elements > 127 are converted to negative numbers."
+
+    |cls|
+
+    cls := self class.
+    "could be an instance of a subclass..."
+    (cls == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[
+        ^ self copy changeClassTo:SignedByteArray.
+    ].
+    ^ super asSignedByteArray
+
+    "
+     'hello world' asSignedByteArray
+     #[1 2 3 4 5 6 7] asSignedByteArray
+     #[1 2 129 4 5 6 7] asSignedByteArray
+    "
+!
+
 beImmutable
     "make myself write-protected"
 
-    self changeClassTo:ImmutableByteArray
+    self class == ByteArray ifTrue:[
+        self changeClassTo:ImmutableByteArray.
+    ] ifFalse:[
+        self shouldNotImplement.
+    ].
+!
+
+beSigned
+    "make mayself signed.
+     elements > 127 are converted to negative numbers."
+
+    self class == ByteArray ifTrue:[
+        self changeClassTo:SignedByteArray.
+    ] ifFalse:[
+        self shouldNotImplement.
+    ].
+
+    "
+        #[ 1 2 3 128 255] copy beSigned
+    "
+!
+
+beUnsigned
+    "thats what I am (but I don't know if this is true for subclasses."
+
+    self class == ByteArray ifTrue:[
+        ^ self.
+    ] ifFalse:[
+        self shouldNotImplement.
+    ].
+
+    "
+        #[ 1 2 3 128 255] copy beUnsigned
+    "
 !
 
 decodeAsLiteralArray
@@ -2929,6 +2991,7 @@
     "
 ! !
 
+
 !ByteArray methodsFor:'searching'!
 
 indexOf:aByte startingAt:start
@@ -2994,6 +3057,7 @@
     "
 ! !
 
+
 !ByteArray methodsFor:'testing'!
 
 isByteArray