Skip to content
Merged
174 changes: 95 additions & 79 deletions ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public BZip2OutputStream(Stream stream, int blockSize)
/// Get/set flag indicating ownership of underlying stream.
/// When the flag is true <see cref="Close"></see> will close the underlying stream also.
/// </summary>
public bool IsStreamOwner {
public bool IsStreamOwner
{
get { return isStreamOwner; }
set { isStreamOwner = value; }
}
Expand All @@ -104,47 +105,58 @@ public bool IsStreamOwner {
/// <summary>
/// Gets a value indicating whether the current stream supports reading
/// </summary>
public override bool CanRead {
get {
public override bool CanRead
{
get
{
return false;
}
}

/// <summary>
/// Gets a value indicating whether the current stream supports seeking
/// </summary>
public override bool CanSeek {
get {
public override bool CanSeek
{
get
{
return false;
}
}

/// <summary>
/// Gets a value indicating whether the current stream supports writing
/// </summary>
public override bool CanWrite {
get {
public override bool CanWrite
{
get
{
return baseStream.CanWrite;
}
}

/// <summary>
/// Gets the length in bytes of the stream
/// </summary>
public override long Length {
get {
public override long Length
{
get
{
return baseStream.Length;
}
}

/// <summary>
/// Gets or sets the current position of this stream.
/// </summary>
public override long Position {
get {
public override long Position
{
get
{
return baseStream.Position;
}
set {
set
{
throw new NotSupportedException("BZip2OutputStream position cannot be set");
}
}
Expand Down Expand Up @@ -282,37 +294,37 @@ void WriteRun()
}

switch (runLength) {
case 1:
last++;
block[last + 1] = (byte)currentChar;
break;
case 2:
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
break;
case 3:
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
break;
default:
inUse[runLength - 4] = true;
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)(runLength - 4);
break;
case 1:
last++;
block[last + 1] = (byte)currentChar;
break;
case 2:
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
break;
case 3:
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
break;
default:
inUse[runLength - 4] = true;
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)currentChar;
last++;
block[last + 1] = (byte)(runLength - 4);
break;
}
} else {
EndBlock();
Expand All @@ -324,7 +336,8 @@ void WriteRun()
/// <summary>
/// Get the number of bytes written to the output.
/// </summary>
public int BytesWritten {
public int BytesWritten
{
get { return bytesOut; }
}

Expand All @@ -335,25 +348,28 @@ public int BytesWritten {
override protected void Dispose(bool disposing)
{
try {
base.Dispose(disposing);
if (!disposed_) {
disposed_ = true;
try {
base.Dispose(disposing);
if (!disposed_) {
disposed_ = true;

if (runLength > 0) {
WriteRun();
}
if (runLength > 0) {
WriteRun();
}

currentChar = -1;
EndBlock();
EndCompression();
Flush();
}
} finally {
if (disposing) {
if (IsStreamOwner) {
baseStream.Close();
currentChar = -1;
EndBlock();
EndCompression();
Flush();
}
} finally {
if (disposing) {
if (IsStreamOwner) {
baseStream.Close();
}
}
}
} catch {
}
}

Expand Down Expand Up @@ -1495,16 +1511,16 @@ void GenerateMTFValues()
zPend--;
while (true) {
switch (zPend % 2) {
case 0:
szptr[wr] = (short)BZip2Constants.RunA;
wr++;
mtfFreq[BZip2Constants.RunA]++;
break;
case 1:
szptr[wr] = (short)BZip2Constants.RunB;
wr++;
mtfFreq[BZip2Constants.RunB]++;
break;
case 0:
szptr[wr] = (short)BZip2Constants.RunA;
wr++;
mtfFreq[BZip2Constants.RunA]++;
break;
case 1:
szptr[wr] = (short)BZip2Constants.RunB;
wr++;
mtfFreq[BZip2Constants.RunB]++;
break;
}
if (zPend < 2) {
break;
Expand All @@ -1523,16 +1539,16 @@ void GenerateMTFValues()
zPend--;
while (true) {
switch (zPend % 2) {
case 0:
szptr[wr] = (short)BZip2Constants.RunA;
wr++;
mtfFreq[BZip2Constants.RunA]++;
break;
case 1:
szptr[wr] = (short)BZip2Constants.RunB;
wr++;
mtfFreq[BZip2Constants.RunB]++;
break;
case 0:
szptr[wr] = (short)BZip2Constants.RunA;
wr++;
mtfFreq[BZip2Constants.RunA]++;
break;
case 1:
szptr[wr] = (short)BZip2Constants.RunB;
wr++;
mtfFreq[BZip2Constants.RunB]++;
break;
}
if (zPend < 2) {
break;
Expand Down
2 changes: 2 additions & 0 deletions ICSharpCode.SharpZipLib/Zip/Compression/PendingBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ public int Flush(byte[] output, int offset, int length)
/// </returns>
public byte[] ToByteArray()
{
AlignToByte();

byte[] result = new byte[end - start];
System.Array.Copy(buffer_, start, result, 0, result.Length);
start = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,8 @@ public override bool CanWrite {
/// </summary>
public override long Length {
get {
return inputBuffer.RawLength;
//return inputBuffer.RawLength;
throw new NotSupportedException("InflaterInputStream Length is not supported");
}
}

Expand Down