Incog.Messaging.IncogStream.WriteBytes C# (CSharp) Method

WriteBytes() public method

Write a byte array to the underlying buffer.
public WriteBytes ( byte bytes ) : int
bytes byte The byte array to write to the buffer.
return int
        public int WriteBytes(byte[] bytes)
        {
            // Ensure the bytes parameter is populated
            if (bytes == null) return -1;

            // Encrypt the bytes
            byte[] cipherbytes = this.mycrypt.GetBytes(bytes, Cryptkeeper.Action.Encrypt);

            // Boundary check the length
            if (cipherbytes.Length > ushort.MaxValue)
            {
                string error = string.Format(
                    "The parameter exceeded the length. The actual length is {0} and the maximum length of a byte array is {1}.",
                    cipherbytes.Length.ToString(),
                    ushort.MaxValue.ToString());
                throw new ApplicationException(error);
            }

            // Set the length
            this.WriteUInt16((ushort)cipherbytes.Length);

            // Send the bytes along their merry way
            this.innerStream.Write(cipherbytes, 0, cipherbytes.Length);

            // Add entropy if needed
            if (this.targetEntropy > 0)
            {
                byte[] noise = ShannonEntropy.GetNoise(cipherbytes, this.targetEntropy, 1024);
                this.innerStream.Write(noise, 0, noise.Length);
            }

            this.innerStream.Flush();
            return 2 + cipherbytes.Length;
        }