emul/mini/src/main/java/java/io/PushbackInputStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 20:39:23 +0100
branchemul
changeset 554 05224402145d
parent 149 emul/src/main/java/java/io/PushbackInputStream.java@32653a09f0db
child 560 53fafe384803
permissions -rw-r--r--
First attempt to separate 'mini' profile from the rest of JDK APIs
jtulach@147
     1
/*
jtulach@147
     2
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
jtulach@147
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@147
     4
 *
jtulach@147
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@147
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@147
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@147
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@147
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@147
    10
 *
jtulach@147
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@147
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@147
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@147
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@147
    15
 * accompanied this code).
jtulach@147
    16
 *
jtulach@147
    17
 * You should have received a copy of the GNU General Public License version
jtulach@147
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@147
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@147
    20
 *
jtulach@147
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@147
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@147
    23
 * questions.
jtulach@147
    24
 */
jtulach@147
    25
jtulach@147
    26
package java.io;
jtulach@147
    27
jtulach@147
    28
/**
jtulach@147
    29
 * A <code>PushbackInputStream</code> adds
jtulach@147
    30
 * functionality to another input stream, namely
jtulach@147
    31
 * the  ability to "push back" or "unread"
jtulach@147
    32
 * one byte. This is useful in situations where
jtulach@147
    33
 * it is  convenient for a fragment of code
jtulach@147
    34
 * to read an indefinite number of data bytes
jtulach@147
    35
 * that  are delimited by a particular byte
jtulach@147
    36
 * value; after reading the terminating byte,
jtulach@147
    37
 * the  code fragment can "unread" it, so that
jtulach@147
    38
 * the next read operation on the input stream
jtulach@147
    39
 * will reread the byte that was pushed back.
jtulach@147
    40
 * For example, bytes representing the  characters
jtulach@147
    41
 * constituting an identifier might be terminated
jtulach@147
    42
 * by a byte representing an  operator character;
jtulach@147
    43
 * a method whose job is to read just an identifier
jtulach@147
    44
 * can read until it  sees the operator and
jtulach@147
    45
 * then push the operator back to be re-read.
jtulach@147
    46
 *
jtulach@147
    47
 * @author  David Connelly
jtulach@147
    48
 * @author  Jonathan Payne
jtulach@147
    49
 * @since   JDK1.0
jtulach@147
    50
 */
jtulach@147
    51
public
jtulach@147
    52
class PushbackInputStream extends FilterInputStream {
jtulach@147
    53
    /**
jtulach@147
    54
     * The pushback buffer.
jtulach@147
    55
     * @since   JDK1.1
jtulach@147
    56
     */
jtulach@147
    57
    protected byte[] buf;
jtulach@147
    58
jtulach@147
    59
    /**
jtulach@147
    60
     * The position within the pushback buffer from which the next byte will
jtulach@147
    61
     * be read.  When the buffer is empty, <code>pos</code> is equal to
jtulach@147
    62
     * <code>buf.length</code>; when the buffer is full, <code>pos</code> is
jtulach@147
    63
     * equal to zero.
jtulach@147
    64
     *
jtulach@147
    65
     * @since   JDK1.1
jtulach@147
    66
     */
jtulach@147
    67
    protected int pos;
jtulach@147
    68
jtulach@147
    69
    /**
jtulach@147
    70
     * Check to make sure that this stream has not been closed
jtulach@147
    71
     */
jtulach@147
    72
    private void ensureOpen() throws IOException {
jtulach@147
    73
        if (in == null)
jtulach@147
    74
            throw new IOException("Stream closed");
jtulach@147
    75
    }
jtulach@147
    76
jtulach@147
    77
    /**
jtulach@147
    78
     * Creates a <code>PushbackInputStream</code>
jtulach@147
    79
     * with a pushback buffer of the specified <code>size</code>,
jtulach@147
    80
     * and saves its  argument, the input stream
jtulach@147
    81
     * <code>in</code>, for later use. Initially,
jtulach@147
    82
     * there is no pushed-back byte  (the field
jtulach@147
    83
     * <code>pushBack</code> is initialized to
jtulach@147
    84
     * <code>-1</code>).
jtulach@147
    85
     *
jtulach@147
    86
     * @param  in    the input stream from which bytes will be read.
jtulach@147
    87
     * @param  size  the size of the pushback buffer.
jtulach@147
    88
     * @exception IllegalArgumentException if size is <= 0
jtulach@147
    89
     * @since  JDK1.1
jtulach@147
    90
     */
jtulach@147
    91
    public PushbackInputStream(InputStream in, int size) {
jtulach@147
    92
        super(in);
jtulach@147
    93
        if (size <= 0) {
jtulach@147
    94
            throw new IllegalArgumentException("size <= 0");
jtulach@147
    95
        }
jtulach@147
    96
        this.buf = new byte[size];
jtulach@147
    97
        this.pos = size;
jtulach@147
    98
    }
jtulach@147
    99
jtulach@147
   100
    /**
jtulach@147
   101
     * Creates a <code>PushbackInputStream</code>
jtulach@147
   102
     * and saves its  argument, the input stream
jtulach@147
   103
     * <code>in</code>, for later use. Initially,
jtulach@147
   104
     * there is no pushed-back byte  (the field
jtulach@147
   105
     * <code>pushBack</code> is initialized to
jtulach@147
   106
     * <code>-1</code>).
jtulach@147
   107
     *
jtulach@147
   108
     * @param   in   the input stream from which bytes will be read.
jtulach@147
   109
     */
jtulach@147
   110
    public PushbackInputStream(InputStream in) {
jtulach@147
   111
        this(in, 1);
jtulach@147
   112
    }
jtulach@147
   113
jtulach@147
   114
    /**
jtulach@147
   115
     * Reads the next byte of data from this input stream. The value
jtulach@147
   116
     * byte is returned as an <code>int</code> in the range
jtulach@147
   117
     * <code>0</code> to <code>255</code>. If no byte is available
jtulach@147
   118
     * because the end of the stream has been reached, the value
jtulach@147
   119
     * <code>-1</code> is returned. This method blocks until input data
jtulach@147
   120
     * is available, the end of the stream is detected, or an exception
jtulach@147
   121
     * is thrown.
jtulach@147
   122
     *
jtulach@147
   123
     * <p> This method returns the most recently pushed-back byte, if there is
jtulach@147
   124
     * one, and otherwise calls the <code>read</code> method of its underlying
jtulach@147
   125
     * input stream and returns whatever value that method returns.
jtulach@147
   126
     *
jtulach@147
   127
     * @return     the next byte of data, or <code>-1</code> if the end of the
jtulach@147
   128
     *             stream has been reached.
jtulach@147
   129
     * @exception  IOException  if this input stream has been closed by
jtulach@147
   130
     *             invoking its {@link #close()} method,
jtulach@147
   131
     *             or an I/O error occurs.
jtulach@147
   132
     * @see        java.io.InputStream#read()
jtulach@147
   133
     */
jtulach@147
   134
    public int read() throws IOException {
jtulach@147
   135
        ensureOpen();
jtulach@147
   136
        if (pos < buf.length) {
jtulach@147
   137
            return buf[pos++] & 0xff;
jtulach@147
   138
        }
jtulach@147
   139
        return super.read();
jtulach@147
   140
    }
jtulach@147
   141
jtulach@147
   142
    /**
jtulach@147
   143
     * Reads up to <code>len</code> bytes of data from this input stream into
jtulach@147
   144
     * an array of bytes.  This method first reads any pushed-back bytes; after
jtulach@147
   145
     * that, if fewer than <code>len</code> bytes have been read then it
jtulach@147
   146
     * reads from the underlying input stream. If <code>len</code> is not zero, the method
jtulach@147
   147
     * blocks until at least 1 byte of input is available; otherwise, no
jtulach@147
   148
     * bytes are read and <code>0</code> is returned.
jtulach@147
   149
     *
jtulach@147
   150
     * @param      b     the buffer into which the data is read.
jtulach@147
   151
     * @param      off   the start offset in the destination array <code>b</code>
jtulach@147
   152
     * @param      len   the maximum number of bytes read.
jtulach@147
   153
     * @return     the total number of bytes read into the buffer, or
jtulach@147
   154
     *             <code>-1</code> if there is no more data because the end of
jtulach@147
   155
     *             the stream has been reached.
jtulach@147
   156
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
jtulach@147
   157
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
jtulach@147
   158
     * <code>len</code> is negative, or <code>len</code> is greater than
jtulach@147
   159
     * <code>b.length - off</code>
jtulach@147
   160
     * @exception  IOException  if this input stream has been closed by
jtulach@147
   161
     *             invoking its {@link #close()} method,
jtulach@147
   162
     *             or an I/O error occurs.
jtulach@147
   163
     * @see        java.io.InputStream#read(byte[], int, int)
jtulach@147
   164
     */
jtulach@147
   165
    public int read(byte[] b, int off, int len) throws IOException {
jtulach@147
   166
        ensureOpen();
jtulach@147
   167
        if (b == null) {
jtulach@147
   168
            throw new NullPointerException();
jtulach@147
   169
        } else if (off < 0 || len < 0 || len > b.length - off) {
jtulach@147
   170
            throw new IndexOutOfBoundsException();
jtulach@147
   171
        } else if (len == 0) {
jtulach@147
   172
            return 0;
jtulach@147
   173
        }
jtulach@147
   174
jtulach@147
   175
        int avail = buf.length - pos;
jtulach@147
   176
        if (avail > 0) {
jtulach@147
   177
            if (len < avail) {
jtulach@147
   178
                avail = len;
jtulach@147
   179
            }
jaroslav@149
   180
            arraycopy(buf, pos, b, off, avail);
jtulach@147
   181
            pos += avail;
jtulach@147
   182
            off += avail;
jtulach@147
   183
            len -= avail;
jtulach@147
   184
        }
jtulach@147
   185
        if (len > 0) {
jtulach@147
   186
            len = super.read(b, off, len);
jtulach@147
   187
            if (len == -1) {
jtulach@147
   188
                return avail == 0 ? -1 : avail;
jtulach@147
   189
            }
jtulach@147
   190
            return avail + len;
jtulach@147
   191
        }
jtulach@147
   192
        return avail;
jtulach@147
   193
    }
jtulach@147
   194
jtulach@147
   195
    /**
jtulach@147
   196
     * Pushes back a byte by copying it to the front of the pushback buffer.
jtulach@147
   197
     * After this method returns, the next byte to be read will have the value
jtulach@147
   198
     * <code>(byte)b</code>.
jtulach@147
   199
     *
jtulach@147
   200
     * @param      b   the <code>int</code> value whose low-order
jtulach@147
   201
     *                  byte is to be pushed back.
jtulach@147
   202
     * @exception IOException If there is not enough room in the pushback
jtulach@147
   203
     *            buffer for the byte, or this input stream has been closed by
jtulach@147
   204
     *            invoking its {@link #close()} method.
jtulach@147
   205
     */
jtulach@147
   206
    public void unread(int b) throws IOException {
jtulach@147
   207
        ensureOpen();
jtulach@147
   208
        if (pos == 0) {
jtulach@147
   209
            throw new IOException("Push back buffer is full");
jtulach@147
   210
        }
jtulach@147
   211
        buf[--pos] = (byte)b;
jtulach@147
   212
    }
jtulach@147
   213
jtulach@147
   214
    /**
jtulach@147
   215
     * Pushes back a portion of an array of bytes by copying it to the front
jtulach@147
   216
     * of the pushback buffer.  After this method returns, the next byte to be
jtulach@147
   217
     * read will have the value <code>b[off]</code>, the byte after that will
jtulach@147
   218
     * have the value <code>b[off+1]</code>, and so forth.
jtulach@147
   219
     *
jtulach@147
   220
     * @param b the byte array to push back.
jtulach@147
   221
     * @param off the start offset of the data.
jtulach@147
   222
     * @param len the number of bytes to push back.
jtulach@147
   223
     * @exception IOException If there is not enough room in the pushback
jtulach@147
   224
     *            buffer for the specified number of bytes,
jtulach@147
   225
     *            or this input stream has been closed by
jtulach@147
   226
     *            invoking its {@link #close()} method.
jtulach@147
   227
     * @since     JDK1.1
jtulach@147
   228
     */
jtulach@147
   229
    public void unread(byte[] b, int off, int len) throws IOException {
jtulach@147
   230
        ensureOpen();
jtulach@147
   231
        if (len > pos) {
jtulach@147
   232
            throw new IOException("Push back buffer is full");
jtulach@147
   233
        }
jtulach@147
   234
        pos -= len;
jaroslav@149
   235
        arraycopy(b, off, buf, pos, len);
jtulach@147
   236
    }
jtulach@147
   237
jtulach@147
   238
    /**
jtulach@147
   239
     * Pushes back an array of bytes by copying it to the front of the
jtulach@147
   240
     * pushback buffer.  After this method returns, the next byte to be read
jtulach@147
   241
     * will have the value <code>b[0]</code>, the byte after that will have the
jtulach@147
   242
     * value <code>b[1]</code>, and so forth.
jtulach@147
   243
     *
jtulach@147
   244
     * @param b the byte array to push back
jtulach@147
   245
     * @exception IOException If there is not enough room in the pushback
jtulach@147
   246
     *            buffer for the specified number of bytes,
jtulach@147
   247
     *            or this input stream has been closed by
jtulach@147
   248
     *            invoking its {@link #close()} method.
jtulach@147
   249
     * @since     JDK1.1
jtulach@147
   250
     */
jtulach@147
   251
    public void unread(byte[] b) throws IOException {
jtulach@147
   252
        unread(b, 0, b.length);
jtulach@147
   253
    }
jtulach@147
   254
jtulach@147
   255
    /**
jtulach@147
   256
     * Returns an estimate of the number of bytes that can be read (or
jtulach@147
   257
     * skipped over) from this input stream without blocking by the next
jtulach@147
   258
     * invocation of a method for this input stream. The next invocation might be
jtulach@147
   259
     * the same thread or another thread.  A single read or skip of this
jtulach@147
   260
     * many bytes will not block, but may read or skip fewer bytes.
jtulach@147
   261
     *
jtulach@147
   262
     * <p> The method returns the sum of the number of bytes that have been
jtulach@147
   263
     * pushed back and the value returned by {@link
jtulach@147
   264
     * java.io.FilterInputStream#available available}.
jtulach@147
   265
     *
jtulach@147
   266
     * @return     the number of bytes that can be read (or skipped over) from
jtulach@147
   267
     *             the input stream without blocking.
jtulach@147
   268
     * @exception  IOException  if this input stream has been closed by
jtulach@147
   269
     *             invoking its {@link #close()} method,
jtulach@147
   270
     *             or an I/O error occurs.
jtulach@147
   271
     * @see        java.io.FilterInputStream#in
jtulach@147
   272
     * @see        java.io.InputStream#available()
jtulach@147
   273
     */
jtulach@147
   274
    public int available() throws IOException {
jtulach@147
   275
        ensureOpen();
jtulach@147
   276
        int n = buf.length - pos;
jtulach@147
   277
        int avail = super.available();
jtulach@147
   278
        return n > (Integer.MAX_VALUE - avail)
jtulach@147
   279
                    ? Integer.MAX_VALUE
jtulach@147
   280
                    : n + avail;
jtulach@147
   281
    }
jtulach@147
   282
jtulach@147
   283
    /**
jtulach@147
   284
     * Skips over and discards <code>n</code> bytes of data from this
jtulach@147
   285
     * input stream. The <code>skip</code> method may, for a variety of
jtulach@147
   286
     * reasons, end up skipping over some smaller number of bytes,
jtulach@147
   287
     * possibly zero.  If <code>n</code> is negative, no bytes are skipped.
jtulach@147
   288
     *
jtulach@147
   289
     * <p> The <code>skip</code> method of <code>PushbackInputStream</code>
jtulach@147
   290
     * first skips over the bytes in the pushback buffer, if any.  It then
jtulach@147
   291
     * calls the <code>skip</code> method of the underlying input stream if
jtulach@147
   292
     * more bytes need to be skipped.  The actual number of bytes skipped
jtulach@147
   293
     * is returned.
jtulach@147
   294
     *
jtulach@147
   295
     * @param      n  {@inheritDoc}
jtulach@147
   296
     * @return     {@inheritDoc}
jtulach@147
   297
     * @exception  IOException  if the stream does not support seek,
jtulach@147
   298
     *            or the stream has been closed by
jtulach@147
   299
     *            invoking its {@link #close()} method,
jtulach@147
   300
     *            or an I/O error occurs.
jtulach@147
   301
     * @see        java.io.FilterInputStream#in
jtulach@147
   302
     * @see        java.io.InputStream#skip(long n)
jtulach@147
   303
     * @since      1.2
jtulach@147
   304
     */
jtulach@147
   305
    public long skip(long n) throws IOException {
jtulach@147
   306
        ensureOpen();
jtulach@147
   307
        if (n <= 0) {
jtulach@147
   308
            return 0;
jtulach@147
   309
        }
jtulach@147
   310
jtulach@147
   311
        long pskip = buf.length - pos;
jtulach@147
   312
        if (pskip > 0) {
jtulach@147
   313
            if (n < pskip) {
jtulach@147
   314
                pskip = n;
jtulach@147
   315
            }
jtulach@147
   316
            pos += pskip;
jtulach@147
   317
            n -= pskip;
jtulach@147
   318
        }
jtulach@147
   319
        if (n > 0) {
jtulach@147
   320
            pskip += super.skip(n);
jtulach@147
   321
        }
jtulach@147
   322
        return pskip;
jtulach@147
   323
    }
jtulach@147
   324
jtulach@147
   325
    /**
jtulach@147
   326
     * Tests if this input stream supports the <code>mark</code> and
jtulach@147
   327
     * <code>reset</code> methods, which it does not.
jtulach@147
   328
     *
jtulach@147
   329
     * @return   <code>false</code>, since this class does not support the
jtulach@147
   330
     *           <code>mark</code> and <code>reset</code> methods.
jtulach@147
   331
     * @see     java.io.InputStream#mark(int)
jtulach@147
   332
     * @see     java.io.InputStream#reset()
jtulach@147
   333
     */
jtulach@147
   334
    public boolean markSupported() {
jtulach@147
   335
        return false;
jtulach@147
   336
    }
jtulach@147
   337
jtulach@147
   338
    /**
jtulach@147
   339
     * Marks the current position in this input stream.
jtulach@147
   340
     *
jtulach@147
   341
     * <p> The <code>mark</code> method of <code>PushbackInputStream</code>
jtulach@147
   342
     * does nothing.
jtulach@147
   343
     *
jtulach@147
   344
     * @param   readlimit   the maximum limit of bytes that can be read before
jtulach@147
   345
     *                      the mark position becomes invalid.
jtulach@147
   346
     * @see     java.io.InputStream#reset()
jtulach@147
   347
     */
jtulach@147
   348
    public synchronized void mark(int readlimit) {
jtulach@147
   349
    }
jtulach@147
   350
jtulach@147
   351
    /**
jtulach@147
   352
     * Repositions this stream to the position at the time the
jtulach@147
   353
     * <code>mark</code> method was last called on this input stream.
jtulach@147
   354
     *
jtulach@147
   355
     * <p> The method <code>reset</code> for class
jtulach@147
   356
     * <code>PushbackInputStream</code> does nothing except throw an
jtulach@147
   357
     * <code>IOException</code>.
jtulach@147
   358
     *
jtulach@147
   359
     * @exception  IOException  if this method is invoked.
jtulach@147
   360
     * @see     java.io.InputStream#mark(int)
jtulach@147
   361
     * @see     java.io.IOException
jtulach@147
   362
     */
jtulach@147
   363
    public synchronized void reset() throws IOException {
jtulach@147
   364
        throw new IOException("mark/reset not supported");
jtulach@147
   365
    }
jtulach@147
   366
jtulach@147
   367
    /**
jtulach@147
   368
     * Closes this input stream and releases any system resources
jtulach@147
   369
     * associated with the stream.
jtulach@147
   370
     * Once the stream has been closed, further read(), unread(),
jtulach@147
   371
     * available(), reset(), or skip() invocations will throw an IOException.
jtulach@147
   372
     * Closing a previously closed stream has no effect.
jtulach@147
   373
     *
jtulach@147
   374
     * @exception  IOException  if an I/O error occurs.
jtulach@147
   375
     */
jtulach@147
   376
    public synchronized void close() throws IOException {
jtulach@147
   377
        if (in == null)
jtulach@147
   378
            return;
jtulach@147
   379
        in.close();
jtulach@147
   380
        in = null;
jtulach@147
   381
        buf = null;
jtulach@147
   382
    }
jaroslav@149
   383
    static void arraycopy(byte[] value, int srcBegin, byte[] dst, int dstBegin, int count) {
jaroslav@149
   384
        while (count-- > 0) {
jaroslav@149
   385
            dst[dstBegin++] = value[srcBegin++];
jaroslav@149
   386
        }
jaroslav@149
   387
    }
jtulach@147
   388
}