< Summary

Information
Class: Renci.SshNet.Common.SshDataStream
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Common\SshDataStream.cs
Line coverage
76%
Covered lines: 68
Uncovered lines: 21
Coverable lines: 89
Total lines: 268
Line coverage: 76.4%
Branch coverage
57%
Covered branches: 8
Total branches: 14
Branch coverage: 57.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
.ctor(...)100%1100%
.ctor(...)100%1100%
get_IsEndOfData()100%1100%
Write(...)100%1100%
Write(...)100%1100%
Write(...)100%10%
Write(...)50%266.66%
Write(...)50%271.42%
ReadBinary()50%271.42%
WriteBinary(...)50%266.66%
WriteBinary(...)100%1100%
ReadBigInt()100%10%
ReadUInt32()100%1100%
ReadUInt64()100%1100%
ReadString(...)50%275%
ToArray()100%2100%
ReadBytes(...)50%275%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Common\SshDataStream.cs

#LineLine coverage
 1using System;
 2using System.Globalization;
 3using System.IO;
 4using System.Text;
 5
 6namespace Renci.SshNet.Common
 7{
 8    /// <summary>
 9    /// Specialized <see cref="MemoryStream"/> for reading and writing data SSH data.
 10    /// </summary>
 11    public class SshDataStream : MemoryStream
 12    {
 13        /// <summary>
 14        /// Initializes a new instance of the <see cref="SshDataStream"/> class with an expandable capacity initialized
 15        /// as specified.
 16        /// </summary>
 17        /// <param name="capacity">The initial size of the internal array in bytes.</param>
 18        public SshDataStream(int capacity)
 11806919            : base(capacity)
 11806920        {
 11806921        }
 22
 23        /// <summary>
 24        /// Initializes a new instance of the <see cref="SshDataStream"/> class for the specified byte array.
 25        /// </summary>
 26        /// <param name="buffer">The array of unsigned bytes from which to create the current stream.</param>
 27        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
 28        public SshDataStream(byte[] buffer)
 9629            : base(buffer)
 9630        {
 9631        }
 32
 33        /// <summary>
 34        /// Initializes a new instance of the <see cref="SshDataStream"/> class for the specified byte array.
 35        /// </summary>
 36        /// <param name="buffer">The array of unsigned bytes from which to create the current stream.</param>
 37        /// <param name="offset">The zero-based offset in <paramref name="buffer"/> at which to begin reading SSH data.<
 38        /// <param name="count">The number of bytes to load.</param>
 39        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
 40        public SshDataStream(byte[] buffer, int offset, int count)
 9471941            : base(buffer, offset, count)
 9471942        {
 9471943        }
 44
 45        /// <summary>
 46        /// Gets a value indicating whether all data from the SSH data stream has been read.
 47        /// </summary>
 48        /// <value>
 49        /// <see langword="true"/> if this instance is end of data; otherwise, <see langword="false"/>.
 50        /// </value>
 51        public bool IsEndOfData
 52        {
 53            get
 9654            {
 9655                return Position >= Length;
 9656            }
 57        }
 58
 59        /// <summary>
 60        /// Writes an <see cref="uint"/> to the SSH data stream.
 61        /// </summary>
 62        /// <param name="value"><see cref="uint"/> data to write.</param>
 63        public void Write(uint value)
 36870464        {
 36870465            var bytes = Pack.UInt32ToBigEndian(value);
 36870466            Write(bytes, 0, bytes.Length);
 36870467        }
 68
 69        /// <summary>
 70        /// Writes an <see cref="ulong"/> to the SSH data stream.
 71        /// </summary>
 72        /// <param name="value"><see cref="ulong"/> data to write.</param>
 73        public void Write(ulong value)
 818274        {
 818275            var bytes = Pack.UInt64ToBigEndian(value);
 818276            Write(bytes, 0, bytes.Length);
 818277        }
 78
 79        /// <summary>
 80        /// Writes a <see cref="BigInteger"/> into the SSH data stream.
 81        /// </summary>
 82        /// <param name="data">The <see cref="BigInteger" /> to write.</param>
 83        public void Write(BigInteger data)
 084        {
 085            var bytes = data.ToByteArray().Reverse();
 086            WriteBinary(bytes, 0, bytes.Length);
 087        }
 88
 89        /// <summary>
 90        /// Writes bytes array data into the SSH data stream.
 91        /// </summary>
 92        /// <param name="data">Byte array data to write.</param>
 93        /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
 94        public void Write(byte[] data)
 3264295        {
 3264296            if (data is null)
 097            {
 098                throw new ArgumentNullException(nameof(data));
 99            }
 100
 32642101            Write(data, 0, data.Length);
 32642102        }
 103
 104        /// <summary>
 105        /// Writes string data to the SSH data stream using the specified encoding.
 106        /// </summary>
 107        /// <param name="s">The string data to write.</param>
 108        /// <param name="encoding">The character encoding to use.</param>
 109        /// <exception cref="ArgumentNullException"><paramref name="s"/> is <see langword="null"/>.</exception>
 110        /// <exception cref="ArgumentNullException"><paramref name="encoding"/> is <see langword="null"/>.</exception>
 111        public void Write(string s, Encoding encoding)
 42208112        {
 42208113            if (encoding is null)
 0114            {
 0115                throw new ArgumentNullException(nameof(encoding));
 116            }
 117
 42208118            var bytes = encoding.GetBytes(s);
 42208119            WriteBinary(bytes, 0, bytes.Length);
 42208120        }
 121
 122        /// <summary>
 123        /// Reads a byte array from the SSH data stream.
 124        /// </summary>
 125        /// <returns>
 126        /// The byte array read from the SSH data stream.
 127        /// </returns>
 128        public byte[] ReadBinary()
 57951129        {
 57951130            var length = ReadUInt32();
 131
 57951132            if (length > int.MaxValue)
 0133            {
 0134                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not s
 135            }
 136
 57951137            return ReadBytes((int)length);
 57951138        }
 139
 140        /// <summary>
 141        /// Writes a buffer preceded by its length into the SSH data stream.
 142        /// </summary>
 143        /// <param name="buffer">The data to write.</param>
 144        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
 145        public void WriteBinary(byte[] buffer)
 97106146        {
 97106147            if (buffer is null)
 0148            {
 0149                throw new ArgumentNullException(nameof(buffer));
 150            }
 151
 97106152            WriteBinary(buffer, 0, buffer.Length);
 97106153        }
 154
 155        /// <summary>
 156        /// Writes a buffer preceded by its length into the SSH data stream.
 157        /// </summary>
 158        /// <param name="buffer">An array of bytes. This method write <paramref name="count"/> bytes from buffer to the 
 159        /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin writing bytes
 160        /// <param name="count">The number of bytes to be written to the current SSH data stream.</param>
 161        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
 162        /// <exception cref="ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/> is gre
 163        /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negat
 164        public void WriteBinary(byte[] buffer, int offset, int count)
 180194165        {
 180194166            Write((uint) count);
 180194167            Write(buffer, offset, count);
 180194168        }
 169
 170        /// <summary>
 171        /// Reads a <see cref="BigInteger"/> from the SSH datastream.
 172        /// </summary>
 173        /// <returns>
 174        /// The <see cref="BigInteger"/> read from the SSH data stream.
 175        /// </returns>
 176        public BigInteger ReadBigInt()
 0177        {
 0178            var length = ReadUInt32();
 0179            var data = ReadBytes((int) length);
 0180            return new BigInteger(data.Reverse());
 0181        }
 182
 183        /// <summary>
 184        /// Reads the next <see cref="uint"/> data type from the SSH data stream.
 185        /// </summary>
 186        /// <returns>
 187        /// The <see cref="uint"/> read from the SSH data stream.
 188        /// </returns>
 189        public uint ReadUInt32()
 240518190        {
 240518191            var data = ReadBytes(4);
 240518192            return Pack.BigEndianToUInt32(data);
 240518193        }
 194
 195        /// <summary>
 196        /// Reads the next <see cref="ulong"/> data type from the SSH data stream.
 197        /// </summary>
 198        /// <returns>
 199        /// The <see cref="ulong"/> read from the SSH data stream.
 200        /// </returns>
 201        public ulong ReadUInt64()
 10778202        {
 10778203            var data = ReadBytes(8);
 10778204            return Pack.BigEndianToUInt64(data);
 10778205        }
 206
 207        /// <summary>
 208        /// Reads the next <see cref="string"/> data type from the SSH data stream.
 209        /// </summary>
 210        /// <param name="encoding">The character encoding to use.</param>
 211        /// <returns>
 212        /// The <see cref="string"/> read from the SSH data stream.
 213        /// </returns>
 214        public string ReadString(Encoding encoding)
 106920215        {
 106920216            var length = ReadUInt32();
 217
 106920218            if (length > int.MaxValue)
 0219            {
 0220                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is no
 221            }
 222
 106920223            var bytes = ReadBytes((int) length);
 106920224            return encoding.GetString(bytes, 0, bytes.Length);
 106920225        }
 226
 227        /// <summary>
 228        /// Writes the stream contents to a byte array, regardless of the <see cref="MemoryStream.Position"/>.
 229        /// </summary>
 230        /// <returns>
 231        /// This method returns the contents of the <see cref="SshDataStream"/> as a byte array.
 232        /// </returns>
 233        /// <remarks>
 234        /// If the current instance was constructed on a provided byte array, a copy of the section of the array
 235        /// to which this instance has access is returned.
 236        /// </remarks>
 237        public override byte[] ToArray()
 118069238        {
 118069239            if (Capacity == Length)
 113658240            {
 113658241                return GetBuffer();
 242            }
 243
 4411244            return base.ToArray();
 118069245        }
 246
 247        /// <summary>
 248        /// Reads next specified number of bytes data type from internal buffer.
 249        /// </summary>
 250        /// <param name="length">Number of bytes to read.</param>
 251        /// <returns>
 252        /// An array of bytes that was read from the internal buffer.
 253        /// </returns>
 254        /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the internal buffer 
 255        private byte[] ReadBytes(int length)
 416167256        {
 416167257            var data = new byte[length];
 416167258            var bytesRead = Read(data, 0, length);
 259
 416167260            if (bytesRead < length)
 0261            {
 0262                throw new ArgumentOutOfRangeException(nameof(length), string.Format(CultureInfo.InvariantCulture, "The r
 263            }
 264
 416167265            return data;
 416167266        }
 267    }
 268}