public ParsedContent Read(Stream stream)
        {
            if (stream.Length - stream.Position <= 0)
            {
                throw new EndOfStreamException();
            }
            var res = new ParsedContent();
            var pos = Channel.ContentPosition;

            if (Channel.ContentHeader == null)
            {
                res.ContentHeader = new Content(pos, new byte[] { });
                var channel_info = new AtomCollection(Channel.ChannelInfo.Extra);
                channel_info.SetChanInfoType("RAW");
                channel_info.SetChanInfoStreamType("application/octet-stream");
                channel_info.SetChanInfoStreamExt("");
                res.ChannelInfo = new ChannelInfo(channel_info);
            }
            res.Contents = new List <Content>();
            while (stream.Length - stream.Position > 0)
            {
                var bytes = new byte[Math.Min(8192, stream.Length - stream.Position)];
                var sz    = stream.Read(bytes, 0, bytes.Length);
                if (sz > 0)
                {
                    Array.Resize(ref bytes, sz);
                    res.Contents.Add(new Content(pos, bytes));
                    pos += sz;
                }
            }
            return(res);
        }
Exemple #2
0
        public async Task ReadAsync(IContentSink sink, Stream stream, CancellationToken cancel_token)
        {
            long pos          = 0;
            var  streamIndex  = Channel.GenerateStreamID();
            var  streamOrigin = DateTime.Now;

            sink.OnContentHeader(new Content(streamIndex, TimeSpan.Zero, pos, new byte[] { }));
            var channel_info = new AtomCollection(Channel.ChannelInfo.Extra);

            channel_info.SetChanInfoType("RAW");
            channel_info.SetChanInfoStreamType("application/octet-stream");
            channel_info.SetChanInfoStreamExt("");
            sink.OnChannelInfo(new ChannelInfo(channel_info));

            bool eof = false;

            do
            {
                var buf = new byte[8192];
                var sz  = await stream.ReadAsync(buf, 0, buf.Length, cancel_token).ConfigureAwait(false);

                if (sz > 0)
                {
                    sink.OnContent(new Content(streamIndex, DateTime.Now - streamOrigin, pos, buf.Take(sz).ToArray()));
                    pos += sz;
                }
                else
                {
                    eof = true;
                }
            } while (!eof);
        }