0

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.

5
  • 1
    That doesn't make any sense. How can you serialize a filestream? Commented May 10, 2012 at 21:29
  • Im passing it as stream object + some information about it as a massage. Commented May 10, 2012 at 21:33
  • @Jonathan Allen: WCF can work in streaming mode, where is just sends the data as bytes. see msdn.microsoft.com/en-us/library/ms731913.aspx Commented May 10, 2012 at 22:33
  • I'm also surprised this is possible. The FileStream object is neither [Serializable] or [DataContract]. Does this work because it is a subclass of Stream, which is [Serializable]? Commented May 11, 2012 at 15:45
  • Must be, cant find other reason. Commented May 11, 2012 at 17:47

1 Answer 1

4

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; } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Good idea. but what do you mean by "Extend FileStream or create a wrapper for it" ? Inheritance ?
Thank you. *you wrote a code after return line. mistake or am I missing here something?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.