本文整理匯總了C#中BigEndianBinaryReader.ReadString方法的典型用法代碼示例。如果您正苦於以下問題:C# BigEndianBinaryReader.ReadString方法的具體用法?C# BigEndianBinaryReader.ReadString怎麽用?C# BigEndianBinaryReader.ReadString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類BigEndianBinaryReader
的用法示例。
在下文中一共展示了BigEndianBinaryReader.ReadString方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: GetClassName
// This method parses just enough of the class file to obtain its name, it doesn't
// validate the class file structure, but it may throw a ClassFormatError if it
// encounters bogus data
internal static string GetClassName(byte[] buf, int offset, int length)
{
BigEndianBinaryReader br = new BigEndianBinaryReader(buf, offset, length);
if(br.ReadUInt32() != 0xCAFEBABE)
{
throw new ClassFormatError("Bad magic number");
}
int minorVersion = br.ReadUInt16();
int majorVersion = br.ReadUInt16();
if((majorVersion & FLAG_MASK_MAJORVERSION) != majorVersion
|| majorVersion < SupportedVersions.Minimum
|| majorVersion > SupportedVersions.Maximum
|| (majorVersion == SupportedVersions.Minimum && minorVersion < 3)
|| (majorVersion == SupportedVersions.Maximum && minorVersion != 0))
{
throw new UnsupportedClassVersionError(majorVersion + "." + minorVersion);
}
int constantpoolcount = br.ReadUInt16();
int[] cpclass = new int[constantpoolcount];
string[] utf8_cp = new string[constantpoolcount];
for(int i = 1; i < constantpoolcount; i++)
{
Constant tag = (Constant)br.ReadByte();
switch(tag)
{
case Constant.Class:
cpclass[i] = br.ReadUInt16();
break;
case Constant.Double:
case Constant.Long:
br.Skip(8);
i++;
break;
case Constant.Fieldref:
case Constant.InterfaceMethodref:
case Constant.Methodref:
case Constant.InvokeDynamic:
case Constant.NameAndType:
case Constant.Float:
case Constant.Integer:
br.Skip(4);
break;
case Constant.MethodHandle:
br.Skip(3);
break;
case Constant.String:
case Constant.MethodType:
br.Skip(2);
break;
case Constant.Utf8:
utf8_cp[i] = br.ReadString("<unknown>");
break;
default:
throw new ClassFormatError("Illegal constant pool type 0x{0:X}", tag);
}
}
br.ReadUInt16(); // access_flags
try
{
return String.Intern(utf8_cp[cpclass[br.ReadUInt16()]].Replace('/', '.'));
}
catch(Exception x)
{
throw new ClassFormatError("{0}: {1}", x.GetType().Name, x.Message);
}
}
示例2: ClassFile
internal ClassFile(byte[] buf, int offset, int length, string inputClassName, ClassFileParseOptions options)
{
try
{
BigEndianBinaryReader br = new BigEndianBinaryReader(buf, offset, length);
if(br.ReadUInt32() != 0xCAFEBABE)
{
throw new ClassFormatError("{0} (Bad magic number)", inputClassName);
}
ushort minorVersion = br.ReadUInt16();
ushort majorVersion = br.ReadUInt16();
if((majorVersion & FLAG_MASK_MAJORVERSION) != majorVersion
|| majorVersion < SupportedVersions.Minimum
|| majorVersion > SupportedVersions.Maximum
|| (majorVersion == SupportedVersions.Minimum && minorVersion < 3)
|| (majorVersion == SupportedVersions.Maximum && minorVersion != 0))
{
throw new UnsupportedClassVersionError(inputClassName + " (" + majorVersion + "." + minorVersion + ")");
}
flags = majorVersion;
int constantpoolcount = br.ReadUInt16();
constantpool = new ConstantPoolItem[constantpoolcount];
string[] utf8_cp = new string[constantpoolcount];
for(int i = 1; i < constantpoolcount; i++)
{
Constant tag = (Constant)br.ReadByte();
switch(tag)
{
case Constant.Class:
constantpool[i] = new ConstantPoolItemClass(br);
break;
case Constant.Double:
constantpool[i] = new ConstantPoolItemDouble(br);
i++;
break;
case Constant.Fieldref:
constantpool[i] = new ConstantPoolItemFieldref(br);
break;
case Constant.Float:
constantpool[i] = new ConstantPoolItemFloat(br);
break;
case Constant.Integer:
constantpool[i] = new ConstantPoolItemInteger(br);
break;
case Constant.InterfaceMethodref:
constantpool[i] = new ConstantPoolItemInterfaceMethodref(br);
break;
case Constant.Long:
constantpool[i] = new ConstantPoolItemLong(br);
i++;
break;
case Constant.Methodref:
constantpool[i] = new ConstantPoolItemMethodref(br);
break;
case Constant.NameAndType:
constantpool[i] = new ConstantPoolItemNameAndType(br);
break;
case Constant.MethodHandle:
if (majorVersion < 51)
goto default;
constantpool[i] = new ConstantPoolItemMethodHandle(br);
break;
case Constant.MethodType:
if (majorVersion < 51)
goto default;
constantpool[i] = new ConstantPoolItemMethodType(br);
break;
case Constant.InvokeDynamic:
if (majorVersion < 51)
goto default;
constantpool[i] = new ConstantPoolItemInvokeDynamic(br);
break;
case Constant.String:
constantpool[i] = new ConstantPoolItemString(br);
break;
case Constant.Utf8:
utf8_cp[i] = br.ReadString(inputClassName);
break;
default:
throw new ClassFormatError("{0} (Illegal constant pool type 0x{1:X})", inputClassName, tag);
}
}
for(int i = 1; i < constantpoolcount; i++)
{
if(constantpool[i] != null)
{
try
{
constantpool[i].Resolve(this, utf8_cp, options);
}
catch(ClassFormatError x)
{
// HACK at this point we don't yet have the class name, so any exceptions throw
// are missing the class name
throw new ClassFormatError("{0} ({1})", inputClassName, x.Message);
}
catch(IndexOutOfRangeException)
{
throw new ClassFormatError("{0} (Invalid constant pool item #{1})", inputClassName, i);
}
//.........這裏部分代碼省略.........
示例3: StringTests
public void StringTests(String expectedValue, Byte[] givenBytes)
{
// arrange
var binaryReader = new BigEndianBinaryReader(givenBytes);
// act
var actualValue = binaryReader.ReadString();
// assert
Assert.That(expectedValue, Is.EqualTo(actualValue));
}
示例4: ClassFile
internal ClassFile(byte[] buf, int offset, int length, string inputClassName, bool allowJavaLangObject)
{
try
{
BigEndianBinaryReader br = new BigEndianBinaryReader(buf, offset, length);
if (br.ReadUInt32() != 0xCAFEBABE)
{
throw new ClassFormatError("{0} (Bad magic number)", inputClassName);
}
int minorVersion = br.ReadUInt16();
majorVersion = br.ReadUInt16();
if (majorVersion < SupportedVersions.Minimum || majorVersion > SupportedVersions.Maximum)
{
throw new UnsupportedClassVersionError(inputClassName + " (" + majorVersion + "." + minorVersion + ")");
}
int constantpoolcount = br.ReadUInt16();
constantpool = new ConstantPoolItem[constantpoolcount];
utf8_cp = new string[constantpoolcount];
for (int i = 1; i < constantpoolcount; i++)
{
Constant tag = (Constant) br.ReadByte();
switch (tag)
{
case Constant.Class:
constantpool[i] = new ConstantPoolItemClass(br);
break;
case Constant.Double:
constantpool[i] = new ConstantPoolItemDouble(br);
i++;
break;
case Constant.Fieldref:
constantpool[i] = new ConstantPoolItemFieldref(br);
break;
case Constant.Float:
constantpool[i] = new ConstantPoolItemFloat(br);
break;
case Constant.Integer:
constantpool[i] = new ConstantPoolItemInteger(br);
break;
case Constant.InterfaceMethodref:
constantpool[i] = new ConstantPoolItemInterfaceMethodref(br);
break;
case Constant.Long:
constantpool[i] = new ConstantPoolItemLong(br);
i++;
break;
case Constant.Methodref:
constantpool[i] = new ConstantPoolItemMethodref(br);
break;
case Constant.NameAndType:
constantpool[i] = new ConstantPoolItemNameAndType(br);
break;
case Constant.String:
constantpool[i] = new ConstantPoolItemString(br);
break;
case Constant.Utf8:
utf8_cp[i] = br.ReadString(inputClassName);
break;
default:
throw new ClassFormatError("{0} (Illegal constant pool type 0x{1:X})", inputClassName, tag);
}
}
for (int i = 1; i < constantpoolcount; i++)
{
if (constantpool[i] != null)
{
try
{
constantpool[i].Resolve(this);
}
catch (ClassFormatError x)
{
// HACK at this point we don't yet have the class name, so any exceptions throw
// are missing the class name
throw new ClassFormatError("{0} ({1})", inputClassName, x.Message);
}
catch (IndexOutOfRangeException)
{
throw new ClassFormatError("{0} (Invalid constant pool item #{1})", inputClassName, i);
}
catch (InvalidCastException)
{
throw new ClassFormatError("{0} (Invalid constant pool item #{1})", inputClassName, i);
}
}
}
access_flags = (__Modifiers) br.ReadUInt16();
// NOTE although the vmspec says (in 4.1) that interfaces must be marked abstract, earlier versions of
// javac (JDK 1.1) didn't do this, so the VM doesn't enforce this rule
// NOTE although the vmspec implies (in 4.1) that ACC_SUPER is illegal on interfaces, it doesn't enforce this
if ((IsInterface && IsFinal) || (IsAbstract && IsFinal))
{
throw new ClassFormatError("{0} (Illegal class modifiers 0x{1:X})", inputClassName, access_flags);
}
this_class = br.ReadUInt16();
ValidateConstantPoolItemClass(inputClassName, this_class);
super_class = br.ReadUInt16();
// NOTE for convenience we allow parsing java/lang/Object (which has no super class), so
// we check for super_class != 0
if (super_class != 0)
//.........這裏部分代碼省略.........
示例5: ReadConstantPool
//.........這裏部分代碼省略.........
u2 string_index;
}
*/
item.ConstantType = ConstantType.String;
item.StringIndex = reader.ReadUInt16();
break;
case ConstantType.Integer:
/*
CONSTANT_Integer_info {
u1 tag;
u4 bytes;
}
*/
item.ConstantType = ConstantType.Integer;
item.Integer = reader.ReadInt32();
break;
case ConstantType.Float:
/*
CONSTANT_Float_info {
u1 tag;
u4 bytes;
}
*/
item.ConstantType = ConstantType.Float;
item.Float = reader.ReadSingle();
break;
case ConstantType.Long:
/*
CONSTANT_Long_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
*/
item.ConstantType = ConstantType.Long;
item.Long = reader.ReadInt64();
// JVM Spec. 4.4.5.
// All 8-byte constants take up two entries in the
// constant_pool table of the class file. If a
// CONSTANT_Long_info or CONSTANT_Double_info structure
// is the item in the constant_pool table at index n,
// then the next usable item in the pool is located at
// index n+2. The constant_pool index n+1 must be valid
// but is considered unusable.2
// 2 In retrospect, making 8-byte constants take two
// constant pool entries was a poor choice.
++i;
break;
case ConstantType.Double:
/*
CONSTANT_Double_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
*/
item.ConstantType = ConstantType.Double;
item.Double = reader.ReadDouble();
// JVM Spec. 4.4.5.
// All 8-byte constants take up two entries in the
// constant_pool table of the class file. If a
// CONSTANT_Long_info or CONSTANT_Double_info structure
// is the item in the constant_pool table at index n,
// then the next usable item in the pool is located at
// index n+2. The constant_pool index n+1 must be valid
// but is considered unusable.2
// 2 In retrospect, making 8-byte constants take two
// constant pool entries was a poor choice.
++i;
break;
case ConstantType.NameAndType:
/*
CONSTANT_NameAndType_info {
u1 tag;
u2 name_index;
u2 descriptor_index;
}
*/
item.ConstantType = ConstantType.NameAndType;
item.NameIndex = reader.ReadUInt16();
item.DescriptorIndex = reader.ReadUInt16();
break;
case ConstantType.Utf8:
/*
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}
*/
item.ConstantType = ConstantType.Utf8;
item.String = reader.ReadString(reader.ReadUInt16());
break;
default:
throw new ApplicationException("Wrong ConstantType: " +
tag);
}
}
return constantPool;
}
示例6: StringTests
public void StringTests(String expectedValue, Byte[] givenBytes)
{
// arrange
var memoryStream = new MemoryStream(givenBytes);
var binaryReader = new BigEndianBinaryReader(memoryStream);
// act
var actualValue = binaryReader.ReadString();
// assert
Assert.Equal(expectedValue, actualValue);
}