| 1 | /* StringBufferInputStream.java -- Read an String as a stream
|
|---|
| 2 | Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.io;
|
|---|
| 40 |
|
|---|
| 41 | /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
|---|
| 42 | * "The Java Language Specification", ISBN 0-201-63451-1
|
|---|
| 43 | * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
|
|---|
| 44 | * Status: Believed complete and correct. Deprecated in JDK 1.1.
|
|---|
| 45 | */
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * This class permits a <code>String</code> to be read as an input stream.
|
|---|
| 49 | * The low eight bits of each character in the <code>String</code> are the
|
|---|
| 50 | * bytes that are returned. The high eight bits of each character are
|
|---|
| 51 | * discarded.
|
|---|
| 52 | * <p>
|
|---|
| 53 | * The mark/reset functionality in this class behaves differently than
|
|---|
| 54 | * normal. The <code>mark()</code> method is always ignored and the
|
|---|
| 55 | * <code>reset()</code> method always resets in stream to start reading from
|
|---|
| 56 | * position 0 in the String. Note that since this method does not override
|
|---|
| 57 | * <code>markSupported()</code> in <code>InputStream</code>, calling that
|
|---|
| 58 | * method will return <code>false</code>.
|
|---|
| 59 | * <p>
|
|---|
| 60 | * Note that this class is deprecated because it does not properly handle
|
|---|
| 61 | * 16-bit Java characters. It is provided for backwards compatibility only
|
|---|
| 62 | * and should not be used for new development. The <code>StringReader</code>
|
|---|
| 63 | * class should be used instead.
|
|---|
| 64 | *
|
|---|
| 65 | * @deprecated
|
|---|
| 66 | *
|
|---|
| 67 | * @author Aaron M. Renn ([email protected])
|
|---|
| 68 | * @author Warren Levy <[email protected]>
|
|---|
| 69 | */
|
|---|
| 70 | public class StringBufferInputStream extends InputStream
|
|---|
| 71 | {
|
|---|
| 72 | /** The String which is the input to this stream. */
|
|---|
| 73 | protected String buffer;
|
|---|
| 74 |
|
|---|
| 75 | /** Position of the next byte in buffer to be read. */
|
|---|
| 76 | protected int pos = 0;
|
|---|
| 77 |
|
|---|
| 78 | /** The length of the String buffer. */
|
|---|
| 79 | protected int count;
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Create a new <code>StringBufferInputStream</code> that will read bytes
|
|---|
| 83 | * from the passed in <code>String</code>. This stream will read from the
|
|---|
| 84 | * beginning to the end of the <code>String</code>.
|
|---|
| 85 | *
|
|---|
| 86 | * @param s The <code>String</code> this stream will read from.
|
|---|
| 87 | */
|
|---|
| 88 | public StringBufferInputStream(String s)
|
|---|
| 89 | {
|
|---|
| 90 | buffer = s;
|
|---|
| 91 | count = s.length();
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * This method returns the number of bytes available to be read from this
|
|---|
| 96 | * stream. The value returned will be equal to <code>count - pos</code>.
|
|---|
| 97 | *
|
|---|
| 98 | * @return The number of bytes that can be read from this stream before
|
|---|
| 99 | * blocking, which is all of them
|
|---|
| 100 | */
|
|---|
| 101 | public int available()
|
|---|
| 102 | {
|
|---|
| 103 | return count - pos;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * This method reads one byte from the stream. The <code>pos</code> counter
|
|---|
| 108 | * is advanced to the next byte to be read. The byte read is returned as
|
|---|
| 109 | * an int in the range of 0-255. If the stream position is already at the
|
|---|
| 110 | * end of the buffer, no byte is read and a -1 is returned in order to
|
|---|
| 111 | * indicate the end of the stream.
|
|---|
| 112 | *
|
|---|
| 113 | * @return The byte read, or -1 if end of stream
|
|---|
| 114 | */
|
|---|
| 115 | public int read()
|
|---|
| 116 | {
|
|---|
| 117 | if (pos >= count)
|
|---|
| 118 | return -1; // EOF
|
|---|
| 119 |
|
|---|
| 120 | return ((int) buffer.charAt(pos++)) & 0xFF;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * This method reads bytes from the stream and stores them into a caller
|
|---|
| 125 | * supplied buffer. It starts storing the data at index <code>offset</code>
|
|---|
| 126 | * into the buffer and attempts to read <code>len</code> bytes. This method
|
|---|
| 127 | * can return before reading the number of bytes requested if the end of the
|
|---|
|
|---|