0

I'm trying to convert a chunk of data from 8-bit packing to 7-bit packing using Bill Koukoutsis' BitStream library from CodeProject.com which still seems to be the go-to source for this kind of thing in C#. The code I'm using is as follows (fs is a filestream initialized outside of the code):

for (int block = 0; block < count; block++)
{
    var theBlock = fetcher(block);
    if (theBlock.Any(x => (x != 0)))
    {
        Console.Write("Good Block =>");
    }
    else
    {
        Console.Write("Zero block =>");
    }
    var sevenBitSize = theBlock.Length - (theBlock.Length/8);
    var theCodedBlock = new byte[sevenBitSize];
    var ms = new MemoryStream(theCodedBlock);
    BitStream bms = ms;
    using (bms)
    {
        foreach (byte t in theBlock)
        {
            bms.Write(t, 0, 7);
        }
        bms.WriteTo(fs);
    }
    if (theCodedBlock.Any(x => (x != 0)))
    {
         Console.Write("Good Block\n");
    }
    else
    {
         Console.Write("Zero block\n");
    }
}

When run, this produces a constant stream of "Good Block => Zero Block". So for some reason the BitStream is outputting zeroes into the theCodedBlock array. It seems to work ok for reading values from a memory array elsewhere in the code, so am I doing something wrong or is this a BitStream bug?

Mark Green
  • 1,310
  • 12
  • 19

1 Answers1

0

Just found the problem:

The BitStream library doesn't properly support writing to the stream it's loaded from; it writes to its internal buffer but doesn't do anything to write it back to the stream. You have to create an empty BitStream, write to that, then convert it back to MemoryStream.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
Mark Green
  • 1,310
  • 12
  • 19