Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ChainedStream to handle partial reads #143

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions MimeKit/IO/ChainedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,15 @@ public override int Read (byte[] buffer, int offset, int count)
int n, nread = 0;

while (current < streams.Count) {
if ((n = streams[current].Read (buffer, offset + nread, count - nread)) > 0) {
nread += n;

if (nread == count)
break;
}
n = streams[current].Read (buffer, offset + nread, count - nread);
nread += n;

current++;
if (nread == count)
break;

if (n==0)
current++;
}

if (nread > 0)
Expand Down
63 changes: 62 additions & 1 deletion UnitTests/ChainedStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void Setup ()
lengths.Add (n);
position += n;

chained.Add (new MemoryStream (segment));
chained.Add (new ReadOneByteStream(new MemoryStream (segment)));
}
}

Expand Down Expand Up @@ -177,5 +177,66 @@ public void TestChainedHeadersAndContent ()

Assert.AreEqual ("Hello, world!\r\n", entity.Text);
}

class ReadOneByteStream : Stream
{
readonly Stream _inner;

public ReadOneByteStream(Stream inner)
{
_inner = inner;
}

public override void Flush()
{
_inner.Flush();
}

public override long Seek(long offset, SeekOrigin origin)
{
return _inner.Seek(offset, origin);
}

public override void SetLength(long value)
{
_inner.SetLength(value);
}

public override int Read(byte[] buffer, int offset, int count)
{
return _inner.Read(buffer, offset, 1);
}

public override void Write(byte[] buffer, int offset, int count)
{
_inner.Write(buffer, offset, count);
}

public override bool CanRead
{
get { return _inner.CanRead; }
}

public override bool CanSeek
{
get { return _inner.CanSeek; }
}

public override bool CanWrite
{
get { return _inner.CanWrite; }
}

public override long Length
{
get { return _inner.Length; }
}

public override long Position
{
get { return _inner.Position; }
set { _inner.Position = value; }
}
}
}
}