Home > OS >  Reading using DeflateStream doesn't match expected size
Reading using DeflateStream doesn't match expected size

Time:11-21

I'm writing a set of discrete binary data to a stream and then to disk. I'm using a buffered file stream to reduce the disk usage.

BinaryWriter -> DeflateStream -> FileStream (buffered)

The data set consists of a header part (with some info) and some raw data, compressed;

1. Signature, 1 byte.
2. Timestamp, 8 bytes.
3. Size of data uncompressed, 8 bytes
4. Data (compressed, using DeflateStream), X bytes

The issue is that when reading the data, doing the inverse operation, the position on the stream doesn't match the expected value.

1. Read signature, 1 byte.
2. Read timestamp, 8 bytes (long).
3. Read data size, 8 bytes (long).
4. Read compressed data, using DeflateStream, (above value) bytes.

This of course breaks reading of all the other items. For a data of size 240_000, reading it results in reading more than that. Since I'm writing the data size right before the raw data, the operation to read the size back is working.

The issue is with the DeflateStream or maybe how I'm using it.

Writer

var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, 200 * 1_048_576);
var binaryWriter = new BinaryWriter(fileStream);

var item = new Item
{
    Signature = (byte)1,
    TimeStamp = DateTime.Now.Ticks,
    Data = new byte[] { .... }
}

binaryWriter.Write(item.Signature); //1 byte.
binaryWriter.Write(item.TimeStamp); //8 bytes.
binaryWriter.Write(item.Data.LongLength); //8 bytes.

//Reported position: 17 (1   8   8)
//Data Length: 240_000

using (var compressStream = new DeflateStream(fileStream, CompressionLevel.Optimal, true))
{
    compressStream.Write(item.Data);
    compressStream.Flush();
}

//Reported position: 8099 (1   8   8   [compressed length])

//           
  • Related