View Javadoc

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