1 17 18 19 package org.apache.tomcat.util.http.fileupload; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 27 37 public class DeferredFileOutputStream 38 extends ThresholdingOutputStream 39 { 40 41 43 44 48 private ByteArrayOutputStream memoryOutputStream; 49 50 51 55 private FileOutputStream diskOutputStream; 56 57 58 63 private OutputStream currentOutputStream; 64 65 66 69 private File outputFile; 70 71 72 74 75 82 public DeferredFileOutputStream(int threshold, File outputFile) 83 { 84 super(threshold); 85 this.outputFile = outputFile; 86 87 memoryOutputStream = new ByteArrayOutputStream (threshold); 88 currentOutputStream = memoryOutputStream; 89 } 90 91 92 94 95 103 protected OutputStream getStream() throws IOException 104 { 105 return currentOutputStream; 106 } 107 108 109 117 protected void thresholdReached() throws IOException 118 { 119 byte[] data = memoryOutputStream.toByteArray(); 120 FileOutputStream fos = new FileOutputStream (outputFile); 121 fos.write(data); 122 diskOutputStream = fos; 123 currentOutputStream = fos; 124 memoryOutputStream = null; 125 } 126 127 128 130 131 138 public boolean isInMemory() 139 { 140 return (!isThresholdExceeded()); 141 } 142 143 144 152 public byte[] getData() 153 { 154 if (memoryOutputStream != null) 155 { 156 return memoryOutputStream.toByteArray(); 157 } 158 return null; 159 } 160 161 162 170 public File getFile() 171 { 172 return outputFile; 173 } 174 } 175 | Popular Tags |