我正在检查URIEncodingBitmap
包的com.adobe.net
类,而且我很难理解内部工作原理。这是代码:
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
的工作方式以及各种(按位)运算符(>>
,<<
,&
,|=
等的工作原理。 ),我几乎完全失去了这门课的确切做法(或者更确切地说:为什么它按照它的方式做事)。
有人可以说明这个类中所有位移和屏蔽的目的是什么?特别:
究竟是什么构造函数初始化,为什么?
一个。 this.position = (c >> 3);
做什么,或者更确切地说,为什么?
湾什么是enc |= 1 << (c & 0x7);
在做什么?
在ShouldEscape()
完全正确的掩码是什么?
答案 0 :(得分:2)
ad 1.构造函数创建一个转义定义数组(长度为16个字节= 128位)。每个字符一位。位的位置对应于字符的序数值,其值表示字符是否应该被转义。
ad a。此行计算给定字符的转义定义数组中的相应字节。
ad b。设置与字节内的字符对应的位。
ad 2.掩码包含给定字符的相应字节,用于检查相应位是否已设置。