/// <summary> /// Advances the stream by a number of positions and returns the original position. /// </summary> /// <param name="count">The number of positions to advance.</param> /// <exception cref="InvalidOperationException"> /// Thrown if the position will fall outside the bounds of the underlying array segment. /// </exception> /// <returns>the position of the stream before advancing.</returns> private int CheckedAdvance(int count) { if (_currentOffset + count > _segmentEnd) { throw PacketReadingException.EndOfStream(); } int old = _currentOffset; _currentOffset += count; return(old); }
/// <inheritdoc /> /// <inheritdoc cref="CheckedAdvance" select="exception[@cref='PacketReadingException']" /> public string ReadLengthString() { // We do this in a convoluted fashion to avoid moving the position forward if we're gonna fail. short length = ReadInt16(true); if (!CanAdvance(2 + length)) { throw PacketReadingException.EndOfStream(); } string s = ReadString(_currentOffset + 2, length); _currentOffset += 2 + length; return(s); }
private short ReadInt16(bool peek) { int start; if (peek) { // If we just wanna peek, don't advance. if (!CanAdvance(2)) { throw PacketReadingException.EndOfStream(); } start = _currentOffset; } else { // Otherwise do advance. start = CheckedAdvance(2); } return(LittleEndianBitConverter.ToInt16(_buffer, start)); }