A class that represents a set of Stream based functions.
Example #1
0
 /// <summary>
 ///     Writes the context body from the remote response.
 /// </summary>
 /// <param name="incomingHttpContext">The incoming HTTP context.</param>
 /// <param name="remoteWebResponse">The remote web response.</param>
 private async Task WriteContextBodyFromRemoteResponse(IStumpsHttpContext incomingHttpContext, HttpWebResponse remoteWebResponse)
 {
     if (remoteWebResponse.ContentLength != 0)
     {
         var responseStream = remoteWebResponse.GetResponseStream();
         incomingHttpContext.Response.ClearBody();
         incomingHttpContext.Response.AppendToBody(await StreamUtility.ConvertStreamToByteArray(responseStream));
     }
 }
        public async Task ConvertStreamToByteArray_ValidStream_ReturnsArray()
        {
            var buffer = CreateByteArray(100);

            using (var ms = new MemoryStream(buffer))
            {
                var newBuffer = await StreamUtility.ConvertStreamToByteArray(ms);

                CollectionAssert.AreEqual(buffer, newBuffer);
            }
        }
        public void CopyStream_NullOutputStream_ThrowsException()
        {
            var ex = Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                using (var ms = new MemoryStream())
                {
                    await StreamUtility.CopyStream(ms, null);
                }
            });

            Assert.That(ex.ParamName.Equals("outputStream", StringComparison.Ordinal));
        }
        public void CopyStreamWithStartingPosition_NullInputStream_ThrowsException()
        {
            var ex = Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                using (var ms = new MemoryStream())
                {
                    await StreamUtility.CopyStream(null, ms, 5);
                }
            });

            Assert.That(ex.ParamName.Equals("inputStream", StringComparison.Ordinal));
        }
        public void WriteUtf8StringToStream_NullValue_ThrowsException()
        {
            var ex = Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                using (var ms = new MemoryStream())
                {
                    await StreamUtility.WriteUtf8StringToStream(null, ms);
                }
            });

            Assert.That(ex.ParamName.Equals("value", StringComparison.Ordinal));
        }
        public async Task CopyStreamWithStartingPosition_ValidStreamsAndStartingPosition_CopiesPartiallyAndResetStart()
        {
            var buffer = CreateByteArray(100);

            using (var source = new MemoryStream(buffer))
            {
                using (var output = new MemoryStream())
                {
                    await StreamUtility.CopyStream(source, output, 5);

                    Assert.AreEqual(95, output.Length);
                    Assert.AreEqual(5, source.Position);
                }
            }
        }
        public async Task CopyStream_ValidStreams_CopiesStream()
        {
            var buffer = CreateByteArray(100);

            using (var source = new MemoryStream(buffer))
            {
                source.Position = 0;

                using (var output = new MemoryStream())
                {
                    await StreamUtility.CopyStream(source, output);

                    Assert.AreEqual(100, output.Length);
                }
            }
        }
        public async Task WriteUtfStringToStream_ValidString_WritesToStream()
        {
            var tempFolder = CreateTempFolder();
            var file       = Path.GetRandomFileName();
            var path       = Path.Combine(tempFolder, file);

            try
            {
                using (var stream = File.OpenWrite(path))
                {
                    await StreamUtility.WriteUtf8StringToStream("HelloWorld", stream);
                }

                var fi = new FileInfo(path);
                Assert.AreEqual(13, fi.Length);
            }
            finally
            {
                if (Directory.Exists(tempFolder))
                {
                    DeleteTempFolder(tempFolder);
                }
            }
        }
Example #9
0
 /// <summary>
 /// Copies data from an input stream to an output stream.
 /// </summary>
 /// <param name="inputStream">The input stream.</param>
 /// <param name="outputStream">The output stream.</param>
 public static async Task CopyStream(Stream inputStream, Stream outputStream)
 {
     await StreamUtility.CopyStream(inputStream, outputStream, -1);
 }
Example #10
0
        public void WriteUtf8StringToStream_NullStream_ThrowsException()
        {
            var ex = Assert.ThrowsAsync <ArgumentNullException>(async() => await StreamUtility.WriteUtf8StringToStream("ABCD", null));

            Assert.That(ex.ParamName.Equals("stream", StringComparison.Ordinal));
        }
Example #11
0
        public void ConvertStreamToByteArray_NullValue_ThrowsException()
        {
            var ex = Assert.ThrowsAsync <ArgumentNullException>(async() => await StreamUtility.ConvertStreamToByteArray(null));

            Assert.That(ex.ParamName.Equals("stream", StringComparison.OrdinalIgnoreCase));
        }