src/java.base/share/classes/java/net/SocketOutputStream.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 54689 b28b7f631301
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 52499
diff changeset
     2
 * Copyright (c) 1995, 2019, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Creates a new SocketOutputStream. Can only be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * by a Socket. This method needs to hang on to the owner Socket so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * that the fd will not be closed.
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 50425
diff changeset
    53
     * @param impl the socket output stream implemented
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    SocketOutputStream(AbstractPlainSocketImpl impl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        super(impl.getFileDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.impl = impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * object associated with this file output stream. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    64
     * The {@code getChannel} method of {@code SocketOutputStream}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    65
     * returns {@code null} since it is a socket based stream.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @return  the file channel associated with this file output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public final FileChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Writes to the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param fd the FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param len the number of bytes that are written
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54689
diff changeset
    82
     * @throws    IOException If an I/O error has occurred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private native void socketWrite0(FileDescriptor fd, byte[] b, int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                     int len) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Writes to the socket with appropriate locking of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * FileDescriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param len the number of bytes that are written
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54689
diff changeset
    93
     * @throws    IOException If an I/O error has occurred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private void socketWrite(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
43202
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
    97
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
    98
        if (len <= 0 || off < 0 || len > b.length - off) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
43202
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   102
            throw new ArrayIndexOutOfBoundsException("len == " + len
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 32649
diff changeset
   103
                    + " off == " + off + " buffer length == " + b.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        FileDescriptor fd = impl.acquireFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            socketWrite0(fd, b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (SocketException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (impl.isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new SocketException("Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            impl.releaseFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Writes a byte to the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param b the data to be written
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54689
diff changeset
   123
     * @throws    IOException If an I/O error has occurred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        temp[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        socketWrite(temp, 0, 1);
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
     * Writes the contents of the buffer <i>b</i> to the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param b the data to be written
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54689
diff changeset
   133
     * @throws    SocketException If an I/O error has occurred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        socketWrite(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Writes <i>length</i> bytes from buffer <i>b</i> starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * offset <i>len</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param len the number of bytes that are written
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54689
diff changeset
   145
     * @throws    SocketException If an I/O error has occurred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        socketWrite(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public void close() throws IOException {
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 52499
diff changeset
   152
        // No longer used. Socket.getOutputStream returns an
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 52499
diff changeset
   153
        // OutputStream which calls Socket.close directly
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 52499
diff changeset
   154
        assert false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Overrides finalize, the fd is closed by the Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47216
diff changeset
   160
    @SuppressWarnings({"deprecation", "removal"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    protected void finalize() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   166
    private static native void init();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}