Ejemplo n.º 1
0
        public void TestMultiStreamWriter()
        {
            string fileName = Path.GetTempFileName() + "q";

            using (FileStream fs = File.Create(fileName))
                using (MultipleStreamWriter writer = new MultipleStreamWriter(fs))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        using (Stream s = writer.CreateStream())
                        {
                            using (StreamWriter sw = new StreamWriter(s))
                            {
                                sw.WriteLine("This is stream {0}", i);
                            }
                        }
                    }

                    using (MultipleStreamWriter subWriter = new MultipleStreamWriter(writer.CreateStream()))
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            using (Stream s = subWriter.CreateStream())
                            {
                                using (StreamWriter sw = new StreamWriter(s))
                                {
                                    sw.WriteLine("This is stream x-{0}", fileName);
                                }
                            }
                        }
                    }
                }

            using (FileStream fs = File.OpenRead(fileName))
                using (MultipleStreamReader reader = new MultipleStreamReader(fs))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        using (Stream s = reader.GetNextStream())
                        {
                            using (StreamReader sw = new StreamReader(s))
                            {
                                string line     = sw.ReadLine();
                                string shouldBe = string.Format("This is stream {0}", i);

                                Assert.That(line, Is.EqualTo(shouldBe));
                            }
                        }
                    }
                }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TPack"/> class.
        /// </summary>
        /// <param name="baseStream">The base stream.</param>
        /// <param name="verificationMode">The verification mode.</param>
        /// <param name="disposeAtClose">A list of objects to dispose in this order when the stream is closed</param>
        internal TPack(Stream baseStream, VerificationMode verificationMode, IDisposable[] disposeAtClose)
        {
            if (baseStream == null)
            {
                throw new ArgumentNullException("baseStream");
            }

            _assuredStream = new AssuredStream(baseStream, verificationMode);

            _disposeAtClose = new List <IDisposable>();
            _disposeAtClose.Add(_assuredStream);
            _disposeAtClose.Add(baseStream);
            if (disposeAtClose != null)
            {
                _disposeAtClose.AddRange(disposeAtClose);
            }

            MultipleStreamCreateArgs msa = new MultipleStreamCreateArgs();

            msa.VerificationMode = VerificationMode.Full;

            _reader = new MultipleStreamReader(_assuredStream, msa);

            Pack pack;

            using (Stream r = _reader.GetNextStream(PackageDefinitionId))
            {
                XPathDocument  doc = new XPathDocument(r);
                XPathNavigator nav = doc.CreateNavigator();
                nav.MoveToRoot();
                nav.MoveToFirstChild();

                TokenizerArgs ta = new TokenizerArgs();
                ta.SkipUnknownNamedItems = true;

                if (!Tokenizer.TryParseXml(nav, out pack) || pack == null)
                {
                    throw new IOException();
                }
                else
                {
                    _pack = pack;
                }
            }
        }