From 85e574618b04a34d5e41444d17ce7e6d5a93cc5b Mon Sep 17 00:00:00 2001 From: netconomy Date: Thu, 6 Sep 2007 12:18:45 +0000 Subject: Streaming Rückbau 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 ++++++++++++++++++---- 7 files changed, 155 insertions(+), 17 deletions(-) (limited to 'src/main/java/at/gv/egiz/pdfas/impl/input') 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) -- cgit v1.2.3