Table of Contents

Class BZip2OutputStream

Namespace
Ionic.BZip2
Assembly
SunamoDotNetZip.dll

A write-only decorator stream that compresses data as it is written using the BZip2 algorithm.

public class BZip2OutputStream : Stream, IAsyncDisposable, IDisposable
Inheritance
BZip2OutputStream
Implements
Inherited Members
Extension Methods

Constructors

BZip2OutputStream(Stream)

Constructs a new BZip2OutputStream, that sends its compressed output to the given output stream.

public BZip2OutputStream(Stream output)

Parameters

output Stream

The destination stream, to which compressed output will be sent.

Examples

This example reads a file, then compresses it with bzip2 file, and writes the compressed data into a newly created file.

var fname = "logfile.log";
using (var fs = File.OpenRead(fname))
{
    var outFname = fname + ".bz2";
    using (var output = File.Create(outFname))
    {
        using (var compressor = new BZip2OutputStream(output))
        {
            byte[] buffer = new byte[2048];
            int n;
            while ((n = fs.Read(buffer, 0, buffer.Length)) > 0)
            {
                compressor.Write(buffer, 0, n);
            }
        }
    }
}

BZip2OutputStream(Stream, bool)

Constructs a new BZip2OutputStream.

public BZip2OutputStream(Stream output, bool leaveOpen)

Parameters

output Stream

the destination stream.

leaveOpen bool

whether to leave the captive stream open upon closing this stream.

BZip2OutputStream(Stream, int)

Constructs a new BZip2OutputStream with specified blocksize.

public BZip2OutputStream(Stream output, int blockSize)

Parameters

output Stream

the destination stream.

blockSize int

The blockSize in units of 100000 bytes. The valid range is 1..9.

BZip2OutputStream(Stream, int, bool)

Constructs a new BZip2OutputStream with specified blocksize, and explicitly specifies whether to leave the wrapped stream open.

public BZip2OutputStream(Stream output, int blockSize, bool leaveOpen)

Parameters

output Stream

the destination stream.

blockSize int

The blockSize in units of 100000 bytes. The valid range is 1..9.

leaveOpen bool

whether to leave the captive stream open upon closing this stream.

Properties

BlockSize

The blocksize parameter specified at construction time.

public int BlockSize { get; }

Property Value

int

CanRead

Indicates whether the stream can be read.

public override bool CanRead { get; }

Property Value

bool

Remarks

The return value is always false.

CanSeek

Indicates whether the stream supports Seek operations.

public override bool CanSeek { get; }

Property Value

bool

Remarks

Always returns false.

CanWrite

Indicates whether the stream can be written.

public override bool CanWrite { get; }

Property Value

bool

Remarks

The return value should always be true, unless and until the object is disposed and closed.

Length

Reading this property always throws a NotImplementedException.

public override long Length { get; }

Property Value

long

Position

The position of the stream pointer.

public override long Position { get; set; }

Property Value

long

Remarks

Setting this property always throws a NotImplementedException. Reading will return the total number of uncompressed bytes written through.

Methods

Close()

Close the stream.

public override void Close()

Remarks

This may or may not close the underlying stream. Check the constructors that accept a bool value.

Flush()

Flush the stream.

public override void Flush()

Read(byte[], int, int)

Calling this method always throws a NotImplementedException.

public override int Read(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

this parameter is never used

offset int

this parameter is never used

count int

this parameter is never used

Returns

int

never returns anything; always throws

Seek(long, SeekOrigin)

Calling this method always throws a NotImplementedException.

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

this is irrelevant, since it will always throw!

origin SeekOrigin

this is irrelevant, since it will always throw!

Returns

long

irrelevant!

SetLength(long)

Calling this method always throws a NotImplementedException.

public override void SetLength(long value)

Parameters

value long

this is irrelevant, since it will always throw!

Write(byte[], int, int)

Write data to the stream.

public override void Write(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

The buffer holding data to write to the stream.

offset int

the offset within that data array to find the first byte to write.

count int

the number of bytes to write.

Remarks

Use the BZip2OutputStream to compress data while writing: create a BZip2OutputStream with a writable output stream. Then call Write() on that BZip2OutputStream, providing uncompressed data as input. The data sent to the output stream will be the compressed form of the input data.

A BZip2OutputStream can be used only for Write() not for Read().