/// <summary> /// Reads a single unsigned 16bit integer from the reader. /// </summary> /// <returns>The unsigned 16bit integer read.</returns> public ushort ReadUInt16() { if (Position + 2 > Length) { throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected 2 bytes but reader only has {Length - Position} bytes remaining."); } ushort v = BigEndianHelper.ReadUInt16(buffer.Buffer, buffer.Offset + Position); Position += 2; return(v); }
/// <summary> /// Reads an array unsigned 16bit integers from the reader. /// </summary> /// <param name="destination">The array to read strings into.</param> /// <param name="offset">The offset at which to write bytes into the array.</param> public void ReadUInt16sInto(ushort[] destination, int offset) { if (Position + 4 > Length) { throw new EndOfStreamException($"Failed to read as the reader does not have enough data remaining. Expected 4 byte array length header but reader only has {Length - Position} bytes remaining."); } int length = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position); if (Position + 4 + length * 2 > Length) { throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected {length * 2} bytes but reader only has {Length - Position - 4} bytes remaining."); } for (int i = 0, j = buffer.Offset + Position + 4; i < length; i++, j += 2) { destination[i + offset] = BigEndianHelper.ReadUInt16(buffer.Buffer, j); } Position += 4 + length * 2; }
/// <summary> /// Reads an array unsigned 16bit integers from the reader. /// </summary> /// <returns>The array of unsigned 16bit integers read.</returns> public ushort[] ReadUInt16s() { if (Position + 4 > Length) { throw new EndOfStreamException($"Failed to read as the reader does not have enough data remaining. Expected 4 byte array length header but reader only has {Length - Position} bytes remaining."); } int length = BigEndianHelper.ReadInt32(buffer.Buffer, buffer.Offset + Position); if (Position + 4 + length * 2 > Length) { throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected {length * 2} bytes but reader only has {Length - Position - 4} bytes remaining."); } ushort[] array = new ushort[length]; for (int i = 0, j = buffer.Offset + Position + 4; i < length; i++, j += 2) { array[i] = BigEndianHelper.ReadUInt16(buffer.Buffer, j); } Position += 4 + length * 2; return(array); }