1 16 17 18 package org.apache.commons.io.output; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 26 36 public class DeferredFileOutputStream 37 extends ThresholdingOutputStream 38 { 39 40 42 43 47 private ByteArrayOutputStream memoryOutputStream; 48 49 50 54 private FileOutputStream diskOutputStream; 55 56 57 62 private OutputStream currentOutputStream; 63 64 65 68 private File outputFile; 69 70 71 73 74 81 public DeferredFileOutputStream(int threshold, File outputFile) 82 { 83 super(threshold); 84 this.outputFile = outputFile; 85 86 memoryOutputStream = new ByteArrayOutputStream(threshold); 87 currentOutputStream = memoryOutputStream; 88 } 89 90 91 93 94 102 protected OutputStream getStream() throws IOException 103 { 104 return currentOutputStream; 105 } 106 107 108 116 protected void thresholdReached() throws IOException 117 { 118 byte[] data = memoryOutputStream.toByteArray(); 119 FileOutputStream fos = new FileOutputStream (outputFile); 120 fos.write(data); 121 diskOutputStream = fos; 122 currentOutputStream = fos; 123 memoryOutputStream = null; 124 } 125 126 127 129 130 137 public boolean isInMemory() 138 { 139 return (!isThresholdExceeded()); 140 } 141 142 143 151 public byte[] getData() 152 { 153 if (memoryOutputStream != null) 154 { 155 return memoryOutputStream.toByteArray(); 156 } 157 return null; 158 } 159 160 161 169 public File getFile() 170 { 171 return outputFile; 172 } 173 } 174 | Popular Tags |