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 --- .../impl/input/ByteArrayPdfDataSourceImpl.java | 23 ++++++---- .../impl/input/CompoundPdfDataSourceImpl.java | 18 ++++++++ .../pdfas/impl/input/DelimitedPdfDataSource.java | 18 ++++++++ .../impl/input/FileBasedPdfDataSourceImpl.java | 30 ++++++++++++- .../impl/input/FileBasedTextDataSourceImpl.java | 18 ++++++++ .../egiz/pdfas/impl/input/TextDataSourceImpl.java | 16 +++++++ .../pdfas/impl/input/helper/DataSourceHelper.java | 49 ++++++++++++++++++---- .../egiz/pdfas/impl/output/ByteArrayDataSink.java | 3 ++ .../signator/textual/TextualSignator_1_0_0.java | 2 +- .../pdfas/impl/vfilter/VerificationFilterImpl.java | 19 +++++++-- 10 files changed, 175 insertions(+), 21 deletions(-) (limited to 'src/main/java/at/gv/egiz/pdfas/impl') diff --git a/src/main/java/at/gv/egiz/pdfas/impl/input/ByteArrayPdfDataSourceImpl.java b/src/main/java/at/gv/egiz/pdfas/impl/input/ByteArrayPdfDataSourceImpl.java index 0d27781..e4a5649 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/input/ByteArrayPdfDataSourceImpl.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/input/ByteArrayPdfDataSourceImpl.java @@ -23,34 +23,43 @@ public class ByteArrayPdfDataSourceImpl implements PdfDataSource { protected byte[] pdf = null; - protected int length = -1; - public ByteArrayPdfDataSourceImpl(byte[] pdf) { PerformanceCounters.byteArrays.increment(); this.pdf = pdf; - this.length = pdf.length; } public ByteArrayPdfDataSourceImpl(byte[] pdf, int length) { PerformanceCounters.byteArrays.increment(); - this.pdf = pdf; - this.length = length; + if (pdf.length == length) + { + this.pdf = pdf; + } + else + { + this.pdf = new byte [length]; + System.arraycopy(pdf, 0, this.pdf, 0, length); + } } public InputStream createInputStream() { - ByteArrayInputStream bais = new ByteArrayInputStream(this.pdf, 0, this.length); + ByteArrayInputStream bais = new ByteArrayInputStream(this.pdf); return bais; } public int getLength() { - return this.length; + return this.pdf.length; } + public byte[] getAsByteArray() + { + return this.pdf; + } + } diff --git a/src/main/java/at/gv/egiz/pdfas/impl/input/CompoundPdfDataSourceImpl.java b/src/main/java/at/gv/egiz/pdfas/impl/input/CompoundPdfDataSourceImpl.java index f77d6be..4f00bb5 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/input/CompoundPdfDataSourceImpl.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/input/CompoundPdfDataSourceImpl.java @@ -43,5 +43,23 @@ public class CompoundPdfDataSourceImpl implements PdfDataSource { return this.originalDataSource.getLength() + this.appendix.length; } + + byte [] cache = null; + /** + * @see at.gv.egiz.pdfas.framework.input.DataSource#getAsByteArray() + */ + public byte[] getAsByteArray() + { + if (cache != null) + { + return cache; + } + + cache = new byte [getLength()]; + System.arraycopy(originalDataSource.getAsByteArray(), 0, cache, 0, originalDataSource.getLength()); + System.arraycopy(appendix, 0, cache, originalDataSource.getLength(), appendix.length); + + return cache; + } } diff --git a/src/main/java/at/gv/egiz/pdfas/impl/input/DelimitedPdfDataSource.java b/src/main/java/at/gv/egiz/pdfas/impl/input/DelimitedPdfDataSource.java index 6c67be2..63874c7 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/input/DelimitedPdfDataSource.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/input/DelimitedPdfDataSource.java @@ -41,4 +41,22 @@ public class DelimitedPdfDataSource implements PdfDataSource return this.len; } + byte [] cache = null; + + /** + * @see at.gv.egiz.pdfas.framework.input.DataSource#getAsByteArray() + */ + public byte[] getAsByteArray() + { + if (cache != null) + { + return cache; + } + + cache = new byte [getLength()]; + System.arraycopy(dataSource.getAsByteArray(), 0, cache, 0, getLength()); + + return cache; + } + } diff --git a/src/main/java/at/gv/egiz/pdfas/impl/input/FileBasedPdfDataSourceImpl.java b/src/main/java/at/gv/egiz/pdfas/impl/input/FileBasedPdfDataSourceImpl.java index 8453192..a3a0803 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/input/FileBasedPdfDataSourceImpl.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/input/FileBasedPdfDataSourceImpl.java @@ -3,6 +3,7 @@ */ package at.gv.egiz.pdfas.impl.input; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -13,6 +14,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import at.gv.egiz.pdfas.framework.input.PdfDataSource; +import at.gv.egiz.pdfas.impl.input.helper.DataSourceHelper; /** * @author wprinz @@ -77,6 +79,15 @@ public class FileBasedPdfDataSourceImpl implements PdfDataSource, FileBased * @see at.gv.egiz.pdfas.framework.input.PdfDataSource#createInputStream() */ public InputStream createInputStream() + { + if (cache == null) + { + getAsByteArray(); + } + return new ByteArrayInputStream(cache); + } + + protected InputStream createFileInputStream() { try { @@ -92,7 +103,6 @@ public class FileBasedPdfDataSourceImpl implements PdfDataSource, FileBased return null; } } - /** * @see at.gv.egiz.pdfas.framework.input.PdfDataSource#getLength() */ @@ -100,4 +110,22 @@ public class FileBasedPdfDataSourceImpl implements PdfDataSource, FileBased { return this.length; } + + byte [] cache = null; + + /** + * @see at.gv.egiz.pdfas.framework.input.DataSource#getAsByteArray() + */ + public byte[] getAsByteArray() + { + if (cache != null) + { + return cache; + } + + cache = DataSourceHelper.convertInputStreamToByteArray(createFileInputStream()); + + return cache; + } + } diff --git a/src/main/java/at/gv/egiz/pdfas/impl/input/FileBasedTextDataSourceImpl.java b/src/main/java/at/gv/egiz/pdfas/impl/input/FileBasedTextDataSourceImpl.java index 6f6c7b4..1988519 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/input/FileBasedTextDataSourceImpl.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/input/FileBasedTextDataSourceImpl.java @@ -13,6 +13,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import at.gv.egiz.pdfas.framework.input.TextDataSource; +import at.gv.egiz.pdfas.impl.input.helper.DataSourceHelper; /** * @author wprinz @@ -121,4 +122,21 @@ public class FileBasedTextDataSourceImpl implements TextDataSource, FileBased return (int) getFile().length(); } + byte [] cache = null; + + /** + * @see at.gv.egiz.pdfas.framework.input.DataSource#getAsByteArray() + */ + public byte[] getAsByteArray() + { + if (cache != null) + { + return cache; + } + + cache = DataSourceHelper.convertInputStreamToByteArray(createInputStream()); + + return cache; + } + } diff --git a/src/main/java/at/gv/egiz/pdfas/impl/input/TextDataSourceImpl.java b/src/main/java/at/gv/egiz/pdfas/impl/input/TextDataSourceImpl.java index b259a3e..c983a8a 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/input/TextDataSourceImpl.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/input/TextDataSourceImpl.java @@ -79,4 +79,20 @@ public class TextDataSourceImpl implements TextDataSource } } + /** + * @see at.gv.egiz.pdfas.framework.input.DataSource#getAsByteArray() + */ + public byte[] getAsByteArray() + { + try + { + byte[] data = getText().getBytes("UTF-8"); + return data; + } + catch (UnsupportedEncodingException e) + { + throw new RuntimeException(e); + } + } + } diff --git a/src/main/java/at/gv/egiz/pdfas/impl/input/helper/DataSourceHelper.java b/src/main/java/at/gv/egiz/pdfas/impl/input/helper/DataSourceHelper.java index 1e2ffdc..138b269 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/input/helper/DataSourceHelper.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/input/helper/DataSourceHelper.java @@ -3,6 +3,7 @@ */ package at.gv.egiz.pdfas.impl.input.helper; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -41,25 +42,55 @@ public class DataSourceHelper * @throws IOException */ public static byte[] convertDataSourceToByteArray(DataSource pdfDataSource) + { + return pdfDataSource.getAsByteArray(); +// try +// { +// PerformanceCounters.byteArrays.increment(); +// +// byte[] data = new byte[pdfDataSource.getLength()]; +// +// int bytes_written = 0; +// +// InputStream is = pdfDataSource.createInputStream(); +// int n = 0; +// while ((n = is.read(data, bytes_written, data.length - bytes_written)) > 0) +// { +// bytes_written += n; +// } +// is.close(); +// +// assert bytes_written == data.length; +// +// return data; +// } +// catch (IOException e) +// { +// log.error(e); +// throw new RuntimeException(e); +// } + } + + public static byte [] convertInputStreamToByteArray(InputStream inputStream) { try { PerformanceCounters.byteArrays.increment(); - byte[] data = new byte[pdfDataSource.getLength()]; + ByteArrayOutputStream baos = new ByteArrayOutputStream(4096); + + byte[] temp = new byte[4096]; - int bytes_written = 0; - - InputStream is = pdfDataSource.createInputStream(); int n = 0; - while ((n = is.read(data, bytes_written, data.length - bytes_written)) > 0) + while ((n = inputStream.read(temp)) > 0) { - bytes_written += n; + baos.write(temp, 0, n); } - is.close(); - - assert bytes_written == data.length; + inputStream.close(); + baos.close(); + byte [] data = baos.toByteArray(); + return data; } catch (IOException e) diff --git a/src/main/java/at/gv/egiz/pdfas/impl/output/ByteArrayDataSink.java b/src/main/java/at/gv/egiz/pdfas/impl/output/ByteArrayDataSink.java index f3d1283..3a3aeca 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/output/ByteArrayDataSink.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/output/ByteArrayDataSink.java @@ -47,6 +47,9 @@ public class ByteArrayDataSink implements DataSink */ public OutputStream createOutputStream(String mimeType, String characterEncoding) { + this.mimeType = mimeType; + this.characterEncoding = characterEncoding; + if (this.baos != null) { log.warn("An output stream is created twice. The old one will be rendered useless."); diff --git a/src/main/java/at/gv/egiz/pdfas/impl/signator/textual/TextualSignator_1_0_0.java b/src/main/java/at/gv/egiz/pdfas/impl/signator/textual/TextualSignator_1_0_0.java index b0494c8..1714afc 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/signator/textual/TextualSignator_1_0_0.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/signator/textual/TextualSignator_1_0_0.java @@ -91,7 +91,7 @@ public class TextualSignator_1_0_0 implements Signator tsi.profile = profile; tsi.pos = pos; - String document_text = PdfAS.extractNormalizedTextTextual(pdfDataSource.createInputStream()); + String document_text = PdfAS.extractNormalizedTextTextual(pdfDataSource); // logger_.debug("signed_text = " + document_text); DataSource ds = new TextDataSourceImpl(document_text); diff --git a/src/main/java/at/gv/egiz/pdfas/impl/vfilter/VerificationFilterImpl.java b/src/main/java/at/gv/egiz/pdfas/impl/vfilter/VerificationFilterImpl.java index 981b868..0c9e1f2 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/vfilter/VerificationFilterImpl.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/vfilter/VerificationFilterImpl.java @@ -18,7 +18,7 @@ import at.gv.egiz.pdfas.framework.input.TextDataSource; import at.gv.egiz.pdfas.framework.verificator.Verificator; import at.gv.egiz.pdfas.framework.vfilter.VerificationFilter; import at.gv.egiz.pdfas.framework.vfilter.VerificationFilterParameters; -import at.gv.egiz.pdfas.impl.input.DelimitedInputStream; +import at.gv.egiz.pdfas.impl.input.DelimitedPdfDataSource; import at.gv.egiz.pdfas.impl.input.helper.DataSourceHelper; import at.gv.egiz.pdfas.impl.vfilter.helper.VerificationFilterBinaryHelper; import at.gv.egiz.pdfas.impl.vfilter.helper.VerificationFilterHelper; @@ -285,8 +285,9 @@ public class VerificationFilterImpl implements VerificationFilter protected String extractText(PdfDataSource pdf, int endOfDocument) throws PresentableException { - DelimitedInputStream dis = new DelimitedInputStream(pdf.createInputStream(), endOfDocument); - return PdfAS.extractNormalizedTextTextual(dis); + DelimitedPdfDataSource dds = new DelimitedPdfDataSource(pdf, endOfDocument); + //DelimitedInputStream dis = new DelimitedInputStream(pdf.createInputStream(), endOfDocument); + return PdfAS.extractNormalizedTextTextual(dds); } protected List extractNewSignaturesFromText(String text) throws VerificationFilterException @@ -453,6 +454,18 @@ public class VerificationFilterImpl implements VerificationFilter log.debug("Extracting signatures:"); List extractedSignatures = extractNewSignaturesFromText(extractedText); log.debug("Extracting signatures finished."); + + if (log.isDebugEnabled()) + { + log.debug("extracted signatures:"); + for (int i = 0; i < extractedSignatures.size(); i++) + { + SignatureHolder sh = (SignatureHolder)extractedSignatures.get(i); + String dateStr = sh.getSignatureObject().getSignationDate(); + EGIZDate ed = EGIZDate.parseFromString(dateStr); + log.debug("#" + i + ": dateStr = " + dateStr + ", egizDate = " + ed.toString()); + } + } return extractedSignatures; } -- cgit v1.2.3