View Javadoc

1   /*
2    * Copyright (c) 2003 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: ByteCachedStream.java,v 1.1.1.1 2004/05/19 12:07:30 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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        } // end of if ()
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        } // end of else
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        } // end of else
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  }// CachedStream