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.InputStream;
22 import java.io.IOException;
23 /***
24 * A CachedStream that returnes streams that are cached in memory in a byte array.
25 *
26 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
27 * @version $Revision: 1.1.1.1 $
28 */
29
30 public class ByteCachedStream implements CachedStream{
31 public ByteCachedStream (){
32
33 }
34
35 /***
36 * Get a CachedOutputStream.
37 *
38 * <p>The typical usecase for this is to later use it to get
39 * a CachedInputStream from it.
40 */
41 public CachedOutputStream getOutputStream() throws IOException {
42 return new ByteCachedOutputStream();
43 }
44
45 /***
46 * Get a rewindable input stream from the given CachedOutputStream.
47 */
48 public CachedInputStream getInputStream(CachedOutputStream cout)throws IOException {
49 if ( cout instanceof ByteCachedOutputStream ) {
50 return new ByteCachedInputStream((ByteCachedOutputStream)cout);
51 }
52 throw new IOException("The CachedOutputStream is not retrieved through this cache; it is not a ByteCachedInputStream");
53 }
54
55 /***
56 * Get a rewindable inputstream from the given input stream, if the stream is a Rewindable input stream, it is just returned.
57 */
58 public CachedInputStream getInputStream(InputStream is)throws IOException {
59 if ( is instanceof CachedInputStream) {
60 return (CachedInputStream)is;
61 } else {
62 CachedOutputStream cout = getOutputStream();
63 IOHelper.connect(is,cout);
64 return getInputStream(cout);
65 }
66
67 }
68
69 /***
70 * Rewind this stream if it is rewindable, otherwise throw an IOException.
71 */
72 public void rewind(InputStream is) throws IOException {
73 if ( is instanceof CachedInputStream) {
74 ((CachedInputStream)is).rewind();
75 } else {
76 throw new IOException("Can not rewind: Stream is not a CacheInputStream");
77 }
78
79
80 }
81 /***
82 * Empty implementation.
83 */
84 public void setProperty(String name, String value) {
85
86 }
87 /***
88 * Chech if property is set, allways returns null.
89 */
90 public String getProperty(String name){
91 return null;
92 }
93
94 }