Skip to content

Commit

Permalink
[BZip2] Enforce baseStream to be non-null in BZip2InputStream and BZi…
Browse files Browse the repository at this point in the history
…p2OutputStream (constructor now throws an ArgumentNullException).
  • Loading branch information
siegfriedpammer committed Aug 18, 2017
1 parent 920d0d2 commit d2526a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
27 changes: 11 additions & 16 deletions src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ during decompression.
int[][] perm = new int[BZip2Constants.GroupCount][];
int[] minLens = new int[BZip2Constants.GroupCount];

Stream baseStream;
readonly Stream baseStream;
bool streamEnd;

int currentChar = -1;
Expand All @@ -91,14 +91,18 @@ during decompression.
/// <param name="stream">Data source</param>
public BZip2InputStream(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
// init arrays
for (int i = 0; i < BZip2Constants.GroupCount; ++i) {
limit[i] = new int[BZip2Constants.MaximumAlphaSize];
baseArray[i] = new int[BZip2Constants.MaximumAlphaSize];
perm[i] = new int[BZip2Constants.MaximumAlphaSize];
}

BsSetStream(stream);
baseStream = stream;
bsLive = 0;
bsBuff = 0;
Initialize();
InitBlock();
SetupBlock();
Expand Down Expand Up @@ -149,10 +153,10 @@ public override long Length {
}

/// <summary>
/// Gets or sets the streams position.
/// Setting the position is not supported and will throw a NotSupportException
/// Gets the current position of the stream.
/// Setting the position is not supported and will throw a NotSupportException.
/// </summary>
/// <exception cref="NotSupportedException">Any attempt to set the position</exception>
/// <exception cref="NotSupportedException">Any attempt to set the position.</exception>
public override long Position {
get {
return baseStream.Position;
Expand All @@ -167,9 +171,7 @@ public override long Position {
/// </summary>
public override void Flush()
{
if (baseStream != null) {
baseStream.Flush();
}
baseStream.Flush();
}

/// <summary>
Expand Down Expand Up @@ -250,7 +252,7 @@ public override int Read(byte[] buffer, int offset, int count)
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing && IsStreamOwner && (baseStream != null)) {
if (disposing && IsStreamOwner) {
baseStream.Dispose();
}
}
Expand Down Expand Up @@ -371,13 +373,6 @@ void Complete()
streamEnd = true;
}

void BsSetStream(Stream stream)
{
baseStream = stream;
bsLive = 0;
bsBuff = 0;
}

void FillBuffer()
{
int thech = 0;
Expand Down
18 changes: 8 additions & 10 deletions src/ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ The current block size is 100000 * this number.
int runLength;
uint blockCRC, combinedCRC;
int allowableBlockSize;
Stream baseStream;
readonly Stream baseStream;
bool disposed_;
#endregion

Expand All @@ -124,7 +124,13 @@ public BZip2OutputStream(Stream stream) : this(stream, 9)
/// </remarks>
public BZip2OutputStream(Stream stream, int blockSize)
{
BsSetStream(stream);
if (stream == null)
throw new ArgumentNullException(nameof(stream));

baseStream = stream;
bsLive = 0;
bsBuff = 0;
bytesOut = 0;

workFactor = 50;
if (blockSize > 9) {
Expand Down Expand Up @@ -528,14 +534,6 @@ void EndCompression()
BsFinishedWithStream();
}

void BsSetStream(Stream stream)
{
baseStream = stream;
bsLive = 0;
bsBuff = 0;
bytesOut = 0;
}

void BsFinishedWithStream()
{
while (bsLive > 0) {
Expand Down

0 comments on commit d2526a8

Please sign in to comment.