KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > pdf > TempFileStreamCache


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 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.pdf;
21
22 // Java
23
import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.File JavaDoc;
27
28 //Commons
29
import org.apache.commons.io.IOUtils;
30
31 /**
32  * StreamCache implementation that uses temporary files rather than heap.
33  */

34 public class TempFileStreamCache implements StreamCache {
35
36     /**
37      * The current output stream.
38      */

39     private OutputStream JavaDoc output;
40
41     /**
42      * The temp file.
43      */

44     private File JavaDoc tempFile;
45
46     /**
47      * Creates a new TempFileStreamCache.
48      *
49      * @throws IOException if there is an IO error
50      */

51     public TempFileStreamCache() throws IOException JavaDoc {
52         tempFile = File.createTempFile("org.apache.fop.pdf.StreamCache-",
53                                        ".temp");
54         tempFile.deleteOnExit();
55     }
56
57     /**
58      * Get the current OutputStream. Do not store it - it may change
59      * from call to call.
60      *
61      * @throws IOException if there is an IO error
62      * @return the output stream for this cache
63      */

64     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
65         if (output == null) {
66             output = new java.io.BufferedOutputStream JavaDoc(
67                        new java.io.FileOutputStream JavaDoc(tempFile));
68         }
69         return output;
70     }
71
72     /**
73      * @see org.apache.fop.pdf.StreamCache#write(byte[])
74      */

75     public void write(byte[] data) throws IOException JavaDoc {
76         getOutputStream().write(data);
77     }
78     
79     /**
80      * Outputs the cached bytes to the given stream.
81      *
82      * @param out the output stream to write to
83      * @return the number of bytes written
84      * @throws IOException if there is an IO error
85      */

86     public int outputContents(OutputStream JavaDoc out) throws IOException JavaDoc {
87         if (output == null) {
88             return 0;
89         }
90
91         output.close();
92         output = null;
93
94         // don't need a buffer because copy() is buffered
95
InputStream JavaDoc input = new java.io.FileInputStream JavaDoc(tempFile);
96         try {
97             return IOUtils.copy(input, out);
98         } finally {
99             IOUtils.closeQuietly(input);
100         }
101     }
102
103     /**
104      * Returns the current size of the stream.
105      *
106      * @throws IOException if there is an IO error
107      * @return the size of the cache
108      */

109     public int getSize() throws IOException JavaDoc {
110         if (output != null) {
111             output.flush();
112         }
113         return (int) tempFile.length();
114     }
115
116     /**
117      * Clears and resets the cache.
118      *
119      * @throws IOException if there is an IO error
120      */

121     public void clear() throws IOException JavaDoc {
122         if (output != null) {
123             output.close();
124             output = null;
125         }
126         if (tempFile.exists()) {
127             tempFile.delete();
128         }
129     }
130 }
131
Popular Tags