Skip to content

Commit d2526a8

Browse files
[BZip2] Enforce baseStream to be non-null in BZip2InputStream and BZip2OutputStream (constructor now throws an ArgumentNullException).
1 parent 920d0d2 commit d2526a8

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs

+11-16
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ during decompression.
6666
int[][] perm = new int[BZip2Constants.GroupCount][];
6767
int[] minLens = new int[BZip2Constants.GroupCount];
6868

69-
Stream baseStream;
69+
readonly Stream baseStream;
7070
bool streamEnd;
7171

7272
int currentChar = -1;
@@ -91,14 +91,18 @@ during decompression.
9191
/// <param name="stream">Data source</param>
9292
public BZip2InputStream(Stream stream)
9393
{
94+
if (stream == null)
95+
throw new ArgumentNullException(nameof(stream));
9496
// init arrays
9597
for (int i = 0; i < BZip2Constants.GroupCount; ++i) {
9698
limit[i] = new int[BZip2Constants.MaximumAlphaSize];
9799
baseArray[i] = new int[BZip2Constants.MaximumAlphaSize];
98100
perm[i] = new int[BZip2Constants.MaximumAlphaSize];
99101
}
100102

101-
BsSetStream(stream);
103+
baseStream = stream;
104+
bsLive = 0;
105+
bsBuff = 0;
102106
Initialize();
103107
InitBlock();
104108
SetupBlock();
@@ -149,10 +153,10 @@ public override long Length {
149153
}
150154

151155
/// <summary>
152-
/// Gets or sets the streams position.
153-
/// Setting the position is not supported and will throw a NotSupportException
156+
/// Gets the current position of the stream.
157+
/// Setting the position is not supported and will throw a NotSupportException.
154158
/// </summary>
155-
/// <exception cref="NotSupportedException">Any attempt to set the position</exception>
159+
/// <exception cref="NotSupportedException">Any attempt to set the position.</exception>
156160
public override long Position {
157161
get {
158162
return baseStream.Position;
@@ -167,9 +171,7 @@ public override long Position {
167171
/// </summary>
168172
public override void Flush()
169173
{
170-
if (baseStream != null) {
171-
baseStream.Flush();
172-
}
174+
baseStream.Flush();
173175
}
174176

175177
/// <summary>
@@ -250,7 +252,7 @@ public override int Read(byte[] buffer, int offset, int count)
250252
/// </summary>
251253
protected override void Dispose(bool disposing)
252254
{
253-
if (disposing && IsStreamOwner && (baseStream != null)) {
255+
if (disposing && IsStreamOwner) {
254256
baseStream.Dispose();
255257
}
256258
}
@@ -371,13 +373,6 @@ void Complete()
371373
streamEnd = true;
372374
}
373375

374-
void BsSetStream(Stream stream)
375-
{
376-
baseStream = stream;
377-
bsLive = 0;
378-
bsBuff = 0;
379-
}
380-
381376
void FillBuffer()
382377
{
383378
int thech = 0;

src/ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs

+8-10
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ The current block size is 100000 * this number.
100100
int runLength;
101101
uint blockCRC, combinedCRC;
102102
int allowableBlockSize;
103-
Stream baseStream;
103+
readonly Stream baseStream;
104104
bool disposed_;
105105
#endregion
106106

@@ -124,7 +124,13 @@ public BZip2OutputStream(Stream stream) : this(stream, 9)
124124
/// </remarks>
125125
public BZip2OutputStream(Stream stream, int blockSize)
126126
{
127-
BsSetStream(stream);
127+
if (stream == null)
128+
throw new ArgumentNullException(nameof(stream));
129+
130+
baseStream = stream;
131+
bsLive = 0;
132+
bsBuff = 0;
133+
bytesOut = 0;
128134

129135
workFactor = 50;
130136
if (blockSize > 9) {
@@ -528,14 +534,6 @@ void EndCompression()
528534
BsFinishedWithStream();
529535
}
530536

531-
void BsSetStream(Stream stream)
532-
{
533-
baseStream = stream;
534-
bsLive = 0;
535-
bsBuff = 0;
536-
bytesOut = 0;
537-
}
538-
539537
void BsFinishedWithStream()
540538
{
541539
while (bsLive > 0) {

0 commit comments

Comments
 (0)