Does the internal source return fixed size buffers? In this case, this is not exactly what it does BufferedStream- it simply reduces the number of calls in the physical stream. You will need a separate caching mechanism - a MemoryStream, which you fill in and empty, would be a smart choice. For example (completely untested):
MemoryStream localBuffer = new MemoryStream();
bool ReadNextChunk()
{
localBuffer.Position = 0;
localBuffer.SetLength(0);
byte[] chunk = null;
if(chunk == null || chunk.Length == 0) return false;
localBuffer.Write(chunk, 0, chunk.Length);
localBuffer.Position = 0;
return true;
}
public override int Read(byte[] buffer, int offset, int count)
{
int bytes;
if ((bytes = localBuffer.Read(buffer, offset, count)) > 0) return bytes;
if (!ReadNextChunk()) return 0;
return localBuffer.Read(buffer, offset, count);
}
source
share