KickJava   Java API By Example, From Geeks To Geeks.

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


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: StreamCacheFactory.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.pdf;
21
22 import java.io.IOException JavaDoc;
23
24 /**
25  * This class is serves as a factory from
26  */

27 public class StreamCacheFactory {
28
29     private static boolean defaultCacheToFile = false;
30     private static StreamCacheFactory fileInstance = null;
31     private static StreamCacheFactory memoryInstance = null;
32
33     private boolean cacheToFile = false;
34     
35     /**
36      * Returns an instance of a StreamCacheFactory with the requested features.
37      * @param cacheToFile True if file shall be cached using a temporary file
38      * @return StreamCacheFactory the requested factory
39      */

40     public static StreamCacheFactory getInstance(boolean cacheToFile) {
41         if (cacheToFile) {
42             if (fileInstance == null) {
43                 fileInstance = new StreamCacheFactory(true);
44             }
45             return fileInstance;
46         } else {
47             if (memoryInstance == null) {
48                 memoryInstance = new StreamCacheFactory(false);
49             }
50             return memoryInstance;
51         }
52     }
53     
54     /**
55      * Returns an instance of a StreamCacheFactory depending on the default
56      * setting for cacheToFile.
57      * @return StreamCacheFactory the requested factory
58      */

59     public static StreamCacheFactory getInstance() {
60         return getInstance(defaultCacheToFile);
61     }
62     
63     /**
64      * Sets the global default for cacheToFile
65      * @param cacheToFile True if stream caches should be held in files.
66      */

67     public static void setDefaultCacheToFile(boolean cacheToFile) {
68         defaultCacheToFile = cacheToFile;
69     }
70
71     /**
72      * Creates a new StreamCacheFactory.
73      * @param cacheToFile True if file shall be cached using a temporary file
74      */

75     public StreamCacheFactory(boolean cacheToFile) {
76         this.cacheToFile = cacheToFile;
77     }
78     
79     /**
80      * Get the correct implementation (based on cacheToFile) of
81      * StreamCache.
82      * @throws IOException if there is an IO error
83      * @return a new StreamCache for caching streams
84      */

85     public StreamCache createStreamCache() throws IOException JavaDoc {
86         if (this.cacheToFile) {
87             return new TempFileStreamCache();
88         } else {
89             return new InMemoryStreamCache();
90         }
91     }
92     
93     /**
94      * Get the correct implementation (based on cacheToFile) of
95      * StreamCache.
96      * @param hintSize a hint about the approximate expected size of the buffer
97      * @throws IOException if there is an IO error
98      * @return a new StreamCache for caching streams
99      */

100     public StreamCache createStreamCache(int hintSize) throws IOException JavaDoc {
101         if (this.cacheToFile) {
102             return new TempFileStreamCache();
103         } else {
104             return new InMemoryStreamCache(hintSize);
105         }
106     }
107     
108     /**
109      * Get the value of the global cacheToFile flag.
110      * @return the current cache to file flag
111      */

112     public boolean getCacheToFile() {
113         return this.cacheToFile;
114     }
115     
116
117 }
118
Popular Tags