开发者

How to read a binary file into an array of bytes?

I have a binary file which I am reading to a collection of byte arra开发者_StackOverflowys.

The file contains multiple (arbitrary number) of records. Essentially a block of bytes. Each record is of arbitrary length.

The header of the file provides the offsets of each of the records.

record 0: offset 2892
record 1: offset 4849
....
record 98: offset 328932
record 99: offset 338498

I have written code to do loop and read in each record to it's byte array. Looking at difference in offsets gives me the record size. A seek to the offset and then a call to ReadBytes() reads the record into its array.

My current incomplete solution won't work for the last record. How would you read that last record into an array (remember it is of arbitrary length).

As for why? Each record is encrypted and needs to be decrypted separately. I am writing code which will read in each record into a byte array. Decrypt it and then write all the record back to a file.

Code added at request:

    //recordOffsets contain byte location of each record start.  All headers (other than universal header) are contained within record 0.
    recordBlocks = new List<RecordBlock>();

    //store all recordOffsets.  Record0 offset will be used to load rest of headers.  Remaining are used to parse text of eBook.
    for (int i = 0; i < standardHeader.numRecs; i++)
    {
        RecordBlock r = new RecordBlock();
        r.offset = bookReader.ReadInt32(EndianReader.Endian.BigEndian);
        r.number = bookReader.ReadInt32(EndianReader.Endian.BigEndian);
        recordBlocks.Add(r);
    }

    foreach (RecordBlock r in recordBlocks)
    {
        if (r.number  == recordBlocks.Count)
        {
            ///deal with last record
        }
        else
        {
            r.size = recordBlocks[(r.number) + 1].offset - r.offset;   
        }
        bookReader.Seek(r.offset, SeekOrigin.Begin);
        r.data = bookReader.ReadBytes(r.size);            
    }


System.IO.File.ReadAllBytes() will read all bytes in Byte Array and after that you can read from that byte array record by record.


You could use the Length property from the FileInfo Class to determine the total number of bytes, so that you can calculate the amount of bytes of the last record as well.
So you can keep most of your current logic.


your problem which seems to me is how will you get the actual record size of the last record.

Either you may add this information in the header explicitly then your code will work as charm in my thoughts.


I'm not a .net guy, but it would seem you have a couple options. There's got to be a way to tell the size of the file. if you can find that, you can read everything. alternatively, the msdn description for binaryreader.readbytes() says thT if you ask for mroe than the stream contains you'll get whatever's in the file. do you know the max size of the blob you're reading? if so, just read that into pre-cleared memory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜