1 61 62 63 package org.apache.commons.fileupload; 64 65 import java.io.ByteArrayOutputStream ; 66 import java.io.File ; 67 import java.io.FileOutputStream ; 68 import java.io.IOException ; 69 import java.io.OutputStream ; 70 71 81 public class DeferredFileOutputStream 82 extends ThresholdingOutputStream 83 { 84 85 87 88 92 private ByteArrayOutputStream memoryOutputStream; 93 94 95 99 private FileOutputStream diskOutputStream; 100 101 102 107 private OutputStream currentOutputStream; 108 109 110 113 private File outputFile; 114 115 116 118 119 126 public DeferredFileOutputStream(int threshold, File outputFile) 127 { 128 super(threshold); 129 this.outputFile = outputFile; 130 131 memoryOutputStream = new ByteArrayOutputStream (threshold); 132 currentOutputStream = memoryOutputStream; 133 } 134 135 136 138 139 147 protected OutputStream getStream() throws IOException 148 { 149 return currentOutputStream; 150 } 151 152 153 161 protected void thresholdReached() throws IOException 162 { 163 byte[] data = memoryOutputStream.toByteArray(); 164 FileOutputStream fos = new FileOutputStream (outputFile); 165 fos.write(data); 166 diskOutputStream = fos; 167 currentOutputStream = fos; 168 memoryOutputStream = null; 169 } 170 171 172 174 175 182 public boolean isInMemory() 183 { 184 return (!isThresholdExceeded()); 185 } 186 187 188 196 public byte[] getData() 197 { 198 if (memoryOutputStream != null) 199 { 200 return memoryOutputStream.toByteArray(); 201 } 202 return null; 203 } 204 205 206 214 public File getFile() 215 { 216 return outputFile; 217 } 218 } 219 | Popular Tags |