/// <summary> /// Takes bytes starting from an offset in an array segment and appends as many as possible to the buffer. /// </summary> /// <remarks> /// This method will append a maximum of <paramref name="count"/> elements to the end of the buffer, starting at <paramref name="offset"/> in <paramref name="buffer"/>. /// <para>If there is not enough space for <paramref name="count"/> elements, it will fill the remaining space with bytes from the start of <paramref name="buffer"/>.</para> /// </remarks> /// <param name="buffer">The array to read from.</param> /// <param name="offset">The start of the segment.</param> /// <param name="count">The number of bytes to append.</param> /// <inheritdoc cref="ThrowIfDisposed()" select="exception[@cref='ObjectDisposedException']" /> /// <exception cref="ArgumentNullException">Thrown if <paramref name="buffer" /> is <see langword="null"/>.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="offset"/> is negative or <paramref name="count"/> is non-positive.</exception> /// <exception cref="ArraySegmentException">Thrown if the array segment given by the <paramref name="offset"/> and <paramref name="count"/> parameters falls outside of the given array's bounds.</exception> /// <returns>the number of bytes that were stored.</returns> public int AppendFill(byte[] buffer, int offset, int count) { ThrowIfDisposed(); Guard.NotNull(() => buffer, buffer); if (offset < 0) { throw GetOffsetIsNegativeException(offset); } if (count <= 0) { throw GetCountIsNonPositiveException(count); } if (buffer.Length < offset || offset + count > buffer.Length) { throw ArraySegmentException.GetByStartAndLength(offset, count); } int stored = AppendInternal(buffer, offset, count); return(stored); }
/// <summary> /// Initializes a new instance of the <see cref="PacketReader"/> class using the given byte array segment as a buffer. /// </summary> /// <param name="buffer">The byte array to use as a buffer.</param> /// <param name="offset">The start of the buffer segment.</param> /// <param name="length">The length of the buffer segment.</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="buffer"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown if <paramref name="offset"/> or <paramref name="length"/> are negative. /// </exception> /// <exception cref="ArraySegmentException"> /// Thrown if the array segment given by the <paramref name="offset"/> and /// <paramref name="length"/> parameters falls outside of the array's bounds. /// </exception> public PacketReader(byte[] buffer, int offset, int length) { Guard.NotNull(() => buffer, buffer); if (offset < 0) { throw new ArgumentOutOfRangeException(nameof(offset), offset, CommonStrings.OffsetMustBeNonNegative); } if (length < 0) { throw new ArgumentOutOfRangeException(nameof(length), length, CommonStrings.LengthMustBeNonNegative); } if (buffer.Length < offset || offset + length > buffer.Length) { throw ArraySegmentException.GetByStartAndLength(offset, length); } _buffer = buffer; _currentOffset = _segmentStart = offset; _segmentEnd = offset + length; }
/// <summary> /// Returns an integer by reading bytes in little-endian byte order. /// </summary> /// <param name="buffer">The buffer to read bytes from.</param> /// <param name="offset">The index of the start of the segment.</param> /// <param name="count">The number of bytes to read.</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="buffer"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <para>Thrown if <paramref name="offset"/> falls outside the bounds of <paramref name="buffer"/>, </para> /// <para>OR, </para> /// <para>if <paramref name="count"/> is such that the segment's end falls outside the bounds of <paramref name="buffer"/>.</para> /// </exception> /// <returns>an integer constructed by reading bytes in little-endian byte order.</returns> private static long FromBytes(byte[] buffer, int offset, int count) { Guard.NotNull(() => buffer, buffer); if (offset < 0) { throw new ArgumentOutOfRangeException(nameof(offset), offset, CommonStrings.OffsetMustBeNonNegative); } if (offset > buffer.Length || offset + count > buffer.Length) { throw ArraySegmentException.GetByStartAndLength(offset, count); } long result = 0; int end = offset + count; for (int position = end - 1; position >= offset; position--) { result = unchecked ((result << 8) | buffer[position]); } return(result); }