0

我正在检查com.adobe.net程序包的URIEncodingBitmap类,并且我很难理解内部工作原理。下面的代码:试图了解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运作和各种(位)运算符(>><<&|=等)的工作,我几乎完全丧失了什么这个类完全(或者说:它为什么按照它的方式来做事)。

难道有人会说这个班的所有位移和遮蔽的目的是什么?特别是:

  1. 什么是构造函数正确初始化,为什么?

    a。什么是this.position = (c >> 3);在做什么,或者说,为什么?

    b。什么是enc |= 1 << (c & 0x7);在做什么?

  2. 什么是在ShouldEscape()准确做的面具?

回答

2

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

ad a。此行为给定字符计算转义定义数组中的适当字节。

ad b。在字节中设置与字符对应的位。

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