View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.tags.soap;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.StringReader;
22  
23  /***
24   * Wraps a String as an InputStream. Note that data will be lost for
25   * characters not in ISO Latin 1, as a simple char->byte mapping is assumed.
26   *
27   * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
28   */
29  public class StringInputStream
30      extends InputStream
31  {
32      /*** Source string, stored as a StringReader */
33      private StringReader in;
34  
35      /***
36       * Composes a stream from a String
37       *
38       * @param source The string to read from. Must not be <code>null</code>.
39       */
40      public StringInputStream( String source )
41      {
42          in = new StringReader( source );
43      }
44  
45      /***
46       * Reads from the Stringreader, returning the same value. Note that
47       * data will be lost for characters not in ISO Latin 1. Clients
48       * assuming a return value in the range -1 to 255 may even fail on
49       * such input.
50       *
51       * @return the value of the next character in the StringReader
52       *
53       * @exception IOException if the original StringReader fails to be read
54       */
55      public int read() throws IOException
56      {
57          return in.read();
58      }
59  
60      /***
61       * Closes the Stringreader.
62       *
63       * @exception IOException if the original StringReader fails to be closed
64       */
65      public void close() throws IOException
66      {
67          in.close();
68      }
69  
70      /***
71       * Marks the read limit of the StringReader.
72       *
73       * @param limit the maximum limit of bytes that can be read before the
74       *              mark position becomes invalid
75       */
76      public synchronized void mark( final int limit )
77      {
78          try
79          {
80              in.mark( limit );
81          }
82          catch ( IOException ioe )
83          {
84              throw new RuntimeException( ioe.getMessage() );
85          }
86      }
87  
88      /***
89       * Resets the StringReader.
90       *
91       * @exception IOException if the StringReader fails to be reset
92       */
93      public synchronized void reset() throws IOException
94      {
95          in.reset();
96      }
97  
98      /***
99       * @see InputStream#markSupported
100      */
101     public boolean markSupported()
102     {
103         return in.markSupported();
104     }
105 }
106