You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@royale.apache.org by GitBox <gi...@apache.org> on 2018/05/29 10:55:08 UTC

[GitHub] nihavend commented on a change in pull request #254: Base64Decoder class added and wrapper classes modified to support decoding

nihavend commented on a change in pull request #254: Base64Decoder class added and wrapper classes modified to support decoding
URL: https://github.com/apache/royale-asjs/pull/254#discussion_r191381749
 
 

 ##########
 File path: frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64Decoder.as
 ##########
 @@ -0,0 +1,258 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+package mx.utils
+{
+
+COMPILE::SWF
+{
+    import flash.utils.ByteArray;
+}
+
+COMPILE::JS
+{
+    import goog.DEBUG;
+}
+/*
+import mx.resources.IResourceManager;
+import mx.resources.ResourceManager;
+
+[ResourceBundle("utils")]
+*/
+
+/**
+ * A utility class to decode a Base64 encoded String to a ByteArray.
+ *  
+ *  @langversion 3.0
+ *  @playerversion Flash 9
+ *  @playerversion AIR 1.1
+ *  @productversion Royale 0.9.4
+ */
+public class Base64Decoder
+{
+    //--------------------------------------------------------------------------
+    //
+    //  Constructor
+    //
+    //--------------------------------------------------------------------------
+
+    /**
+     * Constructor.
+     *  
+     *  @langversion 3.0
+     *  @playerversion Flash 9
+     *  @playerversion AIR 1.1
+     *  @productversion Royale 0.9.4
+     */
+    COMPILE::SWF
+    public function Base64Decoder()
+    {
+        super();
+        data = new ByteArray();
+    }
+
+	COMPILE::JS
+    public function Base64Decoder()
+    {
+        super();
+    }
+
+
+    //--------------------------------------------------------------------------
+    //
+    //  Methods
+    //
+    //--------------------------------------------------------------------------
+
+    /**
+     * Decodes a Base64 encoded String and adds the result to an internal
+     * buffer. Strings must be in ASCII format. 
+     * 
+     * <p>Subsequent calls to this method add on to the internal
+     * buffer. After all data have been encoded, call <code>toByteArray()</code>
+     * to obtain a decoded <code>flash.utils.ByteArray</code>.</p>
+     * 
+     * @param encoded The Base64 encoded String to decode.
+     *  
+     *  @langversion 3.0
+     *  @playerversion Flash 9
+     *  @playerversion AIR 1.1
+     *  @productversion Royale 0.9.4
+     */
+
+    COMPILE::JS
+    public function decode(data:String):String
+    {
+
+		var bytes:Object = new Base64JSWrapper().toByteArray(data);
+		var decodedString:String = new TextEncoderLiteWrapper('utf-8').decode(bytes);
+
+		return decodedString;
+    }
+
+
+    COMPILE::SWF
+    public function decode(encoded:String):void
+    {
+        for (var i:uint = 0; i < encoded.length; ++i)
+        {
+            var c:Number = encoded.charCodeAt(i);
+
+            if (c == ESCAPE_CHAR_CODE)
+                work[count++] = -1;
+            else if (inverse[c] != 64)
+                work[count++] = inverse[c];
+            else
+                continue;
+
+            if (count == 4)
+            {
+                count = 0;
+                data.writeByte((work[0] << 2) | ((work[1] & 0xFF) >> 4));
+                filled++;
+
+                if (work[2] == -1)
+                    break;
+
+                data.writeByte((work[1] << 4) | ((work[2] & 0xFF) >> 2));
+                filled++;
+
+                if (work[3] == -1)
+                    break;
+
+                data.writeByte((work[2] << 6) | work[3]);
+                filled++;
+            }
+        }
+    }
+
+    COMPILE::SWF
+    public function drain():ByteArray
+    {
+        var result:ByteArray = new ByteArray();
+
+        var oldPosition:uint = data.position;    
+        data.position = 0;  // technically, shouldn't need to set this, but carrying over from previous implementation
+        result.writeBytes(data, 0, data.length);        
+        data.position = oldPosition;
+        result.position = 0;
+        
+        filled = 0;
+        return result;
+    }
+
+    COMPILE::SWF
+    public function flush():ByteArray
+    {
+        if (count > 0)
 
 Review comment:
   @piotrzarzycki21  After commenting out IResourceManager and ResourceManager - i am really not sur to handle - the condition became meaningless. Sorry, i missed that. Will fix it.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services