Beispiel #1
0
        /// <summary>
        /// Read bytes from <paramref name="str"/>.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static LineResourceBytes ReadFully(LineResourceStream str)
        {
            Stream s = str.Value;

            if (s == null)
            {
                return(new LineResourceBytes(str.Line, str.Exception, str.Status.Up_(LineStatus.ResourceFailedNull)));
            }

            try
            {
                // Get length
                long length;
                try
                {
                    length = s.Length;
                }
                catch (NotSupportedException)
                {
                    // Cannot get length
                    MemoryStream ms = new MemoryStream();
                    s.CopyTo(ms);
                    return(ms.ToArray());
                }

                if (length > int.MaxValue)
                {
                    return(new LineResourceBytes(str.Line, str.Exception, str.Status.Up_(LineStatus.ResourceFailed2GBLimit)));
                }

                int    _len = (int)length;
                byte[] data = new byte[_len];

                // Read chunks
                int ix = 0;
                while (ix < _len)
                {
                    int count = s.Read(data, ix, _len - ix);

                    // "returns zero (0) if the end of the stream has been reached."
                    if (count == 0)
                    {
                        break;
                    }

                    ix += count;
                }
                if (ix == _len)
                {
                    return(data);
                }

                // Failed to read stream fully
                return(new LineResourceBytes(str.Line, str.Exception, str.Status.Up_(LineStatus.ResourceFailedConversionError)));
            }
            catch (IOException e)
            {
                return(new LineResourceBytes(str.Line, e, str.Status.Up_(LineStatus.ResourceFailedException)));
            }
        }
 /// <summary>
 /// Log <paramref name="str"/>, if loggers are configured.
 /// </summary>
 /// <param name="str"></param>
 public void LogResolveStream(LineResourceStream str)
 {
     for (int i = 0; i < Loggers.Count; i++)
     {
         if (Loggers[i] is IObserver <LineResourceStream> lineLogger)
         {
             lineLogger.OnNext(str);
         }
     }
 }
 /// <summary>
 /// Log error and string, if loggers are configured.
 /// </summary>
 /// <param name="e"></param>
 /// <param name="stream"></param>
 public void LogResolveStream(Exception e, LineResourceStream stream)
 {
     for (int i = 0; i < Loggers.Count; i++)
     {
         if (Loggers[i] is IObserver <LineResourceStream> logger)
         {
             logger.OnNext(stream);
             logger.OnError(e);
         }
     }
 }