Bug 29337 - Stream.CopyTo(destination, bufferSize) adds '\0' characters in destination FileStream
Summary: Stream.CopyTo(destination, bufferSize) adds '\0' characters in destination Fi...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 3.12.0
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2015-04-23 10:12 UTC by ceco
Modified: 2015-04-27 16:01 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description ceco 2015-04-23 10:12:50 UTC
* Problem DOES occur when calling Stream.CopyTo(destination, bufferSize) and the destination stream is FileStream created with useAsync = true. Then there are some '\0' characters added in the destination FileStream.

* Problem DOES NOT occur when calling Stream.CopyTo(destination) without specified bufferSize. This is valid with the aforementioned details about the destination FileStream instance.

* Problem DOES NOT occur when using Microsoft.NET on Windows. I don't use Mono on Windows so I cannot provide information for that use case.

Following is an example which demonstrates the problem on:

* Ubuntu 14.04 LTS
* Mono 3.12.1
* MonoDevelop 5.7

using System;
using System.Globalization;
using System.IO;
using System.Text;
using NUnit.Framework;

namespace ConsoleApplication1
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            StringBuilder dataBuilder = new StringBuilder();
            for (int i = 0; i < 10000; ++i)
            {
                dataBuilder.Append(DateTime.Now.ToString(CultureInfo.InvariantCulture));
            }
            string expectedData = dataBuilder.ToString();

            const string fileName = "test";
            const bool useAsync = true;
            const int bufferSize = 8192;

            byte[] bytes = Encoding.Default.GetBytes(expectedData);
            using (Stream dataStream = new MemoryStream(bytes))
            using (Stream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize, useAsync))
            {
                // this works fine
                //dataStream.CopyTo(fileStream);
                // this fails
                dataStream.CopyTo(fileStream, bufferSize);
            }

            string actualData = File.ReadAllText(fileName);
            Assert.That(actualData, Is.EqualTo(expectedData));
        }
    }
}
Comment 1 ceco 2015-04-23 10:14:51 UTC
Forgot to mention:

Problem DOES NOT occur when the destination FileStream is created with useAsync = false.
Comment 2 Marek Safar 2015-04-27 16:01:17 UTC
Fixed in Mono 4.0