Org.Mozilla.Classfile.ConstantPool.AddUtf8 C# (CSharp) Метод

AddUtf8() приватный Метод

private AddUtf8 ( string k ) : short
k string
Результат short
		internal short AddUtf8(string k)
		{
			int theIndex = itsUtf8Hash.Get(k, -1);
			if (theIndex == -1)
			{
				int strLen = k.Length;
				bool tooBigString;
				if (strLen > MAX_UTF_ENCODING_SIZE)
				{
					tooBigString = true;
				}
				else
				{
					tooBigString = false;
					// Ask for worst case scenario buffer when each char takes 3
					// bytes
					Ensure(1 + 2 + strLen * 3);
					int top = itsTop;
					itsPool[top++] = CONSTANT_Utf8;
					top += 2;
					// skip length
					char[] chars = cfw.GetCharBuffer(strLen);
					Sharpen.Runtime.GetCharsForString(k, 0, strLen, chars, 0);
					for (int i = 0; i != strLen; i++)
					{
						int c = chars[i];
						if (c != 0 && c <= unchecked((int)(0x7F)))
						{
							itsPool[top++] = unchecked((byte)c);
						}
						else
						{
							if (c > unchecked((int)(0x7FF)))
							{
								itsPool[top++] = unchecked((byte)(unchecked((int)(0xE0)) | (c >> 12)));
								itsPool[top++] = unchecked((byte)(unchecked((int)(0x80)) | ((c >> 6) & unchecked((int)(0x3F)))));
								itsPool[top++] = unchecked((byte)(unchecked((int)(0x80)) | (c & unchecked((int)(0x3F)))));
							}
							else
							{
								itsPool[top++] = unchecked((byte)(unchecked((int)(0xC0)) | (c >> 6)));
								itsPool[top++] = unchecked((byte)(unchecked((int)(0x80)) | (c & unchecked((int)(0x3F)))));
							}
						}
					}
					int utfLen = top - (itsTop + 1 + 2);
					if (utfLen > MAX_UTF_ENCODING_SIZE)
					{
						tooBigString = true;
					}
					else
					{
						// Write back length
						itsPool[itsTop + 1] = unchecked((byte)((int)(((uint)utfLen) >> 8)));
						itsPool[itsTop + 2] = unchecked((byte)utfLen);
						itsTop = top;
						theIndex = itsTopIndex++;
						itsUtf8Hash.Put(k, theIndex);
					}
				}
				if (tooBigString)
				{
					throw new ArgumentException("Too big string");
				}
			}
			SetConstantData(theIndex, k);
			itsPoolTypes.Put(theIndex, CONSTANT_Utf8);
			return (short)theIndex;
		}

Usage Example

Пример #1
0
		/// <summary>Construct a ClassFileWriter for a class.</summary>
		/// <remarks>Construct a ClassFileWriter for a class.</remarks>
		/// <param name="className">
		/// the name of the class to write, including
		/// full package qualification.
		/// </param>
		/// <param name="superClassName">
		/// the name of the superclass of the class
		/// to write, including full package qualification.
		/// </param>
		/// <param name="sourceFileName">
		/// the name of the source file to use for
		/// producing debug information, or null if debug information
		/// is not desired
		/// </param>
		public ClassFileWriter(string className, string superClassName, string sourceFileName)
		{
			generatedClassName = className;
			itsConstantPool = new ConstantPool(this);
			itsThisClassIndex = itsConstantPool.AddClass(className);
			itsSuperClassIndex = itsConstantPool.AddClass(superClassName);
			if (sourceFileName != null)
			{
				itsSourceFileNameIndex = itsConstantPool.AddUtf8(sourceFileName);
			}
			// All "new" implementations are supposed to output ACC_SUPER as a
			// class flag. This is specified in the first JVM spec, so it should
			// be old enough that it's okay to always set it.
			itsFlags = ACC_PUBLIC | ACC_SUPER;
		}