jdk/src/share/classes/java/net/SocketOutputStream.java
author juh
Tue, 30 Jul 2013 11:04:19 -0700
changeset 19069 1d9cb0d080e3
parent 5506 202f599c92aa
permissions -rw-r--r--
8021833: javadoc cleanup in java.net Summary: <code> and <tt> converted to {@code }; package.html to package-info.java Reviewed-by: darcy, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
     2
 * Copyright (c) 1995, 2013, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (len <= 0 || off < 0 || off + len > b.length) {
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        FileDescriptor fd = impl.acquireFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            socketWrite0(fd, b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } catch (SocketException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (se instanceof sun.net.ConnectionResetException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                impl.setConnectionResetPending();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                se = new SocketException("Connection reset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (impl.isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                throw new SocketException("Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            impl.releaseFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Writes a byte to the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        temp[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        socketWrite(temp, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Writes the contents of the buffer <i>b</i> to the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception SocketException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        socketWrite(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Writes <i>length</i> bytes from buffer <i>b</i> starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * offset <i>len</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @exception SocketException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        socketWrite(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Closes the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private boolean closing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // Prevent recursion. See BugId 4484411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (closing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        closing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (!socket.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            impl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        closing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Overrides finalize, the fd is closed by the Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    protected void finalize() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private native static void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}