Reputation: 13953
After I pass a reference of filestream from the client to the service, and the service start downloading the stream to him, how can I determine, from the client side, how much bytes were read until now (while Im using the filestream object)?
My goal is to calculate client's upload speed only for this file and the only way I can think of is this.
Upvotes: 0
Views: 1199
Reputation: 2540
Extend FileStream or create a wrapper for it. Override the read methods and have a counter count the bytes read.
extending (not properly implement, but should be more than enough to explain)
public class CountingStream : System.IO.FileStream {
// provide appropriate constructors
// may want to override BeginRead too
// not thread safe
private long _Counter = 0;
public override int ReadByte() {
_Counter++;
return base.ReadByte();
}
public override int Read(byte[] array, int offset, int count) {
// check if not going over the end of the stream
_Counter += count;
return base.Read(array, offset, count);
}
public long BytesReadSoFar {
get {
return _Counter;
}
}
}
Upvotes: 4