/**
* 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.
public InMemoryStreamCache() {
}
+ /**
+ * 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.
*/
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
/**
* 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();
}
/**
}
}
- /**
- * 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;
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;
}
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;
/**
* 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.
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.
*
/**
* 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;
}
/**
return (int) tempFile.length();
}
- /**
- * 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;