Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I efficiently get a subset of a byte array (first N elements) in C#?

Tags:

c#

.net

I have an byte array buffer of max size 1K. I want to write out a subset of the array (the start of the subset will always be element 0, but the length we're interested in is in a variable).

The application here is compression. I pass in a buffer to a compression function. For simplicity, assume the compression will lead to data that is equal, or less than 1K bytes.

byte[] buffer = new byte[1024];
while (true)
{
    uncompressedData = GetNextUncompressedBlock();
    int compressedLength = compress(buffer, uncompressedData);

    // Here, compressedBuffer[0..compressedLength - 1] is what we're interested in

    // There's a method now with signature Write(byte[] compressedData) that
    // I won't be able to change. Short of allocating a custom sized buffer,
    // and copying data into the custom sized buffer... is there any other
    // technique I could use to only expose the data I want?
}

I'd really like to avoid a copy here -- it seems completely unnecessary as all of the data needed is in buffer already.

like image 414
jglouie Avatar asked May 10 '12 17:05

jglouie


People also ask

What is span T?

Span<T> is a new value type at the heart of . NET. It enables the representation of contiguous regions of arbitrary memory, regardless of whether that memory is associated with a managed object, is provided by native code via interop, or is on the stack.

How do you slice an array in C#?

Using the Copy() Method to Slice Array And then we call the Copy() method which takes a source array, starting index, destination array, starting destination index (zero because we're copying to a new array), and a number of elements we want to slice. As we see, this method works the same as the LINQ one.

What is the default value of byte array in C#?

The default values of numeric array elements are set to zero, and reference elements are set to null. Since byte represents integer values from 0 to 255 , all elements are set to 0 in your authToken array.

What is stored in a byte array?

A byte is 8 bits (binary data). A byte array is an array of bytes (tautology FTW!). You could use a byte array to store a collection of binary data, for example, the contents of a file. The downside to this is that the entire file contents must be loaded into memory.


1 Answers

Buffer.BlockCopy would be my choice.

Microsoft example: http://msdn.microsoft.com/en-us/library/system.buffer.blockcopy.aspx

const int INT_SIZE = 4;
int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
Buffer.BlockCopy(arr, 3 * INT_SIZE, arr, 0 * INT_SIZE, 4 * INT_SIZE);
foreach (int value in arr)
   Console.Write("{0}  ", value);
// The example displays the following output:
//       8  10  12  14  10  12  14  16  18  20 

Your code would look like:

uncompressedData = GetNextUncompressedBlock();      
int compressedLength = compress(buffer, uncompressedData);
Buffer.BlockCopy(buffer, 0, buffer, 0, compressedLength);
like image 129
Chris Gessler Avatar answered Oct 26 '22 23:10

Chris Gessler