BzipStream
BzipStream is a small .NET wrapper for the Bzip2 compression utility. Bz2 compression is similar to Gzip where only one file/chunk/blob is compressed, unlike zip, rar, ace, etc. Its normally used to compress a tarball (.tar file).
Below is the implementation diagram:
public class BzipStream : Stream
{
public BzipStream(string filename, FileAccess mode);
public override void Close();
public static byte[] Compress(byte[] buffer);
public static byte[] Compress(byte[] buffer, int level);
public static byte[] Decompress(byte[] buffer);
public override void Flush();
public static BzipStream OpenRead(string filename);
public static BzipStream OpenWrite(string filename);
public override int Read(byte[] buffer, int start, int length);
public override long Seek(long pos, SeekOrigin origin);
public override void SetLength(long len);
public override void Write(byte[] buffer, int start, int length);
public override bool CanRead { get; }
public override bool CanSeek { get; }
public override bool CanWrite { get; }
public override long Length { get; }
public override long Position { get; set; }
}
The De/Compress methods are utility methods to quickly de/compress a byte[]. The following methods are implemented but not supported:
Also FileAccess.ReadWrite
is NOT supported.
Usage (C#)
BzipStream bz = new BzipStream("file.bz2", FileAccess.Write);
Stream s = File.OpenRead("file");
int len = 8192;
byte[] buffer = new byte[len];
while ((len = s.Read(buffer, 0, len)) > 0)
{
bz.Write(buffer, 0, len);
}
s.Close();
bz.Close();
byte[] cbuff = BzipStream.Compress(buffer);
buffer = BzipStream.Decompress(cbuff);
Implementation
libbz2 is compiled to vanilla C, and a single file (dotbz2.cpp) carries the MC++ implementation (and ONLY that file has "managed" compile flags set). Many people have been asking how to do this. Very easy. Just look.
The basic pattern is:
- Get .NET object
- Allocate memory on the heap
- Copy object and get an
IntPtr
- Cast the
IntPtr
to void*
- Cast to
<T>*
- Do unmanaged function
- Cast result to
void*
- Cast to
IntPtr
- Copy from heap to object
- Free heap memory
Steps 7 and 8 are not really necessary as we have a reference to the IntPtr
already.
Conclusion
Just a small example of how to wrap a C library in MC++. Suggestions welcome. I could paste the code, its only 185 lines. Don't flame me for the length of the article. :)