您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StreamCacheFactory.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.pdf;
  18. import java.io.IOException;
  19. /**
  20. * This class is serves as a factory from
  21. */
  22. public class StreamCacheFactory {
  23. private static boolean defaultCacheToFile = false;
  24. private static StreamCacheFactory fileInstance = null;
  25. private static StreamCacheFactory memoryInstance = null;
  26. private boolean cacheToFile = false;
  27. /**
  28. * Returns an instance of a StreamCacheFactory with the requested features.
  29. * @param cacheToFile True if file shall be cached using a temporary file
  30. * @return StreamCacheFactory the requested factory
  31. */
  32. public static StreamCacheFactory getInstance(boolean cacheToFile) {
  33. if (cacheToFile) {
  34. if (fileInstance == null) {
  35. fileInstance = new StreamCacheFactory(true);
  36. }
  37. return fileInstance;
  38. } else {
  39. if (memoryInstance == null) {
  40. memoryInstance = new StreamCacheFactory(false);
  41. }
  42. return memoryInstance;
  43. }
  44. }
  45. /**
  46. * Returns an instance of a StreamCacheFactory depending on the default
  47. * setting for cacheToFile.
  48. * @return StreamCacheFactory the requested factory
  49. */
  50. public static StreamCacheFactory getInstance() {
  51. return getInstance(defaultCacheToFile);
  52. }
  53. /**
  54. * Sets the global default for cacheToFile
  55. * @param cacheToFile True if stream caches should be held in files.
  56. */
  57. public static void setDefaultCacheToFile(boolean cacheToFile) {
  58. defaultCacheToFile = cacheToFile;
  59. }
  60. /**
  61. * Creates a new StreamCacheFactory.
  62. * @param cacheToFile True if file shall be cached using a temporary file
  63. */
  64. public StreamCacheFactory(boolean cacheToFile) {
  65. this.cacheToFile = cacheToFile;
  66. }
  67. /**
  68. * Get the correct implementation (based on cacheToFile) of
  69. * StreamCache.
  70. * @throws IOException if there is an IO error
  71. * @return a new StreamCache for caching streams
  72. */
  73. public StreamCache createStreamCache() throws IOException {
  74. if (this.cacheToFile) {
  75. return new TempFileStreamCache();
  76. } else {
  77. return new InMemoryStreamCache();
  78. }
  79. }
  80. /**
  81. * Get the correct implementation (based on cacheToFile) of
  82. * StreamCache.
  83. * @param hintSize a hint about the approximate expected size of the buffer
  84. * @throws IOException if there is an IO error
  85. * @return a new StreamCache for caching streams
  86. */
  87. public StreamCache createStreamCache(int hintSize) throws IOException {
  88. if (this.cacheToFile) {
  89. return new TempFileStreamCache();
  90. } else {
  91. return new InMemoryStreamCache(hintSize);
  92. }
  93. }
  94. /**
  95. * Get the value of the global cacheToFile flag.
  96. * @return the current cache to file flag
  97. */
  98. public boolean getCacheToFile() {
  99. return this.cacheToFile;
  100. }
  101. }