You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by gr...@apache.org on 2021/11/01 03:00:48 UTC

[royale-asjs] 01/02: Added missing clear method to BinaryData. Using 'clear' method to reset the internal state does not affect any previously retrieved internal 'data' object which may be referenced externally.

This is an automated email from the ASF dual-hosted git repository.

gregdove pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git

commit bd7b6cfa3f773d13ab49c5d9a7403038e3470792
Author: greg-dove <gr...@gmail.com>
AuthorDate: Mon Nov 1 15:56:45 2021 +1300

    Added missing clear method to BinaryData.
    Using 'clear' method to reset the internal state does not affect any previously retrieved internal 'data' object which may be referenced externally.
---
 .../royale/org/apache/royale/utils/BinaryData.as   | 27 ++++++++++++++++++++++
 .../royale/flexUnitTests/BinaryDataTesterTest.as   | 16 ++++++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/BinaryData.as b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/BinaryData.as
index 483bfb1..c7d1f09 100644
--- a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/BinaryData.as
+++ b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/BinaryData.as
@@ -211,6 +211,33 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
     }
 
     /**
+     *  The same as setting the length of the BinaryData to zero,
+     *  but will be more optimized on specific targets.
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @playerversion AIR 2.6
+     *  @productversion Royale 0.9.9
+     */
+    public function clear():void
+    {
+        COMPILE::JS {
+            ba = new ArrayBuffer(0);
+            _position = 0;
+            _len = 0;
+            _typedArray = null;
+            _dataView = null;
+        }
+        COMPILE::SWF {
+            //not calling ba.clear() here, to keep consistent with js implementation of 'clear' above,
+            //because there is the possibility of external usage of 'ba' via the data getter.
+            //having 'clear' on this instance affecting external references would be inconsistent between targets if we propagated the
+            //clear call to the original ByteArray instance
+            ba = new ByteArray();
+        }
+    }
+
+    /**
 	 *  create a string representation of the binary data
 	 */
 	public function toString():String
diff --git a/frameworks/projects/Core/src/test/royale/flexUnitTests/BinaryDataTesterTest.as b/frameworks/projects/Core/src/test/royale/flexUnitTests/BinaryDataTesterTest.as
index 32c2e29..caadcf7 100644
--- a/frameworks/projects/Core/src/test/royale/flexUnitTests/BinaryDataTesterTest.as
+++ b/frameworks/projects/Core/src/test/royale/flexUnitTests/BinaryDataTesterTest.as
@@ -476,7 +476,6 @@ package flexUnitTests {
             //check bytes to account for precision loss between double and float comparisons
             assertTrue(bytesMatchExpectedData(bbe,[66,173,20,123]), "Error testing post writeFloat/readFloat round-tripping");
 
-
         }
     
     
@@ -617,5 +616,20 @@ package flexUnitTests {
             assertEquals( 3.1415927410, ba.readDouble(), "BinaryData readDouble: should be 3.1415927410");
         }
 
+        [Test]
+        public function testClear():void
+        {
+            var ba:BinaryData = new BinaryData();
+            for (var i:int = 0; i < 50; i++) ba.writeByte(i);
+            ba.position = 25;
+            //test that the internal reference is severed to any external extraction of data after clear, this is different to setting 'length' to zero.
+            var olddata:Object = ba.data;
+            ba.clear();
+
+            assertEquals(0, ba.length, "BinaryData length after clear: should be 0");
+            assertEquals(0, ba.position, "BinaryData position after clear: should be 0");
+            assertNotEquals(ba.data, olddata, "BinaryData data accessor after clear: should be new internal instance");
+        }
+
 }
 }