1 /* 2 * Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.io; 27 28 /** 29 * This class allows an application to create an input stream in 30 * which the bytes read are supplied by the contents of a string. 31 * Applications can also read bytes from a byte array by using a 32 * {@code ByteArrayInputStream}. 33 * <p> 34 * Only the low eight bits of each character in the string are used by 35 * this class. 36 * 37 * @author Arthur van Hoff 38 * @see java.io.ByteArrayInputStream 39 * @see java.io.StringReader 40 * @since 1.0 41 * @deprecated This class does not properly convert characters into bytes. As 42 * of JDK 1.1, the preferred way to create a stream from a 43 * string is via the {@code StringReader} class. 44 */ 45 @Deprecated 46 public class StringBufferInputStream extends InputStream { 47 /** 48 * The string from which bytes are read. 49 */ 50 protected String buffer; 51 52 /** 53 * The index of the next character to read from the input stream buffer. 54 * 55 * @see java.io.StringBufferInputStream#buffer 56 */ 57 protected int pos; 58 59 /** 60 * The number of valid characters in the input stream buffer. 61 * 62 * @see java.io.StringBufferInputStream#buffer 63 */ 64 protected int count; 65 66 /** 67 * Creates a string input stream to read data from the specified string. 68 * 69 * @param s the underlying input buffer. 70 */ StringBufferInputStream(String s)71 public StringBufferInputStream(String s) { 72 this.buffer = s; 73 count = s.length(); 74 } 75 76 /** 77 * Reads the next byte of data from this input stream. The value 78 * byte is returned as an {@code int} in the range 79 * {@code 0} to {@code 255}. If no byte is available 80 * because the end of the stream has been reached, the value 81 * {@code -1} is returned. 82 * <p> 83 * The {@code read} method of 84 * {@code StringBufferInputStream} cannot block. It returns the 85 * low eight bits of the next character in this input stream's buffer. 86 * 87 * @return the next byte of data, or {@code -1} if the end of the 88 * stream is reached. 89 */ read()90 public synchronized int read() { 91 return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1; 92 } 93 94 /** 95 * Reads up to {@code len} bytes of data from this input stream 96 * into an array of bytes. 97 * <p> 98 * The {@code read} method of 99 * {@code StringBufferInputStream} cannot block. It copies the 100 * low eight bits from the characters in this input stream's buffer into 101 * the byte array argument. 102 * 103 * @param b the buffer into which the data is read. 104 * @param off the start offset of the data. 105 * @param len the maximum number of bytes read. 106 * @return the total number of bytes read into the buffer, or 107 * {@code -1} if there is no more data because the end of 108 * the stream has been reached. 109 */ 110 @SuppressWarnings("deprecation") read(byte b[], int off, int len)111 public synchronized int read(byte b[], int off, int len) { 112 if (b == null) { 113 throw new NullPointerException(); 114 } else if ((off < 0) || (off > b.length) || (len < 0) || 115 ((off + len) > b.length) || ((off + len) < 0)) { 116 throw new IndexOutOfBoundsException(); 117 } 118 if (pos >= count) { 119 return -1; 120 } 121 122 int avail = count - pos; 123 if (len > avail) { 124 len = avail; 125 } 126 if (len <= 0) { 127 return 0; 128 } 129 buffer.getBytes(pos, pos + len, b, off); 130 pos += len; 131 return len; 132 } 133 134 /** 135 * Skips {@code n} bytes of input from this input stream. Fewer 136 * bytes might be skipped if the end of the input stream is reached. 137 * 138 * @param n the number of bytes to be skipped. 139 * @return the actual number of bytes skipped. 140 */ skip(long n)141 public synchronized long skip(long n) { 142 if (n < 0) { 143 return 0; 144 } 145 if (n > count - pos) { 146 n = count - pos; 147 } 148 pos += n; 149 return n; 150 } 151 152 /** 153 * Returns the number of bytes that can be read from the input 154 * stream without blocking. 155 * 156 * @return the value of {@code count - pos}, which is the 157 * number of bytes remaining to be read from the input buffer. 158 */ available()159 public synchronized int available() { 160 return count - pos; 161 } 162 163 /** 164 * Resets the input stream to begin reading from the first character 165 * of this input stream's underlying buffer. 166 */ reset()167 public synchronized void reset() { 168 pos = 0; 169 } 170 } 171