aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/pdfas/impl/input/helper/DataSourceHelper.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/gv/egiz/pdfas/impl/input/helper/DataSourceHelper.java')
-rw-r--r--src/main/java/at/gv/egiz/pdfas/impl/input/helper/DataSourceHelper.java92
1 files changed, 92 insertions, 0 deletions
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
new file mode 100644
index 0000000..1e2ffdc
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/impl/input/helper/DataSourceHelper.java
@@ -0,0 +1,92 @@
+/**
+ *
+ */
+package at.gv.egiz.pdfas.impl.input.helper;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import at.gv.egiz.pdfas.performance.PerformanceCounters;
+import at.gv.egiz.pdfas.framework.input.DataSource;
+import at.gv.egiz.pdfas.framework.input.PdfDataSource;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @author wprinz
+ *
+ */
+public class DataSourceHelper
+{
+ /**
+ * The log.
+ */
+ private static final Log log = LogFactory.getLog(DataSourceHelper.class);
+
+ /**
+ * Converts a PdfDataSource to a byte array.
+ *
+ * <p>
+ * Note that this function is very memory intensive. Use the Streams whereever
+ * possible.
+ * </p>
+ *
+ * @deprecated
+ *
+ * @param pdfDataSource
+ * @return
+ * @throws IOException
+ */
+ public static byte[] convertDataSourceToByteArray(DataSource pdfDataSource)
+ {
+ 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 void debugDataSourceToFile(DataSource dataSource, File file)
+ {
+ try
+ {
+ InputStream is = dataSource.createInputStream();
+ FileOutputStream fos = new FileOutputStream(file);
+ byte[] data = new byte[2048];
+ int n = -1;
+ while ((n = is.read(data)) > 0)
+ {
+ fos.write(data, 0, n);
+ }
+ is.close();
+ fos.close();
+ }
+ catch (IOException e)
+ {
+ log.error(e);
+ }
+ }
+}