Big Daddy
Big Daddy

Reputation: 5234

Read Multiple Byte Arrays from File

How can I read multiple byte arrays from a file? These byte arrays are images and have the potential to be large.

This is how I'm adding them to the file:

 using (var stream = new FileStream(tempFile, FileMode.Append))
      {
        //convertedImage = one byte array
        stream.Write(convertedImage, 0, convertedImage.Length);
      }

So, now they're in tempFile and I don't know how to retrieve them as individual arrays. Ideally, I'd like to get them as an IEnumerable<byte[]>. Is there a way to split these, maybe?

Upvotes: 1

Views: 1855

Answers (3)

Emma Talbert
Emma Talbert

Reputation: 227

To retrieve multiple sets of byte arrays, you will need to know the length when reading. The easiest way to do this (if you can change the writing code) is to add a length value:

using (var stream = new FileStream(tempFile, FileMode.Append))
{
    //convertedImage = one byte array
    // All ints are 4-bytes
    stream.Write(BitConverter.GetBytes(convertedImage.Length), 0, 4);
    // now, we can write the buffer
    stream.Write(convertedImage, 0, convertedImage.Length);
}

Reading the data is then

using (var stream = new FileStream(tempFile, FileMode.Open))
{
    // loop until we can't read any more
    while (true)
    {
        byte[] convertedImage;
        // All ints are 4-bytes
        int size;
        byte[] sizeBytes = new byte[4];
        // Read size
        int numRead = stream.Read(sizeBytes, 0, 4);
        if (numRead <= 0) {
            break;
        }
        // Convert to int
        size = BitConverter.ToInt32(sizeBytes, 0);
        // Allocate the buffer
        convertedImage = new byte[size];
        stream.Read(convertedImage, 0, size);
        // Do what you will with the array
        listOfArrays.Add(convertedImage);
    } // end while
}

If all saved images are the same size, then you can eliminate the first read and write call from each, and hard-code size to the size of the arrays.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726967

Unless you can work out the number of bytes taken by each individual array from the content of these bytes themselves, you need to store the number of images and their individual lengths into the file.

There are many ways to do it: you could write lengths of the individual arrays preceding each byte array, or you could write a "header" describing the rest of the content before writing the "payload" data to the file.

Header may look as follows:

Byte offset    Description
-----------  -------------------
0000...0003 - Number of files, N
0004...000C - Length of file 1
000D...0014 - Length of file 2
...
XXXX...XXXX - Length of file N
XXXX...XXXX - Content of file 1
XXXX...XXXX - Content of file 2
...
XXXX...XXXX - Content of file N

You can use BitConverter methods to produce byte arrays to be written to the header, or you could use BinaryWriter.

Upvotes: 2

Rob
Rob

Reputation: 464

When you read back how do you get the number of bytes per image/byte array to read?

You will need to store the length too (i.e. first 4 bytes = encoded 32 bit int byte count, followed by the data bytes.)

To read back, read the first four bytes, un-encode it back to an int, and then read that number of bytes, repeat until eof.

Upvotes: 1

Related Questions