public int ReadBytes()

in src/NMS.AMQP/Message/NmsStreamMessage.cs [93:145]


        public int ReadBytes(byte[] value)
        {
            CheckWriteOnlyBody();

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value), "Target byte array was null");
            }

            if (remainingBytes == NO_BYTES_IN_FLIGHT)
            {
                object data = facade.Peek();
                if (data == null)
                {
                    return -1;
                }
                else if (!(data is byte[]))
                {
                    throw new MessageFormatException("Next stream value is not a byte array.");
                }
                bytes = data as byte[];
                remainingBytes = bytes.Length;
            }
            else if (remainingBytes == 0)
            {
                // We previously read all the bytes, but must have filled the destination array.
                remainingBytes = NO_BYTES_IN_FLIGHT;
                bytes = null;
                facade.Pop();
                return -1;
            }

            int previouslyRead = bytes.Length - remainingBytes;
            int lengthToCopy = Math.Min(value.Length, remainingBytes);

            if (lengthToCopy > 0)
            {
                Array.Copy(bytes, previouslyRead, value, 0, lengthToCopy);
            }

            remainingBytes -= lengthToCopy;

            if (remainingBytes == 0 && lengthToCopy < value.Length)
            {
                // All bytes have been read and the destination array was not filled on this
                // call, so the return will enable the caller to determine completion immediately.
                remainingBytes = NO_BYTES_IN_FLIGHT;
                bytes = null;
                facade.Pop();
            }

            return lengthToCopy;
        }