package at.gv.egiz.pdfas.utils; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import at.gv.egiz.pdfas.exceptions.ErrorCode; import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; import com.lowagie.text.pdf.PdfReader; /** * @author tknall */ public class PDFASUtils { private PDFASUtils() { } /** * Verifies that a document could be opened with full permissions. * @param pdfReader The PdfReader * @throws PDFDocumentException Thrown if document has not been opened with full permissions. */ public static void checkReaderPermissions(PdfReader pdfReader) throws PDFDocumentException { if (pdfReader.isEncrypted()) { throw new PDFDocumentException(ErrorCode.DOCUMENT_IS_PROTECTED, "Document is encrypted."); } if (!pdfReader.isOpenedWithFullPermissions()) { throw new PDFDocumentException(ErrorCode.DOCUMENT_IS_PROTECTED, "Document is protected."); } } public static boolean toFile(byte[] data, File file) throws IOException { return PDFASUtils.toFile(new ByteArrayInputStream(data), file); } public static boolean toFile(InputStream inputStream, File file) throws IOException { boolean result = false; BufferedOutputStream bufferedOutputStream = null; try { bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); ConfigUtils.writeInputStreamToOutputStream(inputStream, bufferedOutputStream); } finally { if (bufferedOutputStream != null) { try { bufferedOutputStream.close(); result = true; } catch (IOException e) { result = false; } } } return result; } }