Files

197 lines
4.6 KiB
C#
Raw Permalink Normal View History

2013-04-28 11:25:37 +01:00
using System;
using System.IO;
namespace SharpCompress.Compressor.LZMA.Utilites
{
2013-04-28 12:32:55 +01:00
internal class CrcBuilderStream : Stream
2013-04-28 11:25:37 +01:00
{
private long mProcessed;
private Stream mTarget;
private uint mCRC;
private bool mFinished;
public CrcBuilderStream(Stream target)
{
mTarget = target;
2013-04-28 12:23:41 +01:00
mCRC = CRC.kInitCRC;
2013-04-28 11:25:37 +01:00
}
public long Processed
{
get { return mProcessed; }
}
public uint Finish()
{
2013-04-28 12:32:55 +01:00
if (!mFinished)
2013-04-28 11:25:37 +01:00
{
mFinished = true;
2013-04-28 12:23:41 +01:00
mCRC = CRC.Finish(mCRC);
2013-04-28 11:25:37 +01:00
}
return mCRC;
}
public override bool CanRead
{
get { return false; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Flush()
{
}
public override long Length
{
get { throw new NotSupportedException(); }
}
public override long Position
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new InvalidOperationException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
2013-04-28 12:32:55 +01:00
if (mFinished)
2013-04-28 11:25:37 +01:00
throw new InvalidOperationException("CRC calculation has been finished.");
mProcessed += count;
2013-04-28 12:23:41 +01:00
mCRC = CRC.Update(mCRC, buffer, offset, count);
2013-04-28 11:25:37 +01:00
mTarget.Write(buffer, offset, count);
}
}
2013-04-28 12:32:55 +01:00
internal class ReadingCrcBuilderStream : Stream
2013-04-28 11:25:37 +01:00
{
private long mProcessed;
private Stream mSource;
private uint mCRC;
private bool mFinished;
public ReadingCrcBuilderStream(Stream source)
{
mSource = source;
2013-04-28 12:23:41 +01:00
mCRC = CRC.kInitCRC;
2013-04-28 11:25:37 +01:00
}
protected override void Dispose(bool disposing)
{
try
{
2013-04-28 12:32:55 +01:00
if (disposing)
2013-04-28 11:25:37 +01:00
mSource.Dispose();
}
finally
{
base.Dispose(disposing);
}
}
public long Processed
{
get { return mProcessed; }
}
public uint Finish()
{
2013-04-28 12:32:55 +01:00
if (!mFinished)
2013-04-28 11:25:37 +01:00
{
mFinished = true;
2013-04-28 12:23:41 +01:00
mCRC = CRC.Finish(mCRC);
2013-04-28 11:25:37 +01:00
}
return mCRC;
}
public override bool CanRead
{
get { return mSource.CanRead; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override void Flush()
{
throw new NotImplementedException();
}
public override long Length
{
get { throw new NotImplementedException(); }
}
public override long Position
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public override int Read(byte[] buffer, int offset, int count)
{
2013-04-28 12:32:55 +01:00
if (count > 0 && !mFinished)
2013-04-28 11:25:37 +01:00
{
int read = mSource.Read(buffer, offset, count);
2013-04-28 12:32:55 +01:00
if (read > 0)
2013-04-28 11:25:37 +01:00
{
mProcessed += read;
2013-04-28 12:23:41 +01:00
mCRC = CRC.Update(mCRC, buffer, offset, read);
2013-04-28 11:25:37 +01:00
return read;
}
Finish();
}
return 0;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
}
2013-04-28 12:32:55 +01:00
}