jdk/src/java.base/share/classes/java/net/SocketInputStream.java
author lana
Fri, 19 May 2017 04:17:50 +0000
changeset 45184 d577bc42a1f0
parent 44534 a076dffbc2c1
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43202
diff changeset
     2
 * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5147
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5147
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5147
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5147
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5147
diff changeset
    23
 * questions.
2
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.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.FileChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.net.ConnectionResetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This stream extends FileInputStream to implement a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * SocketInputStream. Note that this class should <b>NOT</b> be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author      Jonathan Payne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class SocketInputStream extends FileInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private boolean eof;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private AbstractPlainSocketImpl impl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private byte temp[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private Socket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Creates a new SocketInputStream. Can only be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * by a Socket. This method needs to hang on to the owner Socket so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * that the fd will not be closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param impl the implemented socket input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    SocketInputStream(AbstractPlainSocketImpl impl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        super(impl.getFileDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.impl = impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        socket = impl.getSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * object associated with this file input stream.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    70
     * The {@code getChannel} method of {@code SocketInputStream}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    71
     * returns {@code null} since it is a socket based stream.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @return  the file channel associated with this file input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public final FileChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Reads into an array of bytes at the specified offset using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * the received socket primitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param fd the FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param b the buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param off the start offset of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param len the maximum number of bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param timeout the read timeout in ms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @return the actual number of bytes read, -1 is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *          returned when the end of the stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private native int socketRead0(FileDescriptor fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                   byte b[], int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                   int timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
    99
    // wrap native call to allow instrumentation
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   100
    /**
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   101
     * Reads into an array of bytes at the specified offset using
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   102
     * the received socket primitive.
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   103
     * @param fd the FileDescriptor
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   104
     * @param b the buffer into which the data is read
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   105
     * @param off the start offset of the data
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   106
     * @param len the maximum number of bytes read
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   107
     * @param timeout the read timeout in ms
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   108
     * @return the actual number of bytes read, -1 is
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   109
     *          returned when the end of the stream is reached.
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   110
     * @exception IOException If an I/O error has occurred.
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   111
     */
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   112
    private int socketRead(FileDescriptor fd,
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   113
                           byte b[], int off, int len,
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   114
                           int timeout)
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   115
        throws IOException {
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   116
        return socketRead0(fd, b, off, len, timeout);
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   117
    }
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   118
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Reads into a byte array data from the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param b the buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @return the actual number of bytes read, -1 is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *          returned when the end of the stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Reads into a byte array <i>b</i> at offset <i>off</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <i>length</i> bytes of data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param b the buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param off the start offset of the data
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 7668
diff changeset
   135
     * @param length the maximum number of bytes read
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return the actual number of bytes read, -1 is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *          returned when the end of the stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public int read(byte b[], int off, int length) throws IOException {
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 2
diff changeset
   141
        return read(b, off, length, impl.getTimeout());
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 2
diff changeset
   142
    }
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 2
diff changeset
   143
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 2
diff changeset
   144
    int read(byte b[], int off, int length, int timeout) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // EOF already encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // connection reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (impl.isConnectionReset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new SocketException("Connection reset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // bounds check
43202
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   158
        if (length <= 0 || off < 0 || length > b.length - off) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
43202
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   162
            throw new ArrayIndexOutOfBoundsException("length == " + length
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   163
                    + " off == " + off + " buffer length == " + b.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        boolean gotReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // acquire file descriptor and do the read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        FileDescriptor fd = impl.acquireFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        try {
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   171
            n = socketRead(fd, b, off, length, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } catch (ConnectionResetException rstExc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            gotReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            impl.releaseFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         * We receive a "connection reset" but there may be bytes still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         * buffered on the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (gotReset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            impl.setConnectionResetPending();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            impl.acquireFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            try {
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   189
                n = socketRead(fd, b, off, length, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } catch (ConnectionResetException rstExc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                impl.releaseFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         * If we get here we are at EOF, the socket has been closed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         * or the connection has been reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (impl.isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new SocketException("Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (impl.isConnectionResetPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            impl.setConnectionReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (impl.isConnectionReset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw new SocketException("Connection reset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Reads a single byte from the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        temp = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int n = read(temp, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return temp[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Skips n bytes of input.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 7668
diff changeset
   233
     * @param numbytes the number of bytes to skip
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @return  the actual number of bytes skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public long skip(long numbytes) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (numbytes <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        long n = numbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        int buflen = (int) Math.min(1024, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        byte data[] = new byte[buflen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        while (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            int r = read(data, 0, (int) Math.min((long) buflen, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (r < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            n -= r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return numbytes - n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Returns the number of bytes that can be read without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @return the number of immediately available bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return impl.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Closes the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private boolean closing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // Prevent recursion. See BugId 4484411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (closing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        closing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (!socket.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            impl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        closing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    void setEOF(boolean eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        this.eof = eof;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Overrides finalize, the fd is closed by the Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43202
diff changeset
   286
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    protected void finalize() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 26190
diff changeset
   292
    private static native void init();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
}