cid#1607171 Data race condition
[LibreOffice.git] / android / source / src / java / org / libreoffice / PDFDocumentAdapter.java
blobcee01c198eca2b1739dfb552b1f3a141c5112723
1 package org.libreoffice;
3 import android.content.Context;
4 import android.os.Bundle;
5 import android.os.CancellationSignal;
6 import android.os.ParcelFileDescriptor;
7 import android.print.PageRange;
8 import android.print.PrintAttributes;
9 import android.print.PrintDocumentAdapter;
10 import android.print.PrintDocumentInfo;
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
19 public class PDFDocumentAdapter extends PrintDocumentAdapter{
20 Context mContext;
21 String pdfFile;
23 public PDFDocumentAdapter(Context mContext, String pdfFile) {
24 this.mContext = mContext;
25 this.pdfFile = pdfFile;
28 @Override
29 public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
30 if (cancellationSignal.isCanceled()) {
31 callback.onLayoutCancelled();
33 else {
34 File f = new File(pdfFile);
35 PrintDocumentInfo.Builder builder=
36 new PrintDocumentInfo.Builder(f.getName());
37 builder.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
38 .setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN)
39 .build();
40 callback.onLayoutFinished(builder.build(),
41 !newAttributes.equals(oldAttributes));
45 @Override
46 public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
47 InputStream in=null;
48 OutputStream out=null;
49 try {
50 File file = new File(pdfFile);
51 in = new FileInputStream(file);
52 out=new FileOutputStream(destination.getFileDescriptor());
54 byte[] buf=new byte[in.available()];
55 int size;
57 while ((size=in.read(buf)) >= 0
58 && !cancellationSignal.isCanceled()) {
59 out.write(buf, 0, size);
62 if (cancellationSignal.isCanceled()) {
63 callback.onWriteCancelled();
65 else {
66 callback.onWriteFinished(new PageRange[] { PageRange.ALL_PAGES });
69 catch (Exception e) {
70 callback.onWriteFailed(e.getMessage());
71 e.printStackTrace();
73 finally {
74 try {
75 in.close();
76 out.close();
78 catch (IOException e) {
79 e.printStackTrace();