aboutsummaryrefslogtreecommitdiffstats
path: root/src/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/java')
-rw-r--r--src/java/org/apache/fop/pdf/InMemoryStreamCache.java48
-rw-r--r--src/java/org/apache/fop/pdf/StreamCache.java73
-rw-r--r--src/java/org/apache/fop/pdf/TempFileStreamCache.java41
3 files changed, 62 insertions, 100 deletions
diff --git a/src/java/org/apache/fop/pdf/InMemoryStreamCache.java b/src/java/org/apache/fop/pdf/InMemoryStreamCache.java
index bec8c6a58..ab464184f 100644
--- a/src/java/org/apache/fop/pdf/InMemoryStreamCache.java
+++ b/src/java/org/apache/fop/pdf/InMemoryStreamCache.java
@@ -58,7 +58,9 @@ import java.io.IOException;
/**
* StreamCache implementation that uses temporary files rather than heap.
*/
-public class InMemoryStreamCache extends StreamCache {
+public class InMemoryStreamCache implements StreamCache {
+
+ private int hintSize = -1;
/**
* The current output stream.
@@ -72,6 +74,14 @@ public class InMemoryStreamCache extends StreamCache {
}
/**
+ * Creates a new InMemoryStreamCache.
+ * @param hintSize a hint about the approximate expected size of the buffer
+ */
+ public InMemoryStreamCache(int hintSize) {
+ this.hintSize = hintSize;
+ }
+
+ /**
* Get the current OutputStream. Do not store it - it may change
* from call to call.
* @throws IOException if there is an error getting the output stream
@@ -79,12 +89,23 @@ public class InMemoryStreamCache extends StreamCache {
*/
public OutputStream getOutputStream() throws IOException {
if (output == null) {
- output = new ByteArrayOutputStream();
+ if (this.hintSize <= 0) {
+ output = new ByteArrayOutputStream(512);
+ } else {
+ output = new ByteArrayOutputStream(this.hintSize);
+ }
}
return output;
}
/**
+ * @see org.apache.fop.pdf.StreamCache#write(byte[])
+ */
+ public void write(byte[] data) throws IOException {
+ getOutputStream().write(data);
+ }
+
+ /**
* Filter the cache with the supplied PDFFilter.
* @param filter the filter to apply
* @throws IOException if an IO error occurs
@@ -112,15 +133,17 @@ public class InMemoryStreamCache extends StreamCache {
/**
* Outputs the cached bytes to the given stream.
- * @param stream the output stream to write to
+ * @param out the output stream to write to
+ * @return the number of bytes written
* @throws IOException if there is an IO error writing to the output stream
*/
- public void outputStreamData(OutputStream stream) throws IOException {
+ public int outputContents(OutputStream out) throws IOException {
if (output == null) {
- return;
+ return 0;
}
- output.writeTo(stream);
+ output.writeTo(out);
+ return output.size();
}
/**
@@ -137,21 +160,10 @@ public class InMemoryStreamCache extends StreamCache {
}
/**
- * Closes the cache and frees resources.
- * @throws IOException if there is an error closing the stream
- */
- public void close() throws IOException {
- if (output != null) {
- output.close();
- output = null;
- }
- }
-
- /**
* Clears and resets the cache.
* @throws IOException if there is an error closing the stream
*/
- public void reset() throws IOException {
+ public void clear() throws IOException {
if (output != null) {
output.close();
output = null;
diff --git a/src/java/org/apache/fop/pdf/StreamCache.java b/src/java/org/apache/fop/pdf/StreamCache.java
index 5eb73b9e8..811d53e2d 100644
--- a/src/java/org/apache/fop/pdf/StreamCache.java
+++ b/src/java/org/apache/fop/pdf/StreamCache.java
@@ -54,96 +54,51 @@ import java.io.OutputStream;
import java.io.IOException;
/**
- * class used to store the bytes for a PDFStream. It's actually a generic
- * cached byte array, along with a factory that returns either an
- * in-memory or tempfile based implementation based on the global
+ * Interface used to store the bytes for a PDFStream. It's actually a generic
+ * cached byte array. There's a factory that returns either an
+ * in-memory or tempfile based implementation based on a
* cacheToFile setting.
*/
-public abstract class StreamCache {
-
- /**
- * Global setting; controls whether to use tempfiles or not.
- */
- private static boolean cacheToFile = false;
-
- /**
- * Change the global cacheToFile flag.
- *
- * @param tizit true if cache to file
- */
- public static void setCacheToFile(boolean tizit) {
- cacheToFile = tizit;
- }
-
- /**
- * Get the value of the global cacheToFile flag.
- *
- * @return the current cache to file flag
- */
- public static boolean getCacheToFile() {
- return cacheToFile;
- }
-
- /**
- * Get the correct implementation (based on cacheToFile) of
- * StreamCache.
- *
- * @throws IOException if there is an IO error
- * @return a new StreamCache for caching streams
- */
- public static StreamCache createStreamCache() throws IOException {
- if (cacheToFile) {
- return new TempFileStreamCache();
- } else {
- return new InMemoryStreamCache();
- }
- }
+public interface StreamCache {
/**
* Get the current OutputStream. Do not store it - it may change
* from call to call.
*
- * @throws IOException if there is an IO error
* @return an output stream for this cache
+ * @throws IOException if there is an IO error
*/
- public abstract OutputStream getOutputStream() throws IOException;
+ OutputStream getOutputStream() throws IOException;
/**
- * Filter the cache with the supplied PDFFilter.
- *
- * @param filter the filter to apply
+ * Convenience method for writing data to the stream cache.
+ * @param data byte array to write
* @throws IOException if there is an IO error
*/
- public abstract void applyFilter(PDFFilter filter) throws IOException;
+ void write(byte[] data) throws IOException;
/**
* Outputs the cached bytes to the given stream.
*
- * @param stream the stream to write to
+ * @param out the stream to write to
+ * @return the number of bytes written
* @throws IOException if there is an IO error
*/
- public abstract void outputStreamData(OutputStream stream) throws IOException;
+ int outputContents(OutputStream out) throws IOException;
/**
* Returns the current size of the stream.
*
- * @throws IOException if there is an IO error
* @return the size of the cache
- */
- public abstract int getSize() throws IOException;
-
- /**
- * Closes the cache and frees resources.
- *
* @throws IOException if there is an IO error
*/
- public abstract void close() throws IOException;
+ int getSize() throws IOException;
/**
* Clears and resets the cache.
*
* @throws IOException if there is an IO error
*/
- public abstract void reset() throws IOException;
+ void clear() throws IOException;
}
diff --git a/src/java/org/apache/fop/pdf/TempFileStreamCache.java b/src/java/org/apache/fop/pdf/TempFileStreamCache.java
index 3c23bb9c4..e02a65c00 100644
--- a/src/java/org/apache/fop/pdf/TempFileStreamCache.java
+++ b/src/java/org/apache/fop/pdf/TempFileStreamCache.java
@@ -56,6 +56,7 @@ import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.File;
@@ -63,12 +64,12 @@ import java.io.File;
/**
* StreamCache implementation that uses temporary files rather than heap.
*/
-public class TempFileStreamCache extends StreamCache {
+public class TempFileStreamCache implements StreamCache {
/**
* The current output stream.
*/
- private BufferedOutputStream output;
+ private OutputStream output;
/**
* The temp file.
@@ -102,6 +103,13 @@ public class TempFileStreamCache extends StreamCache {
}
/**
+ * @see org.apache.fop.pdf.StreamCache#write(byte[])
+ */
+ public void write(byte[] data) throws IOException {
+ getOutputStream().write(data);
+ }
+
+ /**
* Filter the cache with the supplied PDFFilter.
*
* @param filter the filter to apply
@@ -136,21 +144,23 @@ public class TempFileStreamCache extends StreamCache {
/**
* Outputs the cached bytes to the given stream.
*
- * @param stream the output stream to write to
+ * @param out the output stream to write to
+ * @return the number of bytes written
* @throws IOException if there is an IO error
*/
- public void outputStreamData(OutputStream stream) throws IOException {
+ public int outputContents(OutputStream out) throws IOException {
if (output == null) {
- return;
+ return 0;
}
output.close();
output = null;
// don't need a buffer because streamCopy is buffered
- FileInputStream input = new FileInputStream(tempFile);
- StreamUtilities.streamCopy(input, output);
+ InputStream input = new java.io.FileInputStream(tempFile);
+ final long bytesCopied = StreamUtilities.streamCopy(input, out);
input.close();
+ return (int)bytesCopied;
}
/**
@@ -167,26 +177,11 @@ public class TempFileStreamCache extends StreamCache {
}
/**
- * Closes the cache and frees resources.
- *
- * @throws IOException if there is an IO error
- */
- public void close() throws IOException {
- if (output != null) {
- output.close();
- output = null;
- }
- if (tempFile.exists()) {
- tempFile.delete();
- }
- }
-
- /**
* Clears and resets the cache.
*
* @throws IOException if there is an IO error
*/
- public void reset() throws IOException {
+ public void clear() throws IOException {
if (output != null) {
output.close();
output = null;