Example #1
0
            public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination,
                                         CancellationSignal cancellationSignal, WriteResultCallback callback)
            {
                // If we are already cancelled, don't do any work.
                if (cancellationSignal.IsCanceled)
                {
                    callback.OnWriteCancelled();
                    return;
                }

                var onWriteAsyncTask = new MyOnWriteAsyncTask(this, pages, destination, cancellationSignal, callback);

                onWriteAsyncTask.ExecuteOnExecutor(AsyncTask.ThreadPoolExecutor, (Java.Lang.Void[])null);
            }
        public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            InputStream input = null;

            OutputStream output = null;

            try
            {
                File file = new File(path);

                input  = new FileInputStream(file);
                output = new FileOutputStream(destination.FileDescriptor);

                byte[] buf = new byte[8 * 1024];

                int ligne;

                while ((ligne = input.Read(buf)) >= 0 && !cancellationSignal.IsCanceled)
                {
                    output.Write(buf, 0, ligne);
                }

                if (cancellationSignal.IsCanceled)
                {
                    callback.OnWriteCancelled();
                }
                else
                {
                    callback.OnWriteFinished(new PageRange[] { PageRange.AllPages });
                }
            }
            catch (Exception ex)
            {
                callback.OnWriteFailed(ex.Message);
            }
            finally
            {
                try
                {
                    input.Close();

                    output.Close();
                }
                catch (IOException ex)
                {
                    Log.Error("e", "", ex.Message);
                }
            }
        }
Example #3
0
        public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
        {
            InputStream  input  = null;
            OutputStream output = null;

            try
            {
                File file = new File(_filePath);
                input  = new FileInputStream(file);
                output = new FileOutputStream(destination.FileDescriptor);

                byte[] buf = new byte[16384];
                int    size;

                while ((size = input.Read(buf)) >= 0 && !cancellationSignal.IsCanceled)
                {
                    output.Write(buf, 0, size);
                }

                if (cancellationSignal.IsCanceled)
                {
                    callback.OnWriteCancelled();
                }
                else
                {
                    callback.OnWriteFinished(new PageRange[] { PageRange.AllPages });
                }
            }
            catch (Exception e)
            {
                callback.OnWriteFailed(e.Message);
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
            finally
            {
                try
                {
                    input?.Close();
                    output?.Close();
                }
                catch (IOException e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
            }
        }
Example #4
0
            public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
            {
                int totalPages = 1;

                for (int i = 0; i < totalPages; i++)
                {
                    if (pageInRange(pages, i))
                    {
                        PdfDocument.Page[] writtenPagesArray;
                        PdfDocument.Page   page = mPdfDocument.StartPage(i);
                        if (cancellationSignal.IsCanceled)
                        {
                            callback.OnWriteCancelled();
                            mPdfDocument.Close();
                            mPdfDocument = null;
                            return;
                        }
                        drawPage(page);
                        mPdfDocument.FinishPage(page);
                    }
                }
                try
                {
                    FileOutputStream fileOutputStream = new FileOutputStream(destination.FileDescriptor);
                    MemoryStream     stream           = new MemoryStream();
                    mPdfDocument.WriteTo(stream);
                    byte[] bytes = stream.ToArray();
                    stream.Close();
                    fileOutputStream.Write(bytes);
                }
                catch (Java.IO.IOException e)
                {
                    callback.OnWriteFailed(e.ToString());
                    return;
                }
                finally
                {
                    mPdfDocument.Close();
                    mPdfDocument = null;
                }
                callback.OnWriteFinished(pages);
            }
Example #5
0
 protected override void OnCancelled()
 {
     // Task was cancelled, report that.
     callback.OnWriteCancelled();
     mPdfDocument.Close();
 }