Esempio n. 1
0
		public async Task Synchronize_file_with_appended_data(int size)
		{
			var differenceChunk = new MemoryStream();
			var sw = new StreamWriter(differenceChunk);

			sw.Write("Coconut is Stupid");
			sw.Flush();

			var sourceContent = new CombinedStream(SyncTestUtils.PrepareSourceStream(size), differenceChunk) {Position = 0};
			var destinationContent = SyncTestUtils.PrepareSourceStream(size);
			destinationContent.Position = 0;
			var sourceClient = NewAsyncClient(0);
			var destinationClient = NewAsyncClient(1);

			await destinationClient.UploadAsync("test.txt", destinationContent);
			sourceContent.Position = 0;
			await sourceClient.UploadAsync("test.txt", sourceContent);

			var result = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.txt");

			Assert.Equal(sourceContent.Length, result.BytesCopied + result.BytesTransfered);

			string resultMd5;
			using (var resultFileContent = await destinationClient.DownloadAsync("test.txt"))
			{
				resultMd5 = resultFileContent.GetMD5Hash();
			}

			sourceContent.Position = 0;
			var sourceMd5 = sourceContent.GetMD5Hash();

			Assert.True(resultMd5 == sourceMd5);
		}
Esempio n. 2
0
        public async void Should_mark_file_to_be_resolved_using_current_strategy()
        {
            var differenceChunk = new MemoryStream();
            var sw = new StreamWriter(differenceChunk);

            sw.Write("Coconut is Stupid");
            sw.Flush();

            var sourceContent = SyncTestUtils.PrepareSourceStream(10);

            sourceContent.Position = 0;
            var destinationContent = new CombinedStream(differenceChunk, sourceContent);

            var destinationClient = NewAsyncClient(0);
            var sourceClient      = NewAsyncClient(1);

            var sourceMetadata = new RavenJObject
            {
                { "SomeTest-metadata", "some-value" }
            };
            var destinationMetadata = new RavenJObject
            {
                { "SomeTest-metadata", "shouldnt-be-overwritten" }
            };

            await destinationClient.UploadAsync("test.txt", destinationContent, destinationMetadata);

            sourceContent.Position = 0;
            await sourceClient.UploadAsync("test.txt", sourceContent, sourceMetadata);


            var shouldBeConflict = sourceClient.Synchronization.StartAsync("test.txt", destinationClient).Result;

            Assert.Equal(string.Format("File {0} is conflicted", FileHeader.Canonize("test.txt")), shouldBeConflict.Exception.Message);

            await destinationClient.Synchronization.ResolveConflictAsync("test.txt", ConflictResolutionStrategy.CurrentVersion);

            var result = await destinationClient.Synchronization.StartAsync("test.txt", sourceClient);

            Assert.Equal(destinationContent.Length, result.BytesCopied + result.BytesTransfered);

            // check if conflict resolution has been properly set on the source
            string resultMd5;

            using (var resultFileContent = await sourceClient.DownloadAsync("test.txt"))
            {
                var metadata = await sourceClient.GetMetadataForAsync("test.txt");

                Assert.Equal("shouldnt-be-overwritten", metadata.Value <string>("SomeTest-Metadata"));

                resultMd5 = resultFileContent.GetMD5Hash();
            }

            destinationContent.Position = 0;
            var destinationMd5 = destinationContent.GetMD5Hash();

            sourceContent.Position = 0;

            Assert.True(resultMd5 == destinationMd5);
        }
Esempio n. 3
0
        public void EmptyStream()
        {
            byte[] buffer = new byte[16];

            CombinedStream sut = new CombinedStream();

            Assert.AreEqual(0, sut.Read(buffer, 0, buffer.Length));
        }
Esempio n. 4
0
        public void HappyFlow()
        {
            CheckDisposeStream checkDisposeStream = new CheckDisposeStream();

            CombinedStream sut = new CombinedStream(FixedTestData.CreateStreams(checkDisposeStream));

            FixedTestData.AssertStream(sut, FixedTestData.Structure);

            Assert.IsTrue(checkDisposeStream.DisposeCalled);
        }
Esempio n. 5
0
        public void DisposableBagWorks()
        {
            CheckDisposeStream checkDispose = new CheckDisposeStream();

            CombinedStream sut = new CombinedStream();

            ((IDisposablesBag)sut).AddDisposable(checkDispose);

            sut.Dispose();

            Assert.IsTrue(checkDispose.DisposeCalled);
        }
Esempio n. 6
0
        public async void Synchronize_file_with_different_beginning(int size)
        {
            var differenceChunk = new MemoryStream();
            var sw = new StreamWriter(differenceChunk);

            sw.Write("Coconut is Stupid");
            sw.Flush();

            var sourceContent = SyncTestUtils.PrepareSourceStream(size);

            sourceContent.Position = 0;
            var destinationContent = new CombinedStream(differenceChunk, sourceContent)
            {
                Position = 0
            };
            var sourceClient      = NewClient(0);
            var destinationClient = NewClient(1);
            var sourceMetadata    = new RavenJObject
            {
                { "SomeTest-metadata", "some-value" }
            };
            var destinationMetadata = new RavenJObject
            {
                { "SomeTest-metadata", "should-be-overwritten" }
            };

            await destinationClient.UploadAsync("test.txt", destinationMetadata, destinationContent);

            sourceContent.Position = 0;
            await sourceClient.UploadAsync("test.txt", sourceMetadata, sourceContent);

            var result = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.txt");

            Assert.Equal(sourceContent.Length, result.BytesCopied + result.BytesTransfered);

            string resultMd5;

            using (var resultFileContent = new MemoryStream())
            {
                var metadata = destinationClient.DownloadAsync("test.txt", resultFileContent).Result;

                // REVIEW: (Oren) The xxx-yyy-zzz headers are being transformed to: Xxx-Yyy-Zzz by the underlying implementation of HTTP Client. Is that OK? The old test was able to handle it "case insensitively".
                Assert.Equal("some-value", metadata.Value <string>("SomeTest-Metadata"));
                resultFileContent.Position = 0;
                resultMd5 = resultFileContent.GetMD5Hash();
                resultFileContent.Position = 0;
            }

            sourceContent.Position = 0;
            var sourceMd5 = sourceContent.GetMD5Hash();

            Assert.True(resultMd5 == sourceMd5);
        }
Esempio n. 7
0
        public async Task Synchronize_file_with_different_beginning(int size)
        {
            var differenceChunk = new MemoryStream();
            var sw = new StreamWriter(differenceChunk);

            sw.Write("Coconut is Stupid");
            sw.Flush();

            var sourceContent = SyncTestUtils.PrepareSourceStream(size);

            sourceContent.Position = 0;
            var destinationContent = new CombinedStream(differenceChunk, sourceContent)
            {
                Position = 0
            };
            var sourceClient      = NewAsyncClient(0);
            var destinationClient = NewAsyncClient(1);
            var sourceMetadata    = new RavenJObject
            {
                { "SomeTest-metadata", "some-value" }
            };
            var destinationMetadata = new RavenJObject
            {
                { "SomeTest-metadata", "should-be-overwritten" }
            };

            await destinationClient.UploadAsync("test.txt", destinationContent, destinationMetadata);

            sourceContent.Position = 0;
            await sourceClient.UploadAsync("test.txt", sourceContent, sourceMetadata);

            var result = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.txt");

            Assert.Equal(sourceContent.Length, result.BytesCopied + result.BytesTransfered);

            string resultMd5;

            using (var resultFileContent = await destinationClient.DownloadAsync("test.txt"))
            {
                var metadata = await destinationClient.GetMetadataForAsync("test.txt");

                Assert.Equal("some-value", metadata.Value <string>("SomeTest-Metadata"));
                resultMd5 = resultFileContent.GetMD5Hash();
            }

            sourceContent.Position = 0;
            var sourceMd5 = sourceContent.GetMD5Hash();

            Assert.True(resultMd5 == sourceMd5);
        }
Esempio n. 8
0
        public async Task DisposeAsyncWorks()
        {
            CheckDisposeStream checkDisposeStream = new CheckDisposeStream();
            CheckDisposeStream checkDisposeBag    = new CheckDisposeStream();

            CombinedStream sut = new CombinedStream(checkDisposeStream);

            ((IDisposablesBag)sut).AddDisposable(checkDisposeBag);

            await sut.DisposeAsync();

            Assert.IsTrue(checkDisposeStream.DisposeCalled);
            Assert.IsTrue(checkDisposeBag.DisposeCalled);
        }
Esempio n. 9
0
        public void TestCombinedStreamDisposee()
        {
            DisposableFlag f1 = new DisposableFlag();
            DisposableFlag f2 = new DisposableFlag();

            Assert.IsFalse(f1.Disposed || f2.Disposed);

            Stream ms1 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("Hello"))).WithDisposeOf(f1);
            Stream ms2 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("There"))).WithDisposeOf(f2);

            using (Stream cs = new CombinedStream(ms1, ms2))
            { }

            Assert.IsTrue(f1.Disposed && f2.Disposed);//even though not read, we did dispose?
        }
Esempio n. 10
0
        protected string WriteLine(SshShell _shell, string _line)
        {
            CombinedStream oStream   = (CombinedStream)(_shell.GetStream());
            string         strReturn = "";

            for (int ii = 0; ii < _line.Length; ii++)
            {
                //oStream.Write(_line[ii].ToString());
                _shell.Write(_line[ii].ToString());
                System.Threading.Thread.Sleep(200); // sleep for a split second
                int intRead = oStream.ReadByte();
                strReturn += _shell.GetStream();
            }
            return(strReturn);
        }
Esempio n. 11
0
        public void HappyFlow()
        {
            Stream testData = new CombinedStream(FixedTestData.CreateStreams());

            BatchingPipe <Packet <byte> > packetsPipe = new BatchingPipe <Packet <byte> >();
            Stream sut = packetsPipe.ToWriteOnlyStream();

            Task.Run(async() =>
            {
                await testData.CopyToAsync(sut);
                await sut.DisposeAsync(); // Closing/Disposing adapter triggers completion of producer by default.
            });


            FixedTestData.AssertStream(packetsPipe.ToReadOnlyStream());
            //FixedTestData.DebugStream(packetsPipe.ToReadOnlyStream());
        }
Esempio n. 12
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUsername = "******";
            string strPassword = "******";
            //string strExpects = "sc>|[y/n]|} ok|return to ALOM";
            string   strExpects = "#";
            SshShell oSSHshell  = new SshShell("10.49.254.229", strUsername, strPassword);

            oSSHshell.RemoveTerminalEmulationCharacters = true;
            oSSHshell.Connect();
            CombinedStream oSSHstream = (CombinedStream)(oSSHshell.GetStream());

            if (oSSHshell.Connected == true && oSSHshell.ShellOpened == true)
            {
                //int bt = oSSHstream.ReadByte();
                string strOutput = oSSHshell.Expect(strExpects);
                Response.Write(strOutput);
            }
            oSSHshell.Close();
        }
Esempio n. 13
0
        /// <summary>
        /// Connect a channel to the remote server using the 'SCP From' command ('scp -f')
        /// </summary>
        /// <param name="channel">Will contain the new connected channel</param>
        /// <param name="server">Will contaun the new connected server I/O stream</param>
        /// <param name="rfile">The remote path on the server</param>
        /// <param name="recursive">Idicate a recursive scp transfer</param>
        protected void SCP_ConnectFrom(out Channel channel, out Stream server, string rfile, bool recursive)
        {
            string scpCommand = "scp -f ";

            if (recursive)
            {
                scpCommand += "-r ";
            }
            scpCommand += "\"" + rfile + "\"";

            channel = (ChannelExec)m_session.openChannel(ChannelType);
            ((ChannelExec)channel).setCommand(scpCommand);

            server =
                new CombinedStream
                    (channel.getInputStream(), channel.getOutputStream());
            channel.connect();

            //SCP_CheckAck(server);
        }
Esempio n. 14
0
        public async void Should_reuse_pages_when_data_appended(int numberOfPages)
        {
            string filename = FileHeader.Canonize("test");

            var file = SyncTestUtils.PreparePagesStream(numberOfPages);

            var sourceContent = new CombinedStream(file, SyncTestUtils.PreparePagesStream(numberOfPages));
            // add new pages at the end
            var destinationContent = file;

            sourceContent.Position = 0;
            await source.UploadAsync(filename, sourceContent);

            destinationContent.Position = 0;
            await destination.UploadAsync(filename, destinationContent);

            var contentUpdate = new ContentUpdateWorkItem(filename, "http://localhost:12345", sourceRfs.Storage, sourceRfs.SigGenerator);

            // force to upload entire file, we just want to check which pages will be reused
            await contentUpdate.UploadToAsync(destination.Synchronization);

            await destination.Synchronization.ResolveConflictAsync(filename, ConflictResolutionStrategy.RemoteVersion);

            await contentUpdate.UploadToAsync(destination.Synchronization);


            FileAndPagesInformation fileAndPages = null;

            destinationRfs.Storage.Batch(accessor => fileAndPages = accessor.GetFile(filename, 0, 2 * numberOfPages));

            Assert.Equal(2 * numberOfPages, fileAndPages.Pages.Count);

            for (var i = 0; i < numberOfPages; i++)
            {
                Assert.Equal(i + 1, fileAndPages.Pages[i].Id);
                // if page ids are in the original order it means that they were used the existing pages
            }

            sourceContent.Position = 0;
            Assert.Equal(sourceContent.GetMD5Hash(), destination.GetMetadataForAsync(filename).Result["Content-MD5"]);
        }
Esempio n. 15
0
        public void Synchronize_file_with_different_beginning()
        {
            const int size            = 5000;
            var       differenceChunk = new MemoryStream();
            var       sw = new StreamWriter(differenceChunk);

            sw.Write("Coconut is Stupid");
            sw.Flush();

            var sourceContent = PrepareSourceStream(size);

            sourceContent.Position = 0;
            var seedContent = new CombinedStream(differenceChunk, sourceContent);

            using (var sourceSignatureRepository = CreateSignatureRepositoryFor("test2"))
                using (var seedSignatureRepository = CreateSignatureRepositoryFor("test1"))
                {
                    IList <SignatureInfo> seedSignatureInfos;
                    using (var generator = new SigGenerator())
                    {
                        seedContent.Seek(0, SeekOrigin.Begin);
                        seedSignatureInfos = generator.GenerateSignatures(seedContent, "test1", seedSignatureRepository);
                    }
                    IList <SignatureInfo> sourceSignatureInfos;
                    using (var generator = new SigGenerator())
                    {
                        sourceContent.Seek(0, SeekOrigin.Begin);
                        sourceSignatureInfos = generator.GenerateSignatures(sourceContent, "test2", sourceSignatureRepository);
                    }
                    var sourceSize = sourceContent.Length;

                    using (var tested = new NeedListGenerator(seedSignatureRepository, sourceSignatureRepository))
                    {
                        var result = tested.CreateNeedsList(seedSignatureInfos.Last(), sourceSignatureInfos.Last());
                        Assert.NotNull(result);
                        Assert.Equal(2, result.Count);
                        Assert.Equal(0, sourceSize - result.Sum(x => Convert.ToInt32(x.BlockLength)));
                    }
                }
        }
Esempio n. 16
0
        public void TestCombinedStream()
        {
            DisposableFlag f1 = new DisposableFlag();
            DisposableFlag f2 = new DisposableFlag();

            Assert.IsFalse(f1.Disposed || f2.Disposed);

            Stream ms1 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("Hello"))).WithDisposeOf(f1);
            Stream ms2 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("There"))).WithDisposeOf(f2);

            Assert.AreEqual(ms1.Length, ms2.Length);

            int size = (int)ms1.Length;

            byte[] bytes = new byte[size * 2];

            using (Stream cs = new CombinedStream(ms1, ms2))
            {
                Assert.IsTrue(cs.CanRead);

                Assert.IsFalse(f1.Disposed);
                Assert.AreEqual(size, cs.Read(bytes, 0, size));
                Assert.IsFalse(f1.Disposed);                 //still not disposed util read of 0 bytes
                Assert.AreEqual(1, cs.Read(bytes, size, 1)); //read 1 more byte
                Assert.IsTrue(f1.Disposed);
                //now finish the second one...
                Assert.IsFalse(f2.Disposed);
                Assert.AreEqual(size - 1, cs.Read(bytes, size + 1, size - 1));
                Assert.IsFalse(f2.Disposed);//still not done
                Assert.AreEqual(-1, cs.ReadByte());
                Assert.IsTrue(f2.Disposed);
            }

            Assert.AreEqual("HelloThere", Encoding.ASCII.GetString(bytes));

            //both were disposed
            Assert.IsTrue(f1.Disposed && f2.Disposed);
        }
Esempio n. 17
0
        public void CanReadStream(int requestedCount)
        {
            Random rnd = new Random();

            byte[] valueBytes1 = new byte[1024 * 1024];
            byte[] valueBytes2 = new byte[1024 * 1024];
            rnd.NextBytes(valueBytes1);
            rnd.NextBytes(valueBytes2);
            Stream stream1 = new ChunkMemoryStream(valueBytes1, 1024);
            Stream stream2 = new ChunkMemoryStream(valueBytes2, 1024);

            CombinedStream stream = new CombinedStream(stream1, stream2, inner => stream2 = inner);

            byte[] read  = new byte[2 * 1024 * 1024];
            int    nRead = stream.Read(read, 0, requestedCount);

            Assert.Equal(requestedCount, nRead);

            var upperBound = requestedCount > 1024 * 1024 ? 1024 * 1024 : requestedCount;

            for (int i = 0; i < upperBound; i++)
            {
                Assert.Equal(valueBytes1[i], read[i]);
            }
            if (requestedCount > 1024 * 1024)
            {
                for (int i = 0; i < requestedCount - 1024 * 1024; i++)
                {
                    Assert.Equal(valueBytes2[i], read[i + 1024 * 1024]);
                }
            }
            for (int i = requestedCount; i < 2 * 1024 * 1024; i++)
            {
                Assert.Equal(0, read[i]);
            }
        }
Esempio n. 18
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string strSSH = "";
            string strILO = "10.249.237.148";

            if (Request.QueryString["ilo"] != null)
            {
                strILO = "10.249.237.144";
            }

            Variables oVariable  = new Variables(intEnvironment);
            int       intLogging = 0;

            byte[] byt;
            string str;

            Models           oModel            = new Models(0, dsn);
            ModelsProperties oModelsProperties = new ModelsProperties(0, dsn);
            Servers          oServer           = new Servers(0, dsn);
            Settings         oSetting          = new Settings(0, dsn);
            OnDemand         oOnDemand         = new OnDemand(0, dsn);
            Log      oEventLog = new Log(0, dsn);
            SshShell oSSHshell = new SshShell(strILO, oVariable.SolarisUsername(), oVariable.SolarisPassword());

            oSSHshell.RemoveTerminalEmulationCharacters = true;
            oSSHshell.Connect();
            Response.Write("Connected to " + strILO + "...sending commands..." + "<br/>");
            CombinedStream oSSHstream = (CombinedStream)(oSSHshell.GetStream());

            int intStep = 1;

            if (Request.QueryString["none"] == null)
            {
                byt = new byte[100];
                str = "" + strSSH_Carriage;
                byt = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
                if (oSSHshell.Connected == true && oSSHshell.ShellOpened == true)
                {
                    oSSHstream.Write(byt);
                }
            }

            int  bt = 0;
            int  intMinutePrevious = 0;
            bool boolProcessing    = false;

            while (bt != -1 && oSSHshell.Connected == true && oSSHshell.ShellOpened == true)
            {
                bt = oSSHstream.ReadByte();
                // Strip the processing cursor -\|/-\|/ from the output
                if (bt == 8)    // 8 = backspace
                {
                    // Check to see if previous characters were a processing character as well
                    char chrSSH    = strSSH[strSSH.Length - 1];
                    int  intSymbol = (int)chrSSH;
                    while (IsGarbageChar(intSymbol) == true)
                    {
                        if (intLogging > 1)
                        {
                            Response.Write("The symbol [" + chrSSH.ToString() + "] is a garbage character and must be removed" + "<br/>");
                        }
                        strSSH    = strSSH.Substring(0, strSSH.Length - 1);
                        chrSSH    = strSSH[strSSH.Length - 1];
                        intSymbol = (int)chrSSH;
                    }
                    // Set processing to true to exclude future characters
                    boolProcessing = true;
                }
                if (boolProcessing == true && IsGarbageChar(bt) == false)
                {
                    boolProcessing = false;
                }
                if (boolProcessing == false)
                {
                    strSSH += (char)bt;
                }

                string strReadSSH  = "";
                string strWriteSSH = "";
                switch (intStep)
                {
                case 1:
                    strReadSSH  = "-sc>";
                    strWriteSSH = "poweron";
                    break;

                case 2:
                    strReadSSH  = "-sc>";
                    strWriteSSH = "showpower";
                    break;

                case 3:
                    strReadSSH = "-sc>";
                    break;
                }


                if (strReadSSH != "" && strSSH.EndsWith(strReadSSH) == true)
                {
                    try
                    {
                        Response.Write("SSH output ends with [" + strReadSSH + "] : " + strSSH + "<br/>");
                    }
                    catch { }

                    if (intStep == 3)
                    {
                        break;
                    }
                    // Execute next command
                    byt = new byte[100];
                    str = strWriteSSH + strSSH_Carriage;
                    byt = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
                    if (oSSHshell.Connected == true && oSSHshell.ShellOpened == true)
                    {
                        try
                        {
                            Response.Write("Sending command [" + strWriteSSH + "] : " + strSSH + "<br/>");
                        }
                        catch { }
                        oSSHstream.Write(byt);
                    }
                    intStep++;
                }
                else
                {
                }
            }
            Response.Write(strSSH);
            oSSHstream.Close();
            oSSHshell.Close();
        }
Esempio n. 19
0
        /// <summary>
        /// Given a TextReader, create a new IDomDocument from the input.
        /// </summary>
        ///
        /// <exception cref="InvalidDataException">
        /// Thrown when an invalid data error condition occurs.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Thrown when the requested operation is invalid.
        /// </exception>
        ///
        /// <param name="inputStream">
        /// The HTML input.
        /// </param>
        /// <param name="encoding">
        /// The encoding.
        /// </param>
        ///
        /// <returns>
        /// A populated IDomDocument.
        /// </returns>

        public IDomDocument Parse(Stream inputStream, Encoding encoding)
        {
            ActiveStream   = inputStream;
            ActiveEncoding = encoding;

            // split into two streams so we can restart if needed
            // without having to re-parse the entire stream.

            byte[] part1bytes = new byte[preprocessorBlockBytes];
            int    part1size  = inputStream.Read(part1bytes, 0, preprocessorBlockBytes);

            MemoryStream part1stream = new MemoryStream(part1bytes);

            if (part1stream.Length == 0)
            {
                return(new DomFragment());
            }



            // create a combined stream from the pre-fetched part, and the remainder (whose position
            // will be wherever it was left after reading the part 1 block).

            Stream stream;

            // The official order of precedence for character set processing is as follows:
            //
            // HTTP Content-Type header
            // byte-order mark (BOM)
            // XML declaration
            // meta element
            // link charset attribute
            //
            // http://www.w3.org/International/questions/qa-html-encoding-declarations#precedence
            //
            // Chrome does this:
            //
            // A UTF-16 or UTF-8 BOM overrides the HTTP declaration for Internet Explorer, Safari and Chrome browsers.
            //
            // We act like chrome.


            var bomReader = new BOMReader(part1stream);

            if (bomReader.IsBOM)
            {
                // if there is a BOM encoding, and there's either no active encoding specified already, or it's utf-8/utf-16
                // then use it.

                var bomEncoding = bomReader.Encoding;

                if (ActiveEncoding == null ||
                    (bomReader.Encoding != null &&
                     (bomReader.Encoding.WebName == "utf-8" || bomReader.Encoding.WebName == "utf-16")
                    )
                    )
                {
                    ActiveEncoding = bomReader.Encoding;
                }

                // either way strip the BOM.

                stream = new CombinedStream(bomReader.StreamWithoutBOM, inputStream);
            }
            else
            {
                // no BOM, just reset the input stream

                part1stream.Position = 0;
                stream = new CombinedStream(part1stream, inputStream);
            }

            ActiveStreamReader = new StreamReader(stream, ActiveEncoding ?? Encoding.UTF8, false);

            if (HtmlParsingMode == HtmlParsingMode.Auto ||
                ((HtmlParsingMode == HtmlParsingMode.Fragment) &&
                 String.IsNullOrEmpty(FragmentContext)))
            {
                string ctx;
                ActiveStreamReader = GetContextFromStream(ActiveStreamReader, out ctx);

                if (HtmlParsingMode == HtmlParsingMode.Auto)
                {
                    switch (ctx)
                    {
                    case "document":
                        HtmlParsingMode = HtmlParsingMode.Document;
                        ctx             = "";
                        break;

                    case "html":
                        HtmlParsingMode = HtmlParsingMode.Content;
                        break;

                    default:
                        HtmlParsingMode    = HtmlParsingMode.Fragment;
                        HtmlParsingOptions = HtmlParsingOptions.AllowSelfClosingTags;
                        break;
                    }
                }

                if (HtmlParsingMode == HtmlParsingMode.Fragment)
                {
                    FragmentContext = ctx;
                }
            }

            Reset();

            Tokenize();

            // If the character set was declared within the first block

            if (ReEncode == ReEncodeAction.ReEncode)
            {
                AlreadyReEncoded = true;

                if (ActiveStreamOffset >= preprocessorBlockBytes)
                {
                    // this should never happen, since we test this when accepting an alternate encoding and should
                    // have already decided to change the encoding midstream instead of restart. But as a failsafe
                    // in case there's some part of the parser abort sequence I don't understand, just switch
                    // midstream if we end up here for some reason to keep things going.

                    ActiveStreamReader = new StreamReader(ActiveStream, ActiveEncoding);
                }
                else
                {
                    part1stream = new MemoryStream(part1bytes);

                    // if the 2nd stream has already been closed, then the whole thing is less than the
                    // preprocessor block size; just restart the cached stream..

                    if (inputStream.CanRead)
                    {
                        stream = new CombinedStream(part1stream, inputStream);
                    }
                    else
                    {
                        stream = part1stream;
                    }

                    // assign the re-mapped stream to the source and start again
                    ActiveStreamReader = new StreamReader(stream, ActiveEncoding);
                }

                Reset();
                Tokenize();
            }

            // set this before returning document to the client to improve performance during DOM alteration

            IDomIndexQueue indexQueue = treeBuilder.Document.DocumentIndex as IDomIndexQueue;

            if (indexQueue != null)
            {
                indexQueue.QueueChanges = true;
            }


            return(treeBuilder.Document);
        }