KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > io > StringInputStream


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.io;
4
5 import java.io.IOException JavaDoc;
6
7 /**
8  * Simple InputStream subclass to fetch <b>all</b> bytes from a String.
9  * @see AsciiStringInputStream
10  */

11 public class StringInputStream {
12
13     protected int strOffset = 0;
14     protected int charOffset = 0;
15     protected int available;
16     protected String JavaDoc str;
17
18     public StringInputStream(String JavaDoc s) {
19         str = s;
20         available = s.length() << 1;
21     }
22
23     public int read() throws IOException JavaDoc {
24         if (available == 0) {
25             return -1;
26         }
27         available--;
28         char c = str.charAt(strOffset);
29         if (charOffset == 0) {
30             charOffset = 1;
31             return (c & 0x0000ff00) >> 8;
32         } else {
33             charOffset = 0;
34             strOffset++;
35             return c & 0x000000ff;
36         }
37     }
38
39     public int available() throws IOException JavaDoc {
40         return available;
41     }
42
43 }
44
Popular Tags