src/java.base/share/classes/java/net/SocketOutputStream.java
author prappo
Tue, 13 Nov 2018 12:24:34 +0000
changeset 52499 768b1c612100
parent 50425 43b54a307c89
child 54689 b28b7f631301
permissions -rw-r--r--
8213490: Networking area typos and inconsistencies cleanup Reviewed-by: alanb, chegar, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49249
92cca24c8807 8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents: 48224
diff changeset
     2
 * Copyright (c) 1995, 2018, 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
 */
49249
92cca24c8807 8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents: 48224
diff changeset
    41
class SocketOutputStream extends FileOutputStream {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private AbstractPlainSocketImpl impl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private byte temp[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private Socket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Creates a new SocketOutputStream. Can only be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * by a Socket. This method needs to hang on to the owner Socket so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * that the fd will not be closed.
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 50425
diff changeset
    54
     * @param impl the socket output stream implemented
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    SocketOutputStream(AbstractPlainSocketImpl impl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        super(impl.getFileDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.impl = impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        socket = impl.getSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * object associated with this file output stream. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    66
     * The {@code getChannel} method of {@code SocketOutputStream}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    67
     * returns {@code null} since it is a socket based stream.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @return  the file channel associated with this file output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public final FileChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Writes to the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param fd the FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private native void socketWrite0(FileDescriptor fd, byte[] b, int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                     int len) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Writes to the socket with appropriate locking of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * FileDescriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private void socketWrite(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
43202
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
    99
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   100
        if (len <= 0 || off < 0 || len > b.length - off) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
43202
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   104
            throw new ArrayIndexOutOfBoundsException("len == " + len
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   105
                    + " off == " + off + " buffer length == " + b.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        FileDescriptor fd = impl.acquireFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            socketWrite0(fd, b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (SocketException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (impl.isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                throw new SocketException("Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            impl.releaseFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Writes a byte to the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        temp[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        socketWrite(temp, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Writes the contents of the buffer <i>b</i> to the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @exception SocketException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        socketWrite(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Writes <i>length</i> bytes from buffer <i>b</i> starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * offset <i>len</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @exception SocketException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        socketWrite(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Closes the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private boolean closing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // Prevent recursion. See BugId 4484411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (closing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        closing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (!socket.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            impl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        closing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Overrides finalize, the fd is closed by the Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47216
diff changeset
   173
    @SuppressWarnings({"deprecation", "removal"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    protected void finalize() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   179
    private static native void init();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}