From 85e574618b04a34d5e41444d17ce7e6d5a93cc5b Mon Sep 17 00:00:00 2001 From: netconomy Date: Thu, 6 Sep 2007 12:18:45 +0000 Subject: =?UTF-8?q?Streaming=20R=C3=BCckbau?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@210 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../at/gv/egiz/pdfas/web/helper/TempDirHelper.java | 63 +++++++++++++++------- 1 file changed, 43 insertions(+), 20 deletions(-) (limited to 'src/main/java/at/gv/egiz/pdfas/web/helper/TempDirHelper.java') diff --git a/src/main/java/at/gv/egiz/pdfas/web/helper/TempDirHelper.java b/src/main/java/at/gv/egiz/pdfas/web/helper/TempDirHelper.java index 0745bcc..23c01fd 100644 --- a/src/main/java/at/gv/egiz/pdfas/web/helper/TempDirHelper.java +++ b/src/main/java/at/gv/egiz/pdfas/web/helper/TempDirHelper.java @@ -21,12 +21,16 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import at.gv.egiz.pdfas.framework.input.DataSource; +import at.gv.egiz.pdfas.framework.input.PdfDataSource; import at.gv.egiz.pdfas.framework.input.TextDataSource; import at.gv.egiz.pdfas.framework.output.DataSink; +import at.gv.egiz.pdfas.impl.input.ByteArrayPdfDataSourceImpl; import at.gv.egiz.pdfas.impl.input.FileBased; import at.gv.egiz.pdfas.impl.input.FileBasedPdfDataSourceImpl; import at.gv.egiz.pdfas.impl.input.FileBasedTextDataSourceImpl; import at.gv.egiz.pdfas.impl.input.TextDataSourceImpl; +import at.gv.egiz.pdfas.impl.input.helper.DataSourceHelper; +import at.gv.egiz.pdfas.impl.output.ByteArrayDataSink; import at.gv.egiz.pdfas.impl.output.FileBasedDataSink; import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; import at.knowcenter.wag.egov.egiz.pdf.SignatureHolder; @@ -119,11 +123,16 @@ public class TempDirHelper return textLength >= 10000; } - public static FileBasedPdfDataSourceImpl placePdfIntoTempDir(InputStream pdfInput, String fileNameSuffix) throws IOException + public static PdfDataSource placePdfIntoTempDir(InputStream pdfInput, String fileNameSuffix) throws IOException { - File pdfFile = placeInputIntoTempDirFile(pdfInput, fileNameSuffix); - - FileBasedPdfDataSourceImpl pdfDataSource = new FileBasedPdfDataSourceImpl(pdfFile, (int) pdfFile.length()); + log.debug("PERF: placing pdf into memory cache"); + + byte [] pdfData = DataSourceHelper.convertInputStreamToByteArray(pdfInput); + PdfDataSource pdfDataSource = new ByteArrayPdfDataSourceImpl(pdfData); + +// File pdfFile = placeInputIntoTempDirFile(pdfInput, fileNameSuffix); +// +// FileBasedPdfDataSourceImpl pdfDataSource = new FileBasedPdfDataSourceImpl(pdfFile, (int) pdfFile.length()); return pdfDataSource; } @@ -177,35 +186,49 @@ public class TempDirHelper return tmpFile; } - public static FileBasedDataSink createTempDataSink(String fileNameSuffix) throws IOException + public static DataSink createTempDataSink(String fileNameSuffix) throws IOException { - String fileName = formatFileName(fileNameSuffix); - - File tmpFile = createTempFileInDir(fileName); + log.debug("PERF: placing pdf into memory cache"); - FileBasedDataSink fbds = new FileBasedDataSink(tmpFile); + DataSink ds = new ByteArrayDataSink(); + +// String fileName = formatFileName(fileNameSuffix); +// +// File tmpFile = createTempFileInDir(fileName); +// +// FileBasedDataSink ds = new FileBasedDataSink(tmpFile); - return fbds; + return ds; } - public static void writeDataSinkToHttpResponse(FileBasedDataSink fbds, HttpServletResponse response) throws IOException + public static void writeDataSinkToHttpResponse(DataSink ds, HttpServletResponse response) throws IOException { - response.setContentType(fbds.getMimeType()); - response.setCharacterEncoding(fbds.getCharacterEncoding()); + response.setContentType(ds.getMimeType()); + response.setCharacterEncoding(ds.getCharacterEncoding()); OutputStream os = response.getOutputStream(); - byte[] buffer = new byte[2048]; - FileInputStream fis = new FileInputStream(fbds.getFile()); - int n = -1; - while ((n = fis.read(buffer)) > 0) + if (ds instanceof FileBasedDataSink) { - os.write(buffer, 0, n); + FileBasedDataSink fbds = (FileBasedDataSink)ds; + byte[] buffer = new byte[2048]; + FileInputStream fis = new FileInputStream(fbds.getFile()); + int n = -1; + while ((n = fis.read(buffer)) > 0) + { + os.write(buffer, 0, n); + } + fis.close(); } - fis.close(); + else + { + ByteArrayDataSink bads = (ByteArrayDataSink)ds; + os.write(bads.getByteArray()); + } + os.close(); - } +} /** * Deletes the underlying file of the FileBased DataSource. -- cgit v1.2.3