Repository: flex-asjs
Updated Branches:
  refs/heads/develop 9278a9e0c -> ca30fff9e


Added read and write UTF to BinaryData


Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/ca30fff9
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/ca30fff9
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/ca30fff9

Branch: refs/heads/develop
Commit: ca30fff9e251ae603a10eb97c593e155961271a2
Parents: 9278a9e
Author: Harbs <[email protected]>
Authored: Sun Jul 3 08:26:56 2016 +0300
Committer: Harbs <[email protected]>
Committed: Sun Jul 3 08:26:56 2016 +0300

----------------------------------------------------------------------
 .../flex/org/apache/flex/utils/BinaryData.as    | 186 +++++++++++++++++++
 1 file changed, 186 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ca30fff9/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as
----------------------------------------------------------------------
diff --git 
a/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as 
b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as
index 0adcf3c..c39d60f 100644
--- a/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as
+++ b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as
@@ -387,5 +387,191 @@ public class BinaryData
             }
         }
        }
+
+    /**
+     *  Reads a UTF-8 string from the byte stream.
+     *  The string is assumed to be prefixed with an unsigned short indicating 
the length in bytes.
+     * 
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @playerversion AIR 2.6
+     *  @productversion FlexJS 0.7.0
+     */
+    public function readUTF():String
+    {
+        COMPILE::SWF
+        {
+            return ba.readUTF();
+        }
+        // no need to do anything in AS
+        COMPILE::JS
+        {
+            //TODO Doing nothing about the length for now
+            return this.readUTFBytes(ba.byteLength);
+        }
+    }
+
+    /**
+     *  Reads a sequence of UTF-8 bytes specified by the length parameter from 
the byte stream and returns a string.
+     * 
+     *  @param An unsigned short indicating the length of the UTF-8 bytes.
+     *  
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @playerversion AIR 2.6
+     *  @productversion FlexJS 0.7.0
+     */
+    public function readUTFBytes(length:uint):String
+    {
+        COMPILE::SWF
+        {
+            return ba.readUTFBytes(length);
+        }
+
+        COMPILE::JS
+        {
+            // Code taken from GC
+            // Use native implementations if/when available
+            var bytes:Uint8Array = new Uint8Array(ba,_position,length);
+            if('TextDecoder' in window)
+            {
+                var decoder:TextDecoder = new TextDecoder('utf-8');
+                return decoder.decode(bytes);
+            }
+
+            var out:Array = [];
+            var pos:int = 0;
+            var c:int = 0;
+            var c1:int;
+            var c2:int;
+            var c3:int;
+            var c4:int;
+            while (pos < bytes.length)
+            {
+                c1 = bytes[pos++];
+                if (c1 < 128) {
+                    out[c++] = String.fromCharCode(c1);
+                }
+                else if (c1 > 191 && c1 < 224)
+                {
+                    c2 = bytes[pos++];
+                    out[c++] = String.fromCharCode((c1 & 31) << 6 | c2 & 63);
+                }
+                else if (c1 > 239 && c1 < 365)
+                {
+                    // Surrogate Pair
+                    c2 = bytes[pos++];
+                    c3 = bytes[pos++];
+                    c4 = bytes[pos++];
+                    var u:int = ((c1 & 7) << 18 | (c2 & 63) << 12 | (c3 & 63) 
<< 6 | c4 & 63) - 0x10000;
+                    out[c++] = String.fromCharCode(0xD800 + (u >> 10));
+                    out[c++] = String.fromCharCode(0xDC00 + (u & 1023));
+                }
+                else
+                {
+                    c2 = bytes[pos++];
+                    c3 = bytes[pos++];
+                    out[c++] = String.fromCharCode((c1 & 15) << 12 | (c2 & 63) 
<< 6 | c3 & 63);
+                }
+            }
+            return out.join('');
+        }
+    }
+
+
+    /**
+     *  Writes a UTF-8 string to the byte stream.
+     *  The length of the UTF-8 string in bytes is written first, as a 16-bit 
integer,
+     *  followed by the bytes representing the characters of the string. 
+     * 
+     *  @param The string value to be written.
+     *  
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @playerversion AIR 2.6
+     *  @productversion FlexJS 0.7.0
+     */
+    public function writeUTF(value:String):void
+    {
+        COMPILE::SWF
+        {
+            return ba.writeUTF(value);
+        }
+
+        COMPILE::JS
+        {
+            //TODO Doing nothing about the length for now
+            return this.writeUTFBytes(value);
+        }
+    }
+
+    /**
+     *  Writes a UTF-8 string to the byte stream. Similar to the writeUTF() 
method,
+     *  but writeUTFBytes() does not prefix the string with a 16-bit length 
word.
+     * 
+     *  @param The string value to be written.
+     *  
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @playerversion AIR 2.6
+     *  @productversion FlexJS 0.7.0
+     */
+    public function writeUTFBytes(str:String):void
+    {
+        COMPILE::SWF
+        {
+            ba.writeUTFBytes(str);
+        }
+
+        COMPILE::JS
+        {
+            // Code taken from GC
+            // Use native implementations if/when available
+            var bytes:Uint8Array;
+            if('TextEncoder' in window)
+            {
+                var encoder:TextEncoder = new TextEncoder('utf-8');
+                bytes = encoder.encode(str);
+                ba = bytes.buffer;
+                return;
+            }
+
+            var out:Array = [];
+            var p:int = 0;
+            var c:int;
+            for (var i:int = 0; i < str.length; i++)
+            {
+                c = str.charCodeAt(i);
+                if (c < 128)
+                {
+                    out[p++] = c;
+                }
+                else if (c < 2048)
+                {
+                    out[p++] = (c >> 6) | 192;
+                    out[p++] = (c & 63) | 128;
+                }
+                else if (((c & 0xFC00) == 0xD800) && (i + 1) < str.length &&
+                    ((str.charCodeAt(i + 1) & 0xFC00) == 0xDC00))
+                {
+                    // Surrogate Pair
+                    c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) 
& 0x03FF);
+                    out[p++] = (c >> 18) | 240;
+                    out[p++] = ((c >> 12) & 63) | 128;
+                    out[p++] = ((c >> 6) & 63) | 128;
+                    out[p++] = (c & 63) | 128;
+                }
+                else
+                {
+                    out[p++] = (c >> 12) | 224;
+                    out[p++] = ((c >> 6) & 63) | 128;
+                    out[p++] = (c & 63) | 128;
+                }
+            }
+            bytes = new Uint8Array(out);
+            ba = bytes.buffer;
+
+        }
+    }
 }
 }

Reply via email to