You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2003/03/27 11:27:27 UTC
cvs commit: xml-fop/src/java/org/apache/fop/pdf TempFileStreamCache.java InMemoryStreamCache.java StreamCache.java
jeremias 2003/03/27 02:27:27
Modified: src/java/org/apache/fop/pdf TempFileStreamCache.java
InMemoryStreamCache.java StreamCache.java
Log:
Streamlined. Do some simple memory allocation optimization.
Revision Changes Path
1.2 +18 -23 xml-fop/src/java/org/apache/fop/pdf/TempFileStreamCache.java
Index: TempFileStreamCache.java
===================================================================
RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/pdf/TempFileStreamCache.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TempFileStreamCache.java 11 Mar 2003 13:05:09 -0000 1.1
+++ TempFileStreamCache.java 27 Mar 2003 10:27:27 -0000 1.2
@@ -56,6 +56,7 @@
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 @@
/**
* 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 @@
}
/**
+ * @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 @@
/**
* 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 @@
}
/**
- * 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;
1.2 +30 -18 xml-fop/src/java/org/apache/fop/pdf/InMemoryStreamCache.java
Index: InMemoryStreamCache.java
===================================================================
RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/pdf/InMemoryStreamCache.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- InMemoryStreamCache.java 11 Mar 2003 13:05:09 -0000 1.1
+++ InMemoryStreamCache.java 27 Mar 2003 10:27:27 -0000 1.2
@@ -58,7 +58,9 @@
/**
* 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 @@
}
/**
+ * 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 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 @@
/**
* 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 @@
}
/**
- * 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;
1.2 +14 -59 xml-fop/src/java/org/apache/fop/pdf/StreamCache.java
Index: StreamCache.java
===================================================================
RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/pdf/StreamCache.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- StreamCache.java 11 Mar 2003 13:05:09 -0000 1.1
+++ StreamCache.java 27 Mar 2003 10:27:27 -0000 1.2
@@ -54,96 +54,51 @@
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;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: fop-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-cvs-help@xml.apache.org