1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id: TempFileStreamCache.java 1296526 2012-03-03 00:18:45Z gadams $ */
19 
20 package org.apache.fop.pdf;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 
27 import org.apache.commons.io.IOUtils;
28 
29 /**
30  * StreamCache implementation that uses temporary files rather than heap.
31  */
32 public class TempFileStreamCache implements StreamCache {
33 
34     /**
35      * The current output stream.
36      */
37     private OutputStream output;
38 
39     /**
40      * The temp file.
41      */
42     private File tempFile;
43 
44     /**
45      * Creates a new TempFileStreamCache.
46      *
47      * @throws IOException if there is an IO error
48      */
TempFileStreamCache()49     public TempFileStreamCache() throws IOException {
50         tempFile = File.createTempFile("org.apache.fop.pdf.StreamCache-",
51                                        ".temp");
52         tempFile.deleteOnExit();
53     }
54 
55     /**
56      * Get the current OutputStream. Do not store it - it may change
57      * from call to call.
58      *
59      * @throws IOException if there is an IO error
60      * @return the output stream for this cache
61      */
getOutputStream()62     public OutputStream getOutputStream() throws IOException {
63         if (output == null) {
64             output = new java.io.BufferedOutputStream(
65                        new java.io.FileOutputStream(tempFile));
66         }
67         return output;
68     }
69 
70     /**
71      * {@inheritDoc}
72      */
write(byte[] data)73     public void write(byte[] data) throws IOException {
74         getOutputStream().write(data);
75     }
76 
77     /**
78      * Outputs the cached bytes to the given stream.
79      *
80      * @param out the output stream to write to
81      * @return the number of bytes written
82      * @throws IOException if there is an IO error
83      */
outputContents(OutputStream out)84     public int outputContents(OutputStream out) throws IOException {
85         if (output == null) {
86             return 0;
87         }
88 
89         output.close();
90         output = null;
91 
92         // don't need a buffer because copy() is buffered
93         InputStream input = new java.io.FileInputStream(tempFile);
94         try {
95             return IOUtils.copy(input, out);
96         } finally {
97             IOUtils.closeQuietly(input);
98         }
99     }
100 
101     /**
102      * Returns the current size of the stream.
103      *
104      * @throws IOException if there is an IO error
105      * @return the size of the cache
106      */
getSize()107     public int getSize() throws IOException {
108         if (output != null) {
109             output.flush();
110         }
111         return (int) tempFile.length();
112     }
113 
114     /**
115      * Clears and resets the cache.
116      *
117      * @throws IOException if there is an IO error
118      */
clear()119     public void clear() throws IOException {
120         if (output != null) {
121             output.close();
122             output = null;
123         }
124         if (tempFile.exists()) {
125             tempFile.delete();
126         }
127     }
128 }
129