+ internal async Task SaveAsync(TextWriter fs,CancellationTokenSource tokenSource=null)
+ {
+ try
+ {
+ this.rwlock.EnterWriteLock();
+ StringBuilder line = new StringBuilder();
+ for (int i = 0; i < this.Length; i++)
+ {
+ char c = this[i];
+ line.Append(c);
+ if (c == Document.NewLine || i == this.Length - 1)
+ {
+ string str = line.ToString();
+ str = str.Replace(Document.NewLine.ToString(), fs.NewLine);
+ await fs.WriteAsync(str).ConfigureAwait(false);
+ line.Clear();
+ if (tokenSource != null)
+ tokenSource.Token.ThrowIfCancellationRequested();
+#if TEST_ASYNC
+ System.Threading.Thread.Sleep(10);
+#endif
+ }
+ }
+ }
+ finally
+ {
+ this.rwlock.ExitWriteLock();
+ }
+ }
+