TheDeveloperBlog.com


ASP.NET Response.BinaryWrite

Response.BinaryWrite. BinaryWrite outputs binary data to the Response. It is used to write a byte array—which can represent a file cached in memory. We look at the basics of BinaryWrite. We explore its performance—and how it uses an abstract method call.

BinaryWrite performance test result

Response.OutputStream.Write: 293.60 ms
Response.BinaryWrite:        313.94 ms

Note: The performance difference here is not relevant to most websites. And you may need to retest it.


Example. Here we see the general style of code you can use to write binary data to the Response in ASP.NET. Statements below show BinaryWrite and OutputStream.Write. The two parts do the same thing.

Here: This example writes a PNG image to the Default.aspx page when run. This is rendered in the browser window.

Page that uses BinaryWrite: C#

using System;
using System.IO;
using System.Web.UI;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
	// 1.
	// Get path of byte file.
	string path = Server.MapPath("~/Adobe2.png");

	// 2.
	// Get byte array of file.
	byte[] byteArray = File.ReadAllBytes(path);

	// 3A.
	// Write byte array with BinaryWrite.
	Response.BinaryWrite(byteArray);

	// 3B.
	// Write with OutputStream.Write [commented out]
	// Response.OutputStream.Write(byteArray, 0, byteArray.Length);

	// 4.
	// Set content type.
	Response.ContentType = "image/png";
    }
}

The Response object is intrinsic in ASP.NET, meaning it can be accessed directly. Alternatively, you can use base.Response or HttpContext.Current.Response. There is more research on this site about HttpContext.

HttpContext Request Property

Internals. We peek into IL Disassembler and see how BinaryWrite is implemented on Response. You can see that BinaryWrite simply receives the byte[] buffer. It then calls OutputStream.Write with three parameters based on the buffer.

Info: BinaryWrite is a public instance method. OutputStream.Write is an abstract method.

Public MethodAbstract Keyword
Implementation: IL

callvirt instance void [mscorlib]System.IO.Stream::Write(uint8[], int32, int32)


Benchmark. Here we see a micro-benchmark in ASP.NET that compares calling BinaryWrite on Response, to calling Response.OutputStream.Write. Keep in mind that OutputStream.Write is an abstract method on the Stream.

The micro-benchmark compares 3 million calls on each method, and this is repeated 50 times for a good sample. The debugger was disabled and Release mode was used. The times were 293.6 ms and 313.94 ms.

Page that benchmarks BinaryWrite: C#

using System;
using System.Diagnostics;
using System.Text;
using System.Web;
using System.Web.UI;
using System.IO;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
	byte[] b = new byte[1];
	HttpResponse r = Response;
	StringBuilder builder = new StringBuilder();
	Stopwatch stop = new Stopwatch();
	// Stream output = r.OutputStream;

	for (int i = 0; i < 50; i++) // Also tested with Test 2 first.
	{
	    stop = Stopwatch.StartNew();

	    // Test 1
	    for (int v = 0; v < 3000000; v++)
	    {
		r.OutputStream.Write(b, 0, b.Length);
		// output.Write(b, 0, b.Length); <-- faster for another reason
	    }
	    // End

	    stop.Stop();
	    long ms1 = stop.ElapsedMilliseconds;
	    r.ClearContent();
	    stop = Stopwatch.StartNew();

	    // Test 2
	    for (int v = 0; v < 3000000; v++)
	    {
		r.BinaryWrite(b);
	    }
	    // End

	    stop.Stop();
	    long ms2 = stop.ElapsedMilliseconds;
	    r.ClearContent();
	    builder.Append(ms1).Append("\t").Append(ms2).Append("<br/>");
	}
	r.Write(builder.ToString());
    }
}

I undertook this study because my ASP.NET applications all use BinaryWrite. I have seen other sites that also constantly use it. I felt this article might help many developers make small improvements.


Summary. We looked at the BinaryWrite method in ASP.NET. First we saw some basic usage of it, and then we looked into its implementation. Finally, the performance of the method was tested.

And: We examined the micro-benchmark. We found using OutputStream.Write is more efficient.