123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System;
- using System.IO;
- namespace PaintDotNet
- {
- /// <summary>
- /// This was written as a workaround for a bug in SharpZipLib that prevents it
- /// from working right with huge Write() commands. So we split the incoming
- /// requests into smaller requests, like 4KB each or so.
- ///
- /// However, this didn't work around the bug. But now I use this class so that
- /// I can keep tabs on a serialization or deserialization operation and have a
- /// dialog box with a progress bar.
- /// </summary>
- public sealed class SiphonStream
- : Stream
- {
- private Exception throwMe;
- private Stream stream;
- private int siphonSize;
- private object tag = null;
- public object Tag
- {
- get
- {
- return this.tag;
- }
- set
- {
- this.tag = value;
- }
- }
- /// <summary>
- /// Causes the next call to Read() or Write() to throw an IOException instead. The
- /// exception passed to this method will be used as the InnerException.
- /// </summary>
- /// <param name="throwMe"></param>
- public void Abort(Exception newThrowMe)
- {
- if (newThrowMe == null)
- {
- throw new ArgumentException("throwMe may not be null", "throwMe");
- }
- this.throwMe = newThrowMe;
- }
- public event IOEventHandler IOFinished;
- private void OnIOFinished(IOEventArgs e)
- {
- if (IOFinished != null)
- {
- IOFinished(this, e);
- }
- }
- int readAccumulator = 0;
- int writeAccumulator = 0;
- private void ReadAccumulate(int count)
- {
- if (count == -1)
- {
- if (this.readAccumulator > 0)
- {
- OnIOFinished(new IOEventArgs(IOOperationType.Read, this.Position, this.readAccumulator));
- this.readAccumulator = 0;
- }
- }
- else
- {
- WriteAccumulate(-1);
- this.readAccumulator += count;
- while (this.readAccumulator > this.siphonSize)
- {
- OnIOFinished(new IOEventArgs(IOOperationType.Read, this.Position - this.readAccumulator + this.siphonSize, this.siphonSize));
- this.readAccumulator -= this.siphonSize;
- }
- }
- }
- private void WriteAccumulate(int count)
- {
- if (count == -1)
- {
- if (this.writeAccumulator > 0)
- {
- OnIOFinished(new IOEventArgs(IOOperationType.Write, this.Position, writeAccumulator));
- this.writeAccumulator = 0;
- }
- }
- else
- {
- ReadAccumulate(-1);
- this.writeAccumulator += count;
- while (this.writeAccumulator > this.siphonSize)
- {
- OnIOFinished(new IOEventArgs(IOOperationType.Write, this.Position - this.writeAccumulator + this.siphonSize, this.siphonSize));
- this.writeAccumulator -= this.siphonSize;
- }
- }
- }
- public override int Read(byte[] buffer, int offset, int count)
- {
- if (throwMe != null)
- {
- throw new IOException("Aborted", this.throwMe);
- }
- int countLeft = count;
- int cursor = offset;
- int totalAmountRead = 0;
- while (cursor < offset + count)
- {
- int count2 = Math.Min(this.siphonSize, countLeft);
- int amountRead = stream.Read(buffer, cursor, count2);
- ReadAccumulate(amountRead);
- countLeft -= amountRead;
- cursor += amountRead;
- totalAmountRead += amountRead;
- if (amountRead == 0)
- {
- break;
- }
- }
- return totalAmountRead;
- }
- public override void Write(byte[] buffer, int offset, int count)
- {
- if (this.throwMe != null)
- {
- throw new IOException("Aborted", this.throwMe);
- }
- int countLeft = count;
- int cursor = offset;
- while (cursor < offset + count)
- {
- int count2 = Math.Min(this.siphonSize, countLeft);
- stream.Write(buffer, cursor, count2);
- WriteAccumulate(count2);
- countLeft -= count2;
- cursor += count2;
- }
- }
- public override bool CanRead
- {
- get
- {
- return this.stream.CanRead;
- }
- }
- public override bool CanWrite
- {
- get
- {
- return this.stream.CanWrite;
- }
- }
- public override bool CanSeek
- {
- get
- {
- return this.stream.CanSeek;
- }
- }
- public override void Flush()
- {
- this.stream.Flush();
- }
- public override long Length
- {
- get
- {
- return this.stream.Length;
- }
- }
- public override long Position
- {
- get
- {
- return this.stream.Position;
- }
- set
- {
- this.stream.Position = value;
- }
- }
- public override long Seek(long offset, SeekOrigin origin)
- {
- return this.stream.Seek(offset, origin);
- }
- public override void SetLength(long value)
- {
- this.stream.SetLength(value);
- }
- public SiphonStream(Stream underlyingStream)
- : this(underlyingStream, 65536)
- {
- }
- public SiphonStream(Stream underlyingStream, int siphonSize)
- {
- this.stream = underlyingStream;
- this.siphonSize = siphonSize;
- }
- }
- }
|