Class BZip2OutputStream
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
outputStreamThe 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
outputStreamthe destination stream.
leaveOpenboolwhether 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
outputStreamthe destination stream.
blockSizeintThe 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
outputStreamthe destination stream.
blockSizeintThe blockSize in units of 100000 bytes. The valid range is 1..9.
leaveOpenboolwhether 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
CanRead
Indicates whether the stream can be read.
public override bool CanRead { get; }
Property Value
Remarks
The return value is always false.
CanSeek
Indicates whether the stream supports Seek operations.
public override bool CanSeek { get; }
Property Value
Remarks
Always returns false.
CanWrite
Indicates whether the stream can be written.
public override bool CanWrite { get; }
Property Value
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
Position
The position of the stream pointer.
public override long Position { get; set; }
Property Value
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
bufferbyte[]this parameter is never used
offsetintthis parameter is never used
countintthis 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
offsetlongthis is irrelevant, since it will always throw!
originSeekOriginthis 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
valuelongthis 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
bufferbyte[]The buffer holding data to write to the stream.
offsetintthe offset within that data array to find the first byte to write.
countintthe 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().