1 17 18 19 20 package org.apache.fop.pdf; 21 22 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.io.IOException ; 26 import java.io.File ; 27 28 import org.apache.commons.io.IOUtils; 30 31 34 public class TempFileStreamCache implements StreamCache { 35 36 39 private OutputStream output; 40 41 44 private File tempFile; 45 46 51 public TempFileStreamCache() throws IOException { 52 tempFile = File.createTempFile("org.apache.fop.pdf.StreamCache-", 53 ".temp"); 54 tempFile.deleteOnExit(); 55 } 56 57 64 public OutputStream getOutputStream() throws IOException { 65 if (output == null) { 66 output = new java.io.BufferedOutputStream ( 67 new java.io.FileOutputStream (tempFile)); 68 } 69 return output; 70 } 71 72 75 public void write(byte[] data) throws IOException { 76 getOutputStream().write(data); 77 } 78 79 86 public int outputContents(OutputStream out) throws IOException { 87 if (output == null) { 88 return 0; 89 } 90 91 output.close(); 92 output = null; 93 94 InputStream input = new java.io.FileInputStream (tempFile); 96 try { 97 return IOUtils.copy(input, out); 98 } finally { 99 IOUtils.closeQuietly(input); 100 } 101 } 102 103 109 public int getSize() throws IOException { 110 if (output != null) { 111 output.flush(); 112 } 113 return (int) tempFile.length(); 114 } 115 116 121 public void clear() throws IOException { 122 if (output != null) { 123 output.close(); 124 output = null; 125 } 126 if (tempFile.exists()) { 127 tempFile.delete(); 128 } 129 } 130 } 131 | Popular Tags |