src/java.base/share/classes/java/net/SocketOutputStream.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44534 jdk/src/java.base/share/classes/java/net/SocketOutputStream.java@a076dffbc2c1
child 48224 be0df5ab3093
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.FileOutputStream;
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This stream extends FileOutputStream to implement a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * SocketOutputStream. Note that this class should <b>NOT</b> be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author      Jonathan Payne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class SocketOutputStream extends FileOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private AbstractPlainSocketImpl impl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private byte temp[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Socket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Creates a new SocketOutputStream. Can only be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * by a Socket. This method needs to hang on to the owner Socket so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * that the fd will not be closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param impl the socket output stream inplemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    SocketOutputStream(AbstractPlainSocketImpl impl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        super(impl.getFileDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.impl = impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        socket = impl.getSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * object associated with this file output stream. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    67
     * The {@code getChannel} method of {@code SocketOutputStream}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    68
     * returns {@code null} since it is a socket based stream.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @return  the file channel associated with this file output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public final FileChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Writes to the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param fd the FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private native void socketWrite0(FileDescriptor fd, byte[] b, int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                     int len) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Writes to the socket with appropriate locking of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * FileDescriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private void socketWrite(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
43202
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   100
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   101
        if (len <= 0 || off < 0 || len > b.length - off) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
43202
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   105
            throw new ArrayIndexOutOfBoundsException("len == " + len
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   106
                    + " off == " + off + " buffer length == " + b.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        FileDescriptor fd = impl.acquireFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            socketWrite0(fd, b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } catch (SocketException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (se instanceof sun.net.ConnectionResetException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                impl.setConnectionResetPending();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                se = new SocketException("Connection reset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (impl.isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                throw new SocketException("Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            impl.releaseFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Writes a byte to the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        temp[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        socketWrite(temp, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Writes the contents of the buffer <i>b</i> to the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception SocketException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        socketWrite(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Writes <i>length</i> bytes from buffer <i>b</i> starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * offset <i>len</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @exception SocketException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        socketWrite(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Closes the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private boolean closing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // Prevent recursion. See BugId 4484411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (closing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        closing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (!socket.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            impl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        closing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Overrides finalize, the fd is closed by the Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43202
diff changeset
   178
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    protected void finalize() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   184
    private static native void init();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}