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.
Type intro. We look at the basics of the BinaryWrite method in ASP.NET. We explore its performance—and how it uses an abstract method call.
Example. Here we write binary data to the Response in ASP.NET. We call BinaryWrite and OutputStream.Write. The 2 code parts here 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.
Info The Response object is intrinsic in ASP.NET, meaning it can be accessed directly.
using System;
using System.IO;
using System.Web.UI;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Get path of byte file.
string path = Server.MapPath("~/Adobe2.png");
// Get byte array of file.
byte[] byteArray = File.ReadAllBytes(path);
// Write byte array with BinaryWrite.
Response.BinaryWrite(byteArray);
// Write with OutputStream.Write [commented out]// Response.OutputStream.Write(byteArray, 0, byteArray.Length);// Set content type.
Response.ContentType = "image/png";
}
}
Benchmark. Here we compare calling BinaryWrite on Response, to calling Response.OutputStream.Write. OutputStream.Write is an abstract method on the Stream.
Note The performance difference here is not relevant to most websites. And you may need to retest it.
Result Using OutputStream.Write seemed to be faster. In 2022, this should be retested as the data is too old.
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());
}
}Response.OutputStream.Write: 293.60 ms
Response.BinaryWrite: 313.94 ms
Internals. We peek into IL Disassembler and see how BinaryWrite is implemented on Response. You can see that BinaryWrite simply receives the byte[] buffer.
And 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.
A summary. We looked at the BinaryWrite method in ASP.NET. First we saw some basic usage of it, and then tested the performance of the method.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 30, 2022 (edit).