001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.io;
019:
020: import org.apache.harmony.luni.util.Msg;
021:
022: /**
023: * StringBufferInputStream is a class for to allow a String to be used as an
024: * InputStream.
025: *
026: * @deprecated Use StringReader
027: */
028: @Deprecated
029: public class StringBufferInputStream extends InputStream {
030: /**
031: * The String containing the data to read.
032: */
033: protected String buffer;
034:
035: /**
036: * The total number of characters inside the buffer.
037: */
038: protected int count;
039:
040: /**
041: * The current position within the String buffer.
042: */
043: protected int pos;
044:
045: /**
046: * Constructs a new StringBufferInputStream on the String <code>str</code>.
047: *
048: * @param str
049: * the String to read characters from.
050: */
051: public StringBufferInputStream(String str) {
052: if (str == null) {
053: throw new NullPointerException();
054: }
055: buffer = str;
056: count = str.length();
057: }
058:
059: /**
060: * Answers an int representing then number of characters that are available
061: * to read.
062: *
063: * @return the number of characters available.
064: *
065: */
066: @Override
067: public synchronized int available() {
068: return count - pos;
069: }
070:
071: /**
072: * Reads a single byte from this InputStream and returns the result as an
073: * int. The low-order byte is returned or -1 of the end of stream was
074: * encountered.
075: *
076: * @return the byte read or -1 if end of stream.
077: */
078: @Override
079: public synchronized int read() {
080: return pos < count ? buffer.charAt(pos++) & 0xFF : -1;
081: }
082:
083: /**
084: * Reads at most <code>length</code> bytes from this InputStream and
085: * stores them in byte array <code>b</code> starting at
086: * <code>offset</code>. Answer the number of bytes actually read or -1 if
087: * no bytes were read and end of stream was encountered.
088: *
089: * @param b
090: * the byte array in which to store the read bytes.
091: * @param offset
092: * the offset in <code>b</code> to store the read bytes.
093: * @param length
094: * the maximum number of bytes to store in <code>b</code>.
095: * @return the number of bytes actually read or -1 if end of stream.
096: */
097: @Override
098: public synchronized int read(byte b[], int offset, int length) {
099: // According to 22.7.6 should return -1 before checking other
100: // parameters.
101: if (pos >= count) {
102: return -1;
103: }
104: if (b == null) {
105: throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
106: }
107: // avoid int overflow
108: if (offset < 0 || offset > b.length || length < 0
109: || length > b.length - offset) {
110: throw new ArrayIndexOutOfBoundsException();
111: }
112: if (length == 0) {
113: return 0;
114: }
115:
116: int copylen = count - pos < length ? count - pos : length;
117: for (int i = 0; i < copylen; i++) {
118: b[offset + i] = (byte) buffer.charAt(pos + i);
119: }
120: pos += copylen;
121: return copylen;
122: }
123:
124: /**
125: * Reset this InputStream to position 0. Reads/Skips will now take place
126: * from this position.
127: *
128: */
129: @Override
130: public synchronized void reset() {
131: pos = 0;
132: }
133:
134: /**
135: * Skips <code>count</code> number of characters in this InputStream.
136: * Subsequent <code>read()</code>'s will not return these characters
137: * unless <code>reset()</code> is used.
138: *
139: * @param n
140: * the number of characters to skip.
141: * @return the number of characters actually skipped.
142: */
143: @Override
144: public synchronized long skip(long n) {
145: if (n <= 0) {
146: return 0;
147: }
148:
149: int numskipped;
150: if (this .count - pos < n) {
151: numskipped = this .count - pos;
152: pos = this .count;
153: } else {
154: numskipped = (int) n;
155: pos += n;
156: }
157: return numskipped;
158: }
159: }
|