Beispiel #1
0
        /// <summary>
        /// Reades a byte array from the inbound pipe using one or more packages.
        /// </summary>
        /// <returns>The byte array that is read.</returns>
        protected void Read(out Byte[] data)
        {
            _log.Debug(nameof(Read));

            Byte[] buffer    = new Byte[sizeof(Int32)];
            Int32  byteCount = _inStream.Read(buffer, 0, buffer.Length);

            _log.Debug($"Read {byteCount.Format()} out of expected {buffer.Length.Format()} Bytes.");

            Int32 length = BitConverter.ToInt32(buffer, 0);

            _log.Debug($"Length is {length.Format()}.");

            using (MemoryStream ms = new MemoryStream()) {
                for (Int32 i = 0; i < length; i += UInt16.MaxValue)
                {
                    buffer    = new Byte[Math.Min(length - i, UInt16.MaxValue)];
                    byteCount = _inReader.Read(buffer, 0, buffer.Length);

                    _log.Debug($"Read {byteCount.Format()} out of expected {buffer.Length.Format()} Bytes.");

                    ms.Write(buffer, 0, buffer.Length);
                }

                data = ms.ToArray();
            }
        }