/// <summary> /// Reads a single single from the reader. /// </summary> /// <returns>The single read.</returns> public float ReadSingle() { if (Position + 4 > Length) { throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected 4 bytes but reader only has {Length - Position} bytes remaining."); } float v = BigEndianHelper.ReadSingle(buffer.Buffer, buffer.Offset + Position); Position += 4; return(v); }
/// <summary> /// Reads an array of singles from the reader. /// </summary> /// <param name="destination">The array to read singles into.</param> /// <param name="offset">The offset at which to write bytes into the array.</param> public void ReadSinglesInto(float[] 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 * 4 > Length) { throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected {length * 4} bytes but reader only has {Length - Position - 4} bytes remaining."); } for (int i = 0, j = buffer.Offset + Position + 4; i < length; i++, j += 4) { destination[i + offset] = BigEndianHelper.ReadSingle(buffer.Buffer, j); } Position += 4 + length * 4; }
/// <summary> /// Reads an array of singles from the reader. /// </summary> /// <returns>The array of singles read.</returns> public float[] ReadSingles() { 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 * 4 > Length) { throw new EndOfStreamException($"Failed to read data from reader as the reader does not have enough data remaining. Expected {length * 4} bytes but reader only has {Length - Position - 4} bytes remaining."); } float[] array = new float[length]; for (int i = 0, j = buffer.Offset + Position + 4; i < length; i++, j += 4) { array[i] = BigEndianHelper.ReadSingle(buffer.Buffer, j); } Position += 4 + length * 4; return(array); }