protected override void Dispose(bool disposing)
        {
            if (_inputStream is object)
            {
                _inputStream.Close();
                _inputStream.Dispose();
                _inputStream = null;
            }

            if (_outputStream is object)
            {
                _outputStream.Close();
                _outputStream.Unschedule(NSRunLoop.Current, NSRunLoop.NSDefaultRunLoopMode);
                _outputStream.Dispose();
                _outputStream = null;
            }

            base.Dispose(disposing);
        }
Example #2
0
 public void Close()
 {
     _outputStream?.Close();
 }
        IObservable<Unit> SaveFullResolutionImageToFile (FileRequest request)
        {
            return Observable.Create<Unit> (o => {
                var description = request.DescriptionAs<AssetDescription> ();
                var disp = new CancellationDisposable ();
                var token = disp.Token;

                GetAsset (description, token).ContinueWith (t => {
                    using (File.Create (request.Filename))
                    using (var asset = t.Result)
                    using (var representation = asset.DefaultRepresentation) 
                    using (var stream = new NSOutputStream (request.Filename, true)) {
                        stream.Open ();

                        long offset = 0;
                        uint bytesRead = 0;

                        NSError err;

                        // A large enough buffer that shouldn't cause memory warnings
                        byte [] buffer = new byte [131072];

                        GCHandle handle = GCHandle.Alloc (buffer, GCHandleType.Pinned);
                        IntPtr pointer = handle.AddrOfPinnedObject ();
                        
                        unsafe {
                            while (offset < representation.Size && stream.HasSpaceAvailable ()) {
                                bytesRead = representation.GetBytes (pointer, offset, (uint)buffer.Length, out err);
                                stream.Write (buffer, bytesRead);
                                offset += bytesRead;
                            }
                        }
                        
                        stream.Close ();
                        handle.Free ();
                    }

                    o.OnCompleted ();
                }, token).RouteExceptions (o);

                return disp;
            });
        }