74 lines
1.8 KiB
C#
74 lines
1.8 KiB
C#
|
|
using System.Buffers.Binary;
|
||
|
|
|
||
|
|
namespace JCIL.Java.Class;
|
||
|
|
|
||
|
|
public struct Reader(Stream stream)
|
||
|
|
{
|
||
|
|
public readonly Stream Stream = stream;
|
||
|
|
|
||
|
|
public sbyte ReadInt8()
|
||
|
|
{
|
||
|
|
return (sbyte)Stream.ReadByte();
|
||
|
|
}
|
||
|
|
|
||
|
|
public short ReadInt16()
|
||
|
|
{
|
||
|
|
Span<byte> buffer = stackalloc byte[2];
|
||
|
|
Stream.ReadExactly(buffer);
|
||
|
|
return BinaryPrimitives.ReadInt16BigEndian(buffer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public int ReadInt32()
|
||
|
|
{
|
||
|
|
Span<byte> buffer = stackalloc byte[4];
|
||
|
|
Stream.ReadExactly(buffer);
|
||
|
|
return BinaryPrimitives.ReadInt32BigEndian(buffer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public long ReadInt64()
|
||
|
|
{
|
||
|
|
Span<byte> buffer = stackalloc byte[8];
|
||
|
|
Stream.ReadExactly(buffer);
|
||
|
|
return BinaryPrimitives.ReadInt64BigEndian(buffer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public byte ReadUInt8()
|
||
|
|
{
|
||
|
|
return (byte)Stream.ReadByte();
|
||
|
|
}
|
||
|
|
|
||
|
|
public ushort ReadUInt16()
|
||
|
|
{
|
||
|
|
Span<byte> buffer = stackalloc byte[2];
|
||
|
|
Stream.ReadExactly(buffer);
|
||
|
|
return BinaryPrimitives.ReadUInt16BigEndian(buffer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public uint ReadUInt32()
|
||
|
|
{
|
||
|
|
Span<byte> buffer = stackalloc byte[4];
|
||
|
|
Stream.ReadExactly(buffer);
|
||
|
|
return BinaryPrimitives.ReadUInt32BigEndian(buffer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public ulong ReadUInt64()
|
||
|
|
{
|
||
|
|
Span<byte> buffer = stackalloc byte[8];
|
||
|
|
Stream.ReadExactly(buffer);
|
||
|
|
return BinaryPrimitives.ReadUInt64BigEndian(buffer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public float ReadFloat()
|
||
|
|
{
|
||
|
|
Span<byte> buffer = stackalloc byte[4];
|
||
|
|
Stream.ReadExactly(buffer);
|
||
|
|
return BinaryPrimitives.ReadSingleBigEndian(buffer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public double ReadDouble()
|
||
|
|
{
|
||
|
|
Span<byte> buffer = stackalloc byte[8];
|
||
|
|
Stream.ReadExactly(buffer);
|
||
|
|
return BinaryPrimitives.ReadDoubleBigEndian(buffer);
|
||
|
|
}
|
||
|
|
}
|