public static void XmlBaseWriter_WriteStartEndElementAsync() { string actual; int byteSize = 1024; byte[] bytes = GetByteArray(byteSize); string expect = GetExpectString(bytes, byteSize); using (var ms = new AsyncMemoryStream()) { var writer = XmlDictionaryWriter.CreateTextWriter(ms); writer.WriteStartDocument(); // NOTE: the async method has only one overload that takes 3 params var t1 = writer.WriteStartElementAsync(null, "data", null); t1.Wait(); writer.WriteBase64(bytes, 0, byteSize); var t2 = writer.WriteEndElementAsync(); t2.Wait(); writer.WriteEndDocument(); writer.Flush(); ms.Position = 0; var sr = new StreamReader(ms); actual = sr.ReadToEnd(); } Assert.StrictEqual(expect, actual); }
public static void XmlBaseWriter_WriteStartEndElementAsync() { string actual; int byteSize = 1024; byte[] bytes = GetByteArray(byteSize); string expect = GetExpectString(bytes, byteSize); using (var ms = new AsyncMemoryStream()) { var writer = XmlDictionaryWriter.CreateTextWriter(ms); writer.WriteStartDocument(); // NOTE: the async method has only one overload that takes 3 params var t1 = writer.WriteStartElementAsync(null, "data", null); t1.Wait(); writer.WriteBase64(bytes, 0, byteSize); var t2 = writer.WriteEndElementAsync(); t2.Wait(); writer.WriteEndDocument(); writer.Flush(); ms.Position = 0; var sr = new StreamReader(ms); actual = sr.ReadToEnd(); } Assert.StrictEqual(expect, actual); }
public static void XmlBaseWriter_FlushAsync() { string actual; int byteSize = 1024; byte[] bytes = GetByteArray(byteSize); string expect = GetExpectString(bytes, byteSize); using (var ms = new AsyncMemoryStream()) { var writer = XmlDictionaryWriter.CreateTextWriter(ms); writer.WriteStartDocument(); writer.WriteStartElement("data"); writer.WriteBase64(bytes, 0, byteSize); writer.WriteEndElement(); writer.WriteEndDocument(); var task = writer.FlushAsync(); task.Wait(); ms.Position = 0; var sr = new StreamReader(ms); actual = sr.ReadToEnd(); } Assert.StrictEqual(expect, actual); }
public void SmallCanCompressAndDecompressUsingStreamTransformer() { var str = "This is a string to compress and uncompress"; var data = Encoding.UTF8.GetBytes(str); byte[] compData; using (var cms = new AsyncMemoryStream()) using (var compressed = cms.AddTransformer(_compressor.CompressWriteStream)) { compressed.WriteAsync(data, 0, data.Length, CancellationToken.None).Wait(); compressed.Complete(CancellationToken.None).Wait(); compData = cms.ToArray(); } byte[] decData; using (var reader = new ReadStreamWrapper(new MemoryStream(compData))) using (var decompressed = reader.AddTransformer(_compressor.DecompressReadStream)) { decData = decompressed.ReadBytes().Result; } var decStr = Encoding.UTF8.GetString(decData, 0, decData.Length); Assert.AreEqual(str, decStr); }
public static void XmlBaseWriter_FlushAsync() { string actual = null; int byteSize = 1024; byte[] bytes = GetByteArray(byteSize); string expect = GetExpectString(bytes, byteSize); string lastCompletedOperation = null; try { using (var ms = new AsyncMemoryStream()) { var writer = XmlDictionaryWriter.CreateTextWriter(ms); lastCompletedOperation = "XmlDictionaryWriter.CreateTextWriter()"; writer.WriteStartDocument(); lastCompletedOperation = "writer.WriteStartDocument()"; writer.WriteStartElement("data"); lastCompletedOperation = "writer.WriteStartElement()"; writer.WriteBase64(bytes, 0, byteSize); lastCompletedOperation = "writer.WriteBase64()"; writer.WriteEndElement(); lastCompletedOperation = "writer.WriteEndElement()"; writer.WriteEndDocument(); lastCompletedOperation = "writer.WriteEndDocument()"; var task = writer.FlushAsync(); lastCompletedOperation = "writer.FlushAsync()"; task.Wait(); ms.Position = 0; var sr = new StreamReader(ms); actual = sr.ReadToEnd(); } } catch (Exception e) { var sb = new StringBuilder(); sb.AppendLine($"An error occurred: {e.Message}"); sb.AppendLine(e.StackTrace); sb.AppendLine(); sb.AppendLine($"The last completed operation before the exception was: {lastCompletedOperation}"); Assert.True(false, sb.ToString()); } Assert.Equal(expect, actual); }
public static void XmlBaseWriter_FlushAsync() { string actual = null; int byteSize = 1024; byte[] bytes = GetByteArray(byteSize); string expect = GetExpectString(bytes, byteSize); string lastCompletedOperation = null; try { using (var ms = new AsyncMemoryStream()) { var writer = XmlDictionaryWriter.CreateTextWriter(ms); lastCompletedOperation = "XmlDictionaryWriter.CreateTextWriter()"; writer.WriteStartDocument(); lastCompletedOperation = "writer.WriteStartDocument()"; writer.WriteStartElement("data"); lastCompletedOperation = "writer.WriteStartElement()"; writer.WriteBase64(bytes, 0, byteSize); lastCompletedOperation = "writer.WriteBase64()"; writer.WriteEndElement(); lastCompletedOperation = "writer.WriteEndElement()"; writer.WriteEndDocument(); lastCompletedOperation = "writer.WriteEndDocument()"; var task = writer.FlushAsync(); lastCompletedOperation = "writer.FlushAsync()"; task.Wait(); ms.Position = 0; var sr = new StreamReader(ms); actual = sr.ReadToEnd(); } } catch(Exception e) { var sb = new StringBuilder(); sb.AppendLine($"An error occured: {e.Message}"); sb.AppendLine(e.StackTrace); sb.AppendLine(); sb.AppendLine($"The last completed operation before the exception was: {lastCompletedOperation}"); Assert.True(false, sb.ToString()); } Assert.StrictEqual(expect, actual); }
public static void XmlBaseWriter_WriteBase64Async() { string actual; int byteSize = 1024; byte[] bytes = GetByteArray(byteSize); string expect = GetExpectString(bytes, byteSize); using (var ms = new AsyncMemoryStream()) { var writer = XmlDictionaryWriter.CreateTextWriter(ms); writer.WriteStartDocument(); writer.WriteStartElement("data"); var task = writer.WriteBase64Async(bytes, 0, byteSize); task.Wait(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); ms.Position = 0; var sr = new StreamReader(ms); actual = sr.ReadToEnd(); } Assert.StrictEqual(expect, actual); }
public void LargeCanCompressAndDecompressUsingStreamTransformer() { var data = RandomData(1); byte[] compData; using (var cms = new AsyncMemoryStream()) using (var compressed = cms.AddTransformer(_compressor.CompressWriteStream)) { compressed.WriteAsync(data, 0, data.Length, CancellationToken.None).Wait(); compressed.Complete(CancellationToken.None).Wait(); compData = cms.ToArray(); } byte[] newData; using (var reader = new ReadStreamWrapper(new MemoryStream(compData))) using (var decompressed = reader.AddTransformer(_compressor.DecompressReadStream)) { newData = decompressed.ReadBytes().Result; } Assert.IsTrue(data.SequenceEqual(newData)); }