001    package echopoint.util.io;
002    
003    import java.io.InputStream;
004    
005    /**
006     * <code>StringInputStream</code> can read a String and return it as an
007     * <code>InputStream.</code>
008     */
009    
010    public class StringInputStream extends InputStream {
011            private String buffer;
012    
013            private int pos;
014    
015            private int count;
016    
017            /**
018             * Constructs a <code>StringInputStream</code> from the specified
019             * <code>String</code>
020             *
021             * @param s - the string to read
022             */
023            public StringInputStream(String s) {
024                    this.buffer = s;
025                    count = s.length();
026            }
027    
028            /**
029             * @see java.io.InputStream#read()
030             */
031            public synchronized int read() {
032                    return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
033            }
034    
035            /**
036             * @see java.io.InputStream#read(byte[], int, int)
037             */
038            public synchronized int read(byte b[], int off, int len) {
039                    if (b == null) {
040                            throw new NullPointerException();
041                    } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
042                            throw new IndexOutOfBoundsException();
043                    }
044                    if (pos >= count) {
045                            return -1;
046                    }
047                    if (pos + len > count) {
048                            len = count - pos;
049                    }
050                    if (len <= 0) {
051                            return 0;
052                    }
053                    String s = buffer;
054                    int cnt = len;
055                    while (--cnt >= 0) {
056                            b[off++] = (byte) s.charAt(pos++);
057                    }
058    
059                    return len;
060            }
061    
062            /**
063             * @see java.io.InputStream#skip(long)
064             */
065            public synchronized long skip(long n) {
066                    if (n < 0) {
067                            return 0;
068                    }
069                    if (n > count - pos) {
070                            n = count - pos;
071                    }
072                    pos += n;
073                    return n;
074            }
075    
076            /**
077             * @see java.io.InputStream#available()
078             */
079            public synchronized int available() {
080                    return count - pos;
081            }
082    
083            /**
084             * @see java.io.InputStream#reset()
085             */
086            public synchronized void reset() {
087                    pos = 0;
088            }
089    }
090