Example #1
0
 public MtStreamReadingContext CreateReadingContext()
 {
     if (!_stream.CanRead)
     {
         throw new Exception("You can't read from this stream!");
     }
     var r = new MtStreamReadingContext(0);
     return r;
 }
Example #2
0
        public MtStreamReadingContext CreateReadingContext()
        {
            if (!_stream.CanRead)
            {
                throw new Exception("You can't read from this stream!");
            }
            var r = new MtStreamReadingContext(0);

            return(r);
        }
Example #3
0
        public void Read(MtStreamReadingContext ctx, Action <byte[], bool> readStuffCallback, Action doneCallback)
        {
#if DEBUG && !SILVERLIGHT
            Debug.Print("Read");
#endif

            try
            {
                var buffer = new byte[ReadBufferSize];

                AsyncCallback readCallback = null;

                readCallback = r =>
                {
                    var count = _stream.EndRead(r);

                    // Create a new array, copy read bytes to there
                    var read = new byte[count];

                    // Update position
                    ctx._position += count;

                    if (count > 0)
                    {
                        // Copy buffer to final count array
                        Array.Copy(buffer, read, count);

                        readStuffCallback(read, ctx._position >= _stream.Length);
                    }

                    if (ctx._position >= _stream.Length)
                    {
                        doneCallback();
                    }
                };

                if (ctx._position >= _stream.Length)
                {
                    throw new Exception("Can't read further, end of stream!");
                }

                // Put cursor in the right place
                _stream.Position = ctx._position;

#if DEBUG && !SILVERLIGHT
                MultiTasksRuntime.DebugDisplayInfo();
#endif
                _stream.BeginRead(buffer, 0, ReadBufferSize, readCallback, null);
            }
            catch (Exception e)
            {
                throw new Exception("Exception on Stream.read.", e);
            }
        }
Example #4
0
        public void Read(MtStreamReadingContext ctx, Action<byte[], bool> readStuffCallback, Action doneCallback)
        {
            #if DEBUG && !SILVERLIGHT
            Debug.Print("Read");
            #endif

            try
            {

                var buffer = new byte[ReadBufferSize];

                AsyncCallback readCallback = null;

                readCallback = r =>
                {
                    var count = _stream.EndRead(r);

                    // Create a new array, copy read bytes to there
                    var read = new byte[count];

                    // Update position
                    ctx._position += count;

                    if (count > 0)
                    {
                        // Copy buffer to final count array
                        Array.Copy(buffer, read, count);

                        readStuffCallback(read, ctx._position >= _stream.Length);
                    }

                    if (ctx._position >= _stream.Length)
                    {
                        doneCallback();
                    }
                };

                if (ctx._position >= _stream.Length)
                {
                    throw new Exception("Can't read further, end of stream!");
                }

                // Put cursor in the right place
                _stream.Position = ctx._position;

            #if DEBUG && !SILVERLIGHT
                MultiTasksRuntime.DebugDisplayInfo();
            #endif
                _stream.BeginRead(buffer, 0, ReadBufferSize, readCallback, null);
            }
            catch (Exception e)
            {
                throw new Exception("Exception on Stream.read.", e);
            }
        }