1 package com.quadcap.io; 2 3 40 41 import java.io.ByteArrayInputStream ; 42 import java.io.ByteArrayOutputStream ; 43 import java.io.File ; 44 import java.io.FileInputStream ; 45 import java.io.FileNotFoundException ; 46 import java.io.FileOutputStream ; 47 import java.io.InputStream ; 48 import java.io.IOException ; 49 import java.io.OutputStream ; 50 51 58 public class SaveRestoreStream { 59 ByteArrayOutputStream bos = null; 60 FileOutputStream fos = null; 61 File file = null; 62 int len = 0; 63 int max = 4096; 64 65 68 public SaveRestoreStream() { 69 } 70 71 77 public SaveRestoreStream(int max) { 78 this.max = max; 79 } 80 81 87 public SaveRestoreStream(InputStream in) throws IOException { 88 OutputStream out = getOutputStream(); 89 try { 90 IO.copyStream(in, out); 91 } finally { 92 out.close(); 93 } 94 } 95 96 101 public OutputStream getOutputStream() throws IOException { 102 if (file == null) { 103 bos = new ByteArrayOutputStream (); 104 if (fos != null) fos.close(); 105 fos = null; 106 } else { 107 bos = null; 108 fos = new FileOutputStream (file); 109 } 110 111 return new OutputStream () { 112 public void write(int c) throws IOException { 113 doWrite(c); 114 } 115 public void flush() throws IOException { 116 if (bos != null) bos.flush(); 117 else fos.flush(); 118 } 119 public void close() throws IOException { 120 if (bos != null) { 121 len = bos.size(); 122 } else if (fos != null) { 123 fos.close(); 124 fos = null; 125 len = (int)file.length(); 126 } 127 } 128 }; 129 } 130 131 public int length() { 132 return len; 133 } 134 135 static int tmpFileCount = 0; 136 137 static synchronized File tmpFile() throws IOException { 138 return File.createTempFile("" + System.currentTimeMillis() + "." + 139 (tmpFileCount++), ".tmp"); 140 } 141 142 148 public void doWrite(int c) throws IOException { 149 if (bos != null) { 150 if (bos.size() < max) { 151 bos.write(c); 152 } else { 153 file = tmpFile(); 154 fos = new FileOutputStream (file); 155 bos.writeTo(fos); 156 fos.write(c); 157 bos.close(); 158 bos = null; 159 } 160 } else { 161 fos.write(c); 162 } 163 } 164 165 170 public InputStream getInputStream() throws IOException { 171 return getInputStream(true); 172 } 173 174 public InputStream getInputStream(final boolean del) throws IOException { 175 if (bos != null) return new ByteArrayInputStream (bos.toByteArray()); 176 if (fos != null) { 177 fos.close(); 178 fos = null; 179 } 180 if (file != null) { 181 final FileInputStream fis = new FileInputStream (file); 182 final File f = file; 183 if (del) file = null; 184 return new InputStream () { 185 public int read() throws IOException { 186 return fis.read(); 187 } 188 public int read(byte[] buf) throws IOException { 189 return fis.read(buf); 190 } 191 public int read(byte[] buf, int off, int cnt) throws IOException { 192 return fis.read(buf, off, cnt); 193 } 194 public void close() throws IOException { 195 fis.close(); 196 if (del) f.delete(); 197 } 198 public void finalize() { 199 try { 200 close(); 201 } catch (Throwable t) {} 202 } 203 }; 204 } 205 throw new IOException ("no data"); 206 } 207 208 211 public String toString() { 212 if (bos != null) return bos.toString(); 213 if (fos != null) { 214 throw new RuntimeException ("too large"); 215 } 216 return null; 217 } 218 219 public void close() { 220 bos = null; 221 if (fos != null) { 222 try { 223 fos.close(); 224 } catch (Exception e) { 225 } 226 } 227 fos = null; 228 if (file != null) { 229 try { 230 file.delete(); 231 } catch (Exception e) { 232 } 233 } 234 file = null; 235 len = 0; 236 } 237 238 public void reset() { 239 close(); 240 } 241 242 public void finalize() { 243 close(); 244 } 245 } 246 | Popular Tags |