1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.backsource.utils.io;
21 import java.io.File;
22 import java.io.IOException;
23 /***
24 * A StreamCache that cached data in temporary files.
25 *
26 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
27 * @version $Revision: 1.1.1.1 $
28 */
29
30 public class FileCachedStream extends ByteCachedStream{
31 protected static final String TMP_PREFIX ="FileCached";
32 protected static final String TMP_SUFIX =".tmp";
33
34 public FileCachedStream (){
35
36
37 }
38 /***
39 * Get a CachedOutputStream.
40 *
41 * <p>The typical usecase for this is to later use it to get
42 * a CachedInputStream from it.
43 */
44 public CachedOutputStream getOutputStream() throws IOException {
45 File f = File.createTempFile(TMP_PREFIX,TMP_SUFIX);
46 f.deleteOnExit();
47 return new FileCachedOutputStream(f);
48 }
49
50 /***
51 * Get a rewindable input stream from the given CachedOutputStream.
52 */
53 public CachedInputStream getInputStream(CachedOutputStream cout)throws IOException {
54 if ( cout instanceof FileCachedOutputStream ) {
55 return new FileCachedInputStream((FileCachedOutputStream)cout);
56 }
57 throw new IOException("The CachedOutputStream is not retrieved through this cache; it is not a FileCachedOutputStream");
58 }
59
60 }