SpanReader
A ref struct that reads primitive types from a span of bytes.
Konstruktoren
public SpanReader(ReadOnlySpan<byte> span, bool isBigEndian, bool allowNegativeShift)
spanReader = SpanReader(span, isBigEndian, allowNegativeShift)
Initializes a new instance of the SpanReader struct.
- span
- The span of bytes to read from.
- isBigEndian
- If true, reads in big-endian order; otherwise, little-endian.
- allowNegativeShift
- If true, allows negative Skip to move position backward.
public SpanReader(Memory<byte> memory, bool isBigEndian, bool allowNegativeShift)
spanReader = SpanReader(memory, isBigEndian, allowNegativeShift)
Initializes a new instance of the SpanReader struct.
- memory
- The memory of bytes to read from.
- isBigEndian
- If true, reads in big-endian order; otherwise, little-endian.
- allowNegativeShift
- If true, allows negative Skip to move position backward.
public SpanReader(byte[] buffer, bool isBigEndian, bool allowNegativeShift)
spanReader = SpanReader(buffer, isBigEndian, allowNegativeShift)
Initializes a new instance of the SpanReader struct.
- buffer
- The array of bytes to read from.
- isBigEndian
- If true, reads in big-endian order; otherwise, little-endian.
- allowNegativeShift
- If true, allows negative Skip to move position backward.
Eigenschaften
public bool End { get; }
value = spanReader.End
Gets a value indicating whether the reader has reached the end of the span.
public int Position { get; }
value = spanReader.Position
Gets the current position in the span.
public int Remaining { get; }
value = spanReader.Remaining
Gets the number of bytes remaining to be read.
Methoden
public bool ReadBoolean()
result = spanReader.ReadBoolean()
Reads a boolean value from the current position and advances the position by 1 byte.
Rückgabe: The boolean value at the current position.
public byte ReadByte()
result = spanReader.ReadByte()
Reads a byte from the current position and advances the position by 1 byte.
Rückgabe: The byte at the current position.
public char ReadChar()
result = spanReader.ReadChar()
Reads a character from the current position and advances the position.
Rückgabe: The character at the current position.
public DateTime ReadDateTime()
result = spanReader.ReadDateTime()
Reads a DateTime value from the current position and advances the position.
Rückgabe: The DateTime value at the current position.
public decimal ReadDecimal()
result = spanReader.ReadDecimal()
Reads a decimal value from the current position and advances the position by 16 bytes.
Rückgabe: The decimal value at the current position.
public double ReadDouble()
result = spanReader.ReadDouble()
Reads a double-precision floating-point value from the current position and advances the position by 8 bytes.
Rückgabe: The double-precision floating-point value at the current position.
public Guid ReadGuid()
result = spanReader.ReadGuid()
Reads a GUID value from the current position and advances the position by 16 bytes.
Rückgabe: The GUID value at the current position.
public Half ReadHalf()
result = spanReader.ReadHalf()
Reads a half-precision floating-point value from the current position and advances the position by 2 bytes.
Rückgabe: The half-precision floating-point value at the current position.
public short ReadInt16()
result = spanReader.ReadInt16()
Reads a 16-bit integer from the current position and advances the position by 2 bytes.
Rückgabe: The 16-bit integer at the current position.
public int ReadInt32()
result = spanReader.ReadInt32()
Reads a 32-bit integer from the current position and advances the position by 4 bytes.
Rückgabe: The 32-bit integer at the current position.
public long ReadInt64()
result = spanReader.ReadInt64()
Reads a 64-bit integer from the current position and advances the position by 8 bytes.
Rückgabe: The 64-bit integer at the current position.
public sbyte ReadSByte()
result = spanReader.ReadSByte()
Reads a signed byte from the current position and advances the position by 1 byte.
Rückgabe: The signed byte at the current position.
public float ReadSingle()
result = spanReader.ReadSingle()
Reads a single-precision floating-point value from the current position and advances the position by 4 bytes.
Rückgabe: The single-precision floating-point value at the current position.
public ReadOnlySpan<byte> ReadSpan(int length)
result = spanReader.ReadSpan(length)
Reads a span of bytes of the specified length from the current position and advances the position.
- length
- The number of bytes to read.
Rückgabe: A span containing the bytes read from the current position.
public string ReadString(int length, Encoding encoding)
result = spanReader.ReadString(length, encoding)
Reads a string from the current position and advances the position.
- length
- The length of the string in bytes. The length must be a multiple of the character size.
- encoding
- The encoding to use for the string. This parameter cannot be null.
Rückgabe: The string at the current position.
public void ReadStructArray<T>(T[] array, int elementSize, int count)
spanReader.ReadStructArray(array, elementSize, count)
Reads an array of structures of type from the current position and advances the position.
- array
- An array containing the structures of type read from the current position.
- elementSize
- The size of each structure in bytes.
- count
- The number of structures to read.
public T ReadStruct<T>(int size)
result = spanReader.ReadStruct(size)
Reads a structure of type from the current position and advances the position by the size of T.
Rückgabe: The structure of type at the current position.
public TimeSpan ReadTimeSpan()
result = spanReader.ReadTimeSpan()
Reads a TimeSpan value from the current position and advances the position.
Rückgabe: The TimeSpan value at the current position.
public ushort ReadUInt16()
result = spanReader.ReadUInt16()
Reads an unsigned 16-bit integer from the current position and advances the position by 2 bytes.
Rückgabe: The unsigned 16-bit integer at the current position.
public uint ReadUInt32()
result = spanReader.ReadUInt32()
Reads an unsigned 32-bit integer from the current position and advances the position by 4 bytes.
Rückgabe: The unsigned 32-bit integer at the current position.
public ulong ReadUInt64()
result = spanReader.ReadUInt64()
Reads an unsigned 64-bit integer from the current position and advances the position by 8 bytes.
Rückgabe: The unsigned 64-bit integer at the current position.
public void Skip(int count)
spanReader.Skip(count)
Skips a specified number of bytes from the current position.
- count
- The number of bytes to skip. Must be non-negative.