【问题标题】:Trying to understand the workings of com.adobe.net.URIEncodingBitmap试图了解 com.adobe.net.URIEncodingBitmap 的工作原理
【发布时间】:2013-11-24 17:49:45
【问题描述】:

我正在检查 com.adobe.net 包的 URIEncodingBitmap 类,我很难准确地理解其内部工作原理。代码如下:

package com.adobe.net
{
    import flash.utils.ByteArray;

    /**
     * This class implements an efficient lookup table for URI
     * character escaping.  This class is only needed if you
     * create a derived class of URI to handle custom URI
     * syntax.  This class is used internally by URI.
     * 
     * @langversion ActionScript 3.0
     * @playerversion Flash 9.0* 
     */
    public class URIEncodingBitmap extends ByteArray
    {
        /**
         * Constructor.  Creates an encoding bitmap using the given
         * string of characters as the set of characters that need
         * to be URI escaped.
         * 
         * @langversion ActionScript 3.0
         * @playerversion Flash 9.0
         */
        public function URIEncodingBitmap(charsToEscape:String) : void
        {
            var i:int;
            var data:ByteArray = new ByteArray();

            // Initialize our 128 bits (16 bytes) to zero
            for (i = 0; i < 16; i++)
                this.writeByte(0);

            data.writeUTFBytes(charsToEscape);
            data.position = 0;

            while (data.bytesAvailable)
            {
                var c:int = data.readByte();

                if (c > 0x7f)
                    continue;  // only escape low bytes

                var enc:int;
                this.position = (c >> 3);
                enc = this.readByte();
                enc |= 1 << (c & 0x7);
                this.position = (c >> 3);
                this.writeByte(enc);
            }
        }

        /**
         * Based on the data table contained in this object, check
         * if the given character should be escaped.
         * 
         * @param char  the character to be escaped.  Only the first
         * character in the string is used.  Any other characters
         * are ignored.
         * 
         * @return  the integer value of the raw UTF8 character.  For
         * example, if '%' is given, the return value is 37 (0x25).
         * If the character given does not need to be escaped, the
         * return value is zero.
         * 
         * @langversion ActionScript 3.0
         * @playerversion Flash 9.0 
         */
        public function ShouldEscape(char:String) : int
        {
            var data:ByteArray = new ByteArray();
            var c:int, mask:int;

            // write the character into a ByteArray so
            // we can pull it out as a raw byte value.
            data.writeUTFBytes(char);
            data.position = 0;
            c = data.readByte();

            if (c & 0x80)
            {
                // don't escape high byte characters.  It can make international
                // URI's unreadable.  We just want to escape characters that would
                // make URI syntax ambiguous.
                return 0;
            }
            else if ((c < 0x1f) || (c == 0x7f))
            {
                // control characters must be escaped.
                return c;
            }

            this.position = (c >> 3);
            mask = this.readByte();

            if (mask & (1 << (c & 0x7)))
            {
                // we need to escape this, return the numeric value
                // of the character
                return c;
            }
            else
            {
                return 0;
            }
        }
    }
}

虽然我了解ByteArray 的工作原理以及各种(按位)运算符(&gt;&gt;、&lt;&lt;、&amp;、|= 等)的工作原理,但我几乎完全不知所措这个类究竟做了什么(或者更确切地说:为什么它会以这种方式做事)。

有人可以简要介绍一下此类中所有位移和掩码的目的吗?特别是:

  1. 构造函数到底初始化了什么,为什么?

    一个。 this.position = (c &gt;&gt; 3); 在做什么,或者更确切地说,为什么?

    b. enc |= 1 &lt;&lt; (c &amp; 0x7); 在做什么?

  2. ShouldEscape() 中的掩码到底在做什么?

【问题讨论】:

    标签: actionscript-3 bit-manipulation urlencode bit-shift bitmask


    【解决方案1】:

    ad 1. 构造函数创建一个转义定义数组(长度 16 字节 = 128 位)。每个字符一位。位的位置对应于字符的序数值,其值表示字符是否应该转义。

    广告此行计算给定字符的转义定义数组中的适当字节。

    广告 b.设置字节内字符对应的位。

    ad 2. 掩码包含给定字符的适当字节,用于检查相应位是否设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多