1 package org.apache.maven.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }