private async Task WriteTextFeed(TextDataFeed feed, Encoding encoding, bool needBom, TdsParserStateObject stateObj, int size) { Debug.Assert(encoding == null || !needBom); char[] inBuff = new char[constTextBufferSize]; encoding = encoding ?? Encoding.Unicode; ConstrainedTextWriter writer = new ConstrainedTextWriter(new StreamWriter(new TdsOutputStream(this, stateObj, null), encoding), size); if (needBom) { if (_asyncWrite) { await writer.WriteAsync((char)TdsEnums.XMLUNICODEBOM).ConfigureAwait(false); } else { writer.Write((char)TdsEnums.XMLUNICODEBOM); } } int nWritten = 0; do { int nRead = 0; if (_asyncWrite) { nRead = await feed._source.ReadBlockAsync(inBuff, 0, constTextBufferSize).ConfigureAwait(false); } else { nRead = feed._source.ReadBlock(inBuff, 0, constTextBufferSize); } if (nRead == 0) { break; } if (_asyncWrite) { await writer.WriteAsync(inBuff, 0, nRead).ConfigureAwait(false); } else { writer.Write(inBuff, 0, nRead); } nWritten += nRead; } while (!writer.IsComplete); if (_asyncWrite) { await writer.FlushAsync().ConfigureAwait(false); } else { writer.Flush(); } }
private async Task WriteXmlFeed(XmlDataFeed feed, TdsParserStateObject stateObj, bool needBom, Encoding encoding, int size) { byte[] preambleToSkip = null; if (!needBom) { preambleToSkip = encoding.GetPreamble(); } ConstrainedTextWriter writer = new ConstrainedTextWriter(new StreamWriter(new TdsOutputStream(this, stateObj, preambleToSkip), encoding), size); XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.CloseOutput = false; // don't close the memory stream writerSettings.ConformanceLevel = ConformanceLevel.Fragment; if (_asyncWrite) { writerSettings.Async = true; } XmlWriter ww = XmlWriter.Create(writer, writerSettings); if (feed._source.ReadState == ReadState.Initial) { feed._source.Read(); } while (!feed._source.EOF && !writer.IsComplete) { // We are copying nodes from a reader to a writer. This will cause the // XmlDeclaration to be emitted despite ConformanceLevel.Fragment above. // Therefore, we filter out the XmlDeclaration while copying. if (feed._source.NodeType == XmlNodeType.XmlDeclaration) { feed._source.Read(); continue; } if (_asyncWrite) { await ww.WriteNodeAsync(feed._source, true).ConfigureAwait(false); } else { ww.WriteNode(feed._source, true); } } if (_asyncWrite) { await ww.FlushAsync().ConfigureAwait(false); } else { ww.Flush(); } }