1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.io;
19
20import java.util.Arrays;
21
22/**
23 * A specialized {@link InputStream} that reads bytes from a {@code String} in
24 * a sequential manner.
25 *
26 * @deprecated Use {@link StringReader} instead.
27 */
28@Deprecated
29public class StringBufferInputStream extends InputStream {
30    /**
31     * The source string containing the data to read.
32     */
33    protected String buffer;
34
35    /**
36     * The total number of characters in the source string.
37     */
38    protected int count;
39
40    /**
41     * The current position within the source string.
42     */
43    protected int pos;
44
45    /**
46     * Construct a new {@code StringBufferInputStream} with {@code str} as
47     * source. The size of the stream is set to the {@code length()} of the
48     * string.
49     *
50     * @param str
51     *            the source string for this stream.
52     * @throws NullPointerException
53     *             if {@code str} is {@code null}.
54     */
55    public StringBufferInputStream(String str) {
56        if (str == null) {
57            throw new NullPointerException("str == null");
58        }
59        buffer = str;
60        count = str.length();
61    }
62
63    @Override
64    public synchronized int available() {
65        return count - pos;
66    }
67
68    /**
69     * Reads a single byte from the source string and returns it as an integer
70     * in the range from 0 to 255. Returns -1 if the end of the source string
71     * has been reached.
72     *
73     * @return the byte read or -1 if the end of the source string has been
74     *         reached.
75     */
76    @Override
77    public synchronized int read() {
78        return pos < count ? buffer.charAt(pos++) & 0xFF : -1;
79    }
80
81    @Override public synchronized int read(byte[] buffer, int byteOffset, int byteCount) {
82        if (buffer == null) {
83            throw new NullPointerException("buffer == null");
84        }
85        Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount);
86        if (byteCount == 0) {
87            return 0;
88        }
89
90        int copylen = count - pos < byteCount ? count - pos : byteCount;
91        for (int i = 0; i < copylen; ++i) {
92            buffer[byteOffset + i] = (byte) this.buffer.charAt(pos + i);
93        }
94        pos += copylen;
95        return copylen;
96    }
97
98    /**
99     * Resets this stream to the beginning of the source string.
100     */
101    @Override
102    public synchronized void reset() {
103        pos = 0;
104    }
105
106    /**
107     * Skips {@code charCount} characters in the source string. It does nothing and
108     * returns 0 if {@code charCount} is negative. Less than {@code charCount} characters are
109     * skipped if the end of the source string is reached before the operation
110     * completes.
111     *
112     * @return the number of characters actually skipped.
113     */
114    @Override
115    public synchronized long skip(long charCount) {
116        if (charCount <= 0) {
117            return 0;
118        }
119
120        int numskipped;
121        if (this.count - pos < charCount) {
122            numskipped = this.count - pos;
123            pos = this.count;
124        } else {
125            numskipped = (int) charCount;
126            pos += charCount;
127        }
128        return numskipped;
129    }
130}
131