0N/A/*
2362N/A * Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/A/**
0N/A * This class allows an application to create an input stream in
0N/A * which the bytes read are supplied by the contents of a string.
0N/A * Applications can also read bytes from a byte array by using a
0N/A * <code>ByteArrayInputStream</code>.
0N/A * <p>
0N/A * Only the low eight bits of each character in the string are used by
0N/A * this class.
0N/A *
0N/A * @author Arthur van Hoff
0N/A * @see java.io.ByteArrayInputStream
0N/A * @see java.io.StringReader
0N/A * @since JDK1.0
0N/A * @deprecated This class does not properly convert characters into bytes. As
0N/A * of JDK&nbsp;1.1, the preferred way to create a stream from a
0N/A * string is via the <code>StringReader</code> class.
0N/A */
0N/A@Deprecated
0N/Apublic
0N/Aclass StringBufferInputStream extends InputStream {
0N/A /**
0N/A * The string from which bytes are read.
0N/A */
0N/A protected String buffer;
0N/A
0N/A /**
0N/A * The index of the next character to read from the input stream buffer.
0N/A *
0N/A * @see java.io.StringBufferInputStream#buffer
0N/A */
0N/A protected int pos;
0N/A
0N/A /**
0N/A * The number of valid characters in the input stream buffer.
0N/A *
0N/A * @see java.io.StringBufferInputStream#buffer
0N/A */
0N/A protected int count;
0N/A
0N/A /**
0N/A * Creates a string input stream to read data from the specified string.
0N/A *
0N/A * @param s the underlying input buffer.
0N/A */
0N/A public StringBufferInputStream(String s) {
0N/A this.buffer = s;
0N/A count = s.length();
0N/A }
0N/A
0N/A /**
0N/A * Reads the next byte of data from this input stream. The value
0N/A * byte is returned as an <code>int</code> in the range
0N/A * <code>0</code> to <code>255</code>. If no byte is available
0N/A * because the end of the stream has been reached, the value
0N/A * <code>-1</code> is returned.
0N/A * <p>
0N/A * The <code>read</code> method of
0N/A * <code>StringBufferInputStream</code> cannot block. It returns the
0N/A * low eight bits of the next character in this input stream's buffer.
0N/A *
0N/A * @return the next byte of data, or <code>-1</code> if the end of the
0N/A * stream is reached.
0N/A */
0N/A public synchronized int read() {
0N/A return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
0N/A }
0N/A
0N/A /**
0N/A * Reads up to <code>len</code> bytes of data from this input stream
0N/A * into an array of bytes.
0N/A * <p>
0N/A * The <code>read</code> method of
0N/A * <code>StringBufferInputStream</code> cannot block. It copies the
0N/A * low eight bits from the characters in this input stream's buffer into
0N/A * the byte array argument.
0N/A *
0N/A * @param b the buffer into which the data is read.
0N/A * @param off the start offset of the data.
0N/A * @param len the maximum number of bytes read.
0N/A * @return the total number of bytes read into the buffer, or
0N/A * <code>-1</code> if there is no more data because the end of
0N/A * the stream has been reached.
0N/A */
0N/A public synchronized int read(byte b[], int off, int len) {
0N/A if (b == null) {
0N/A throw new NullPointerException();
0N/A } else if ((off < 0) || (off > b.length) || (len < 0) ||
0N/A ((off + len) > b.length) || ((off + len) < 0)) {
0N/A throw new IndexOutOfBoundsException();
0N/A }
0N/A if (pos >= count) {
0N/A return -1;
0N/A }
0N/A if (pos + len > count) {
0N/A len = count - pos;
0N/A }
0N/A if (len <= 0) {
0N/A return 0;
0N/A }
0N/A String s = buffer;
0N/A int cnt = len;
0N/A while (--cnt >= 0) {
0N/A b[off++] = (byte)s.charAt(pos++);
0N/A }
0N/A
0N/A return len;
0N/A }
0N/A
0N/A /**
0N/A * Skips <code>n</code> bytes of input from this input stream. Fewer
0N/A * bytes might be skipped if the end of the input stream is reached.
0N/A *
0N/A * @param n the number of bytes to be skipped.
0N/A * @return the actual number of bytes skipped.
0N/A */
0N/A public synchronized long skip(long n) {
0N/A if (n < 0) {
0N/A return 0;
0N/A }
0N/A if (n > count - pos) {
0N/A n = count - pos;
0N/A }
0N/A pos += n;
0N/A return n;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of bytes that can be read from the input
0N/A * stream without blocking.
0N/A *
0N/A * @return the value of <code>count&nbsp;-&nbsp;pos</code>, which is the
0N/A * number of bytes remaining to be read from the input buffer.
0N/A */
0N/A public synchronized int available() {
0N/A return count - pos;
0N/A }
0N/A
0N/A /**
0N/A * Resets the input stream to begin reading from the first character
0N/A * of this input stream's underlying buffer.
0N/A */
0N/A public synchronized void reset() {
0N/A pos = 0;
0N/A }
0N/A}