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