Squared.Task.IO.FileDataAdapter.ReadToEnd C# (CSharp) Method

ReadToEnd() public method

public ReadToEnd ( ) : Future
return Future
        public Future<byte[]> ReadToEnd()
        {
            var length = (int)(BaseStream.Length - BaseStream.Position);
            var bytes = new byte[length];
            var result = new Future<byte[]>();

            var f = Read(bytes, 0, length);

            f.RegisterOnComplete(
                (_) => {
                    int bytesRead;
                    Exception error;

                    f.GetResult(out bytesRead, out error);

                    if ((error == null) && (bytesRead == bytes.Length))
                        result.SetResult(bytes, null);
                    else
                        result.SetResult(null, error);
                }
            );

            return result;
        }

Usage Example

Ejemplo n.º 1
0
        protected IEnumerator<object> SnapshotIOTask()
        {
            ActivityIndicator.Item progress = null;
            IFuture previousSnapshot = null;

            while (true) {
                if ((SnapshotLoadQueue.Count <= 0) && (previousSnapshot != null))
                    yield return previousSnapshot;

                if (SnapshotLoadQueue.Count <= 0) {
                    SnapshotLoadState.Count = 0;
                    if (progress != null) {
                        progress.Dispose();
                        progress = null;
                    }
                }

                var f = SnapshotLoadQueue.Dequeue();
                using (f)
                    yield return f;

                var filename = f.Result;

                if (progress == null)
                    progress = Activities.AddItem("Loading snapshots");

                using (var fda = new FileDataAdapter(
                    filename, FileMode.Open,
                    FileAccess.Read, FileShare.Read, 1024 * 128
                )) {
                    var maximum = SnapshotLoadState.Count + Math.Max(0, SnapshotLoadQueue.Count) + 1;
                    progress.Maximum = maximum;
                    progress.Progress = Math.Min(maximum, SnapshotLoadState.Count);

                    var fBytes = fda.ReadToEnd();
                    yield return fBytes;

                    var fText = Future.RunInThread(
                        () => Encoding.ASCII.GetString(fBytes.Result)
                    );
                    yield return fText;

                    var text = fText.Result;
                    fBytes = null;
                    fText = null;

                    // Wait for the last snapshot load we triggered to finish
                    if (previousSnapshot != null)
                        yield return previousSnapshot;

                    previousSnapshot = Scheduler.Start(
                        FinishLoadingSnapshot(filename, text), TaskExecutionPolicy.RunAsBackgroundTask
                    );

                    text = null;
                }
            }
        }
All Usage Examples Of Squared.Task.IO.FileDataAdapter::ReadToEnd