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.5KB

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