You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TempFileStreamCache.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // Java
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.io.IOException;
  22. import java.io.File;
  23. //Commons
  24. import org.apache.commons.io.CopyUtils;
  25. /**
  26. * StreamCache implementation that uses temporary files rather than heap.
  27. */
  28. public class TempFileStreamCache implements StreamCache {
  29. /**
  30. * The current output stream.
  31. */
  32. private OutputStream output;
  33. /**
  34. * The temp file.
  35. */
  36. private File tempFile;
  37. /**
  38. * Creates a new TempFileStreamCache.
  39. *
  40. * @throws IOException if there is an IO error
  41. */
  42. public TempFileStreamCache() throws IOException {
  43. tempFile = File.createTempFile("org.apache.fop.pdf.StreamCache-",
  44. ".temp");
  45. tempFile.deleteOnExit();
  46. }
  47. /**
  48. * Get the current OutputStream. Do not store it - it may change
  49. * from call to call.
  50. *
  51. * @throws IOException if there is an IO error
  52. * @return the output stream for this cache
  53. */
  54. public OutputStream getOutputStream() throws IOException {
  55. if (output == null) {
  56. output = new java.io.BufferedOutputStream(
  57. new java.io.FileOutputStream(tempFile));
  58. }
  59. return output;
  60. }
  61. /**
  62. * @see org.apache.fop.pdf.StreamCache#write(byte[])
  63. */
  64. public void write(byte[] data) throws IOException {
  65. getOutputStream().write(data);
  66. }
  67. /**
  68. * Outputs the cached bytes to the given stream.
  69. *
  70. * @param out the output stream to write to
  71. * @return the number of bytes written
  72. * @throws IOException if there is an IO error
  73. */
  74. public int outputContents(OutputStream out) throws IOException {
  75. if (output == null) {
  76. return 0;
  77. }
  78. output.close();
  79. output = null;
  80. // don't need a buffer because streamCopy is buffered
  81. InputStream input = new java.io.FileInputStream(tempFile);
  82. final long bytesCopied = CopyUtils.copy(input, out);
  83. input.close();
  84. return (int)bytesCopied;
  85. }
  86. /**
  87. * Returns the current size of the stream.
  88. *
  89. * @throws IOException if there is an IO error
  90. * @return the size of the cache
  91. */
  92. public int getSize() throws IOException {
  93. if (output != null) {
  94. output.flush();
  95. }
  96. return (int) tempFile.length();
  97. }
  98. /**
  99. * Clears and resets the cache.
  100. *
  101. * @throws IOException if there is an IO error
  102. */
  103. public void clear() throws IOException {
  104. if (output != null) {
  105. output.close();
  106. output = null;
  107. }
  108. if (tempFile.exists()) {
  109. tempFile.delete();
  110. }
  111. }
  112. }