C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
This data may be part of a data file, image file, compressed file, downloaded server response, or many other files.
With byte arrays, we have an ideal representation of this data. The byte array type allows you to store low-level representations. It is useful when optimizing certain methods.
This program shows how to allocate a byte array on the managed heap. The program also measures the memory usage of the managed heap before and after this allocation occurs.
Also: The program demonstrates that a byte array is a precise representation of bytes, not a higher-level construct.
Based on: .NET 4.5 C# program that allocates byte array using System; class Program { static void Main() { byte[] array1 = null; // // Allocate three million bytes and measure memory usage. // long bytes1 = GC.GetTotalMemory(false); array1 = new byte[1000 * 1000 * 3]; array1[0] = 0; long bytes2 = GC.GetTotalMemory(false); Console.WriteLine(bytes2 - bytes1); } } Output 3000032
Memory usage. The GC.GetTotalMemory method first gathers the memory usage figures from the system before and after the allocation of the byte array.
Then: The byte array variable is assigned a reference to three million bytes in an array allocated on the managed heap.
Allocating a three million element array of bytes causes three million bytes to be added to the memory usage of the program. Byte has minimal overhead.
Read bytes. We read in a byte array from a file with the System.IO namespace. An easy way to read a file into a binary array is the File.ReadAllBytes method in the File class.
C# program that uses File.ReadAllBytes using System; using System.IO; class Program { static void Main() { byte[] array = File.ReadAllBytes("C:\\profiles\\file.bin"); Console.WriteLine(array[0]); Console.WriteLine(array[1]); Console.WriteLine(array[2]); } } Output 56 66 80
Seek, read. We use Seek() on some Stream classes to read a part of a file into a byte array. It is faster to read in an entire byte array at once. This uses the file system's buffering.
ASP.NET uses byte arrays in its internal code. You can use the BinaryWrite method and provide a byte array parameter to achieve a fast write to the HTTP response.
Note: Some websites use this approach to optimize server-side performance. Byte arrays are used frequently in this approach.
Modify. It is also possible to modify a byte array. We show how to rewrite a binary file encoded in GZIP to remove parts of the binary format, resulting in superior data compression.
Test elements. We can test bytes. With a GZIP file in an array, we can test the first two elements and the length. This tells us if the file contains the correct magic GZIP header bytes.
Note: This process is also called "data sniffing" and is used constantly on the Internet and in web browsers.
Download. You can download data over the Internet or other network with a network connection with WebClient. We directly access the bytes of any server's response.
Tip: The data it returns is stored in a byte array. This is useful for scraping web sites or automating tedious Internet tasks.
GZipStream allows you to use a byte array buffer for expanding or compressing GZIP-encoded data. This is an easy way to implement compression or decompression for web applications.
File system caching. With byte arrays, you are usually just storing the exact data from the file system in the memory of your program.
However: In some cases, this actually duplicates the Windows file system cache mechanism that is optimized for this exact purpose.
Note: Byte array caches on the file system can improve or degrade performance in various situations.
BinaryReader. This class allows us to imperatively read in a binary file with more helper methods than testing the bytes yourself.
Class: The BinaryReader class allows you to easily transform individual bytes into integers or strings.
A summary. We looked at the byte array type, seeing its memory allocation statistics. Next we provided pointers to other articles that focus on the byte array type.