aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/pdfas/impl/input
diff options
context:
space:
mode:
authornetconomy <netconomy@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c>2007-09-06 12:18:45 +0000
committernetconomy <netconomy@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c>2007-09-06 12:18:45 +0000
commit85e574618b04a34d5e41444d17ce7e6d5a93cc5b (patch)
tree1720e8a11493b8175a927e7462687f8ec6848609 /src/main/java/at/gv/egiz/pdfas/impl/input
parent148948fa82b26a78b5c2b7d0a79777474b64e581 (diff)
downloadpdf-as-3-85e574618b04a34d5e41444d17ce7e6d5a93cc5b.tar.gz
pdf-as-3-85e574618b04a34d5e41444d17ce7e6d5a93cc5b.tar.bz2
pdf-as-3-85e574618b04a34d5e41444d17ce7e6d5a93cc5b.zip
Streaming Rückbau
git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@210 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c
Diffstat (limited to 'src/main/java/at/gv/egiz/pdfas/impl/input')
-rw-r--r--src/main/java/at/gv/egiz/pdfas/impl/input/ByteArrayPdfDataSourceImpl.java23
-rw-r--r--src/main/java/at/gv/egiz/pdfas/impl/input/CompoundPdfDataSourceImpl.java18
-rw-r--r--src/main/java/at/gv/egiz/pdfas/impl/input/DelimitedPdfDataSource.java18
-rw-r--r--src/main/java/at/gv/egiz/pdfas/impl/input/FileBasedPdfDataSourceImpl.java30
-rw-r--r--src/main/java/at/gv/egiz/pdfas/impl/input/FileBasedTextDataSourceImpl.java18
-rw-r--r--src/main/java/at/gv/egiz/pdfas/impl/input/TextDataSourceImpl.java16
-rw-r--r--src/main/java/at/gv/egiz/pdfas/impl/input/helper/DataSourceHelper.java49
7 files changed, 155 insertions, 17 deletions
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
@@ -78,6 +80,15 @@ public class FileBasedPdfDataSourceImpl implements PdfDataSource, FileBased
*/
public InputStream createInputStream()
{
+ if (cache == null)
+ {
+ getAsByteArray();
+ }
+ return new ByteArrayInputStream(cache);
+ }
+
+ protected InputStream createFileInputStream()
+ {
try
{
FileInputStream fis = new FileInputStream(getFile());
@@ -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;
@@ -42,24 +43,54 @@ public class DataSourceHelper
*/
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)