Org.Mozilla.Classfile.ClassFileWriter.ToByteArray C# (CSharp) Method

ToByteArray() public method

Get the class file as array of bytesto the OutputStream.
Get the class file as array of bytesto the OutputStream.
public ToByteArray ( ) : byte[]
return byte[]
		public virtual byte[] ToByteArray()
		{
			int dataSize = GetWriteSize();
			byte[] data = new byte[dataSize];
			int offset = 0;
			short sourceFileAttributeNameIndex = 0;
			if (itsSourceFileNameIndex != 0)
			{
				sourceFileAttributeNameIndex = itsConstantPool.AddUtf8("SourceFile");
			}
			offset = PutInt32(FileHeaderConstant, data, offset);
			offset = PutInt16(MinorVersion, data, offset);
			offset = PutInt16(MajorVersion, data, offset);
			offset = itsConstantPool.Write(data, offset);
			offset = PutInt16(itsFlags, data, offset);
			offset = PutInt16(itsThisClassIndex, data, offset);
			offset = PutInt16(itsSuperClassIndex, data, offset);
			offset = PutInt16(itsInterfaces.Size(), data, offset);
			for (int i = 0; i < itsInterfaces.Size(); i++)
			{
				int interfaceIndex = System.Convert.ToInt16(((short)(itsInterfaces.Get(i))));
				offset = PutInt16(interfaceIndex, data, offset);
			}
			offset = PutInt16(itsFields.Size(), data, offset);
			for (int i_1 = 0; i_1 < itsFields.Size(); i_1++)
			{
				ClassFileField field = (ClassFileField)itsFields.Get(i_1);
				offset = field.Write(data, offset);
			}
			offset = PutInt16(itsMethods.Size(), data, offset);
			for (int i_2 = 0; i_2 < itsMethods.Size(); i_2++)
			{
				ClassFileMethod method = (ClassFileMethod)itsMethods.Get(i_2);
				offset = method.Write(data, offset);
			}
			if (itsSourceFileNameIndex != 0)
			{
				offset = PutInt16(1, data, offset);
				// attributes count
				offset = PutInt16(sourceFileAttributeNameIndex, data, offset);
				offset = PutInt32(2, data, offset);
				offset = PutInt16(itsSourceFileNameIndex, data, offset);
			}
			else
			{
				offset = PutInt16(0, data, offset);
			}
			// no attributes
			if (offset != dataSize)
			{
				// Check getWriteSize is consistent with write!
				throw new Exception();
			}
			return data;
		}

Usage Example

コード例 #1
0
ファイル: Codegen.cs プロジェクト: hazzik/Rhino.Net
		private byte[] GenerateCode(string encodedSource)
		{
			bool hasScript = (scriptOrFnNodes[0].GetType() == Token.SCRIPT);
			bool hasFunctions = (scriptOrFnNodes.Length > 1 || !hasScript);
			string sourceFile = null;
			if (compilerEnv.IsGenerateDebugInfo())
			{
				sourceFile = scriptOrFnNodes[0].GetSourceName();
			}
			ClassFileWriter cfw = new ClassFileWriter(mainClassName, SUPER_CLASS_NAME, sourceFile);
			cfw.AddField(ID_FIELD_NAME, "I", ClassFileWriter.ACC_PRIVATE);
			if (hasFunctions)
			{
				GenerateFunctionConstructor(cfw);
			}
			if (hasScript)
			{
				cfw.AddInterface("org/mozilla/javascript/Script");
				GenerateScriptCtor(cfw);
				GenerateMain(cfw);
				GenerateExecute(cfw);
			}
			GenerateCallMethod(cfw);
			GenerateResumeGenerator(cfw);
			GenerateNativeFunctionOverrides(cfw, encodedSource);
			int count = scriptOrFnNodes.Length;
			for (int i = 0; i != count; ++i)
			{
				ScriptNode n = scriptOrFnNodes[i];
				BodyCodegen bodygen = new BodyCodegen();
				bodygen.cfw = cfw;
				bodygen.codegen = this;
				bodygen.compilerEnv = compilerEnv;
				bodygen.scriptOrFn = n;
				bodygen.scriptOrFnIndex = i;
				try
				{
					bodygen.GenerateBodyCode();
				}
				catch (ClassFileWriter.ClassFileFormatException e)
				{
					throw ReportClassFileFormatException(n, e.Message);
				}
				if (n.GetType() == Token.FUNCTION)
				{
					OptFunctionNode ofn = OptFunctionNode.Get(n);
					GenerateFunctionInit(cfw, ofn);
					if (ofn.IsTargetOfDirectCall())
					{
						EmitDirectConstructor(cfw, ofn);
					}
				}
			}
			EmitRegExpInit(cfw);
			EmitConstantDudeInitializers(cfw);
			return cfw.ToByteArray();
		}