src/jdk.net/linux/classes/jdk/internal/net/rdma/RdmaSocketOutputStream.java
author bpb
Wed, 06 Feb 2019 12:48:01 -0800
branchrsocket-branch
changeset 57156 81e4a12fd1a4
parent 57115 512e7cc6ccce
child 57160 c502c299d41e
permissions -rw-r--r--
rsocket-branch: change recent copyright year to 2019 and new files to 2019 only
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
57156
81e4a12fd1a4 rsocket-branch: change recent copyright year to 2019 and new files to 2019 only
bpb
parents: 57115
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     4
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    10
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    16
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    20
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    23
 * questions.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    24
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    25
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    26
package jdk.internal.net.rdma;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
import java.io.FileDescriptor;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    29
import java.io.FileOutputStream;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    30
import java.io.IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    31
import java.nio.channels.FileChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    32
import java.net.Socket;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    33
import java.net.SocketException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    34
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
class RdmaSocketOutputStream extends FileOutputStream
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
{
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
    static {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
        init();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
    private RdmaSocketImpl impl = null;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
    private byte temp[] = new byte[1];
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
    private Socket socket = null;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
    RdmaSocketOutputStream(RdmaSocketImpl impl) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
        super(impl.getFileDescriptor());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
        this.impl = impl;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
        socket = impl.getSocket();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
    public final FileChannel getChannel() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
        return null;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
    /**
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
     * Writes to the socket.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
     * @param fd the FileDescriptor
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
     * @param b the data to be written
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
     * @param off the start offset in the data
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
     * @param len the number of bytes that are written
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
     * @exception IOException If an I/O error has occurred.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
     */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
    private native void rdmaSocketWrite0(FileDescriptor fd, byte[] b,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
            int off, int len) throws IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
    /**
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
     * Writes to the socket with appropriate locking of the
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
     * FileDescriptor.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
     * @param b the data to be written
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
     * @param off the start offset in the data
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
     * @param len the number of bytes that are written
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
     * @exception IOException If an I/O error has occurred.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
     */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
    private void rdmaSocketWrite(byte b[], int off, int len)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
            throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
        if (len <= 0 || off < 0 || len > b.length - off) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
            if (len == 0) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
                return;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
            throw new ArrayIndexOutOfBoundsException("len == " + len
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
                    + " off == " + off + " buffer length == " + b.length);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
        FileDescriptor fd = impl.acquireFD();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
            rdmaSocketWrite0(fd, b, off, len);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
        } catch (SocketException se) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
            if (se instanceof sun.net.ConnectionResetException) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
                impl.setConnectionResetPending();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
                se = new SocketException("Connection reset");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
            if (impl.isClosedOrPending()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    93
                throw new SocketException("Socket closed");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    94
            } else {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
                throw se;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
        } finally {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
            impl.releaseFD();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
    /**
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
     * Writes a byte to the socket.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
     * @param b the data to be written
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
     * @exception IOException If an I/O error has occurred.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
     */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
    public void write(int b) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
        temp[0] = (byte)b;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
        rdmaSocketWrite(temp, 0, 1);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   110
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   111
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   112
    /**
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   113
     * Writes the contents of the buffer <i>b</i> to the socket.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   114
     * @param b the data to be written
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   115
     * @exception SocketException If an I/O error has occurred.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   116
     */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   117
    public void write(byte b[]) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   118
        rdmaSocketWrite(b, 0, b.length);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   119
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   120
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   121
    /**
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   122
     * Writes <i>length</i> bytes from buffer <i>b</i> starting at
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   123
     * offset <i>len</i>.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   124
     * @param b the data to be written
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   125
     * @param off the start offset in the data
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   126
     * @param len the number of bytes that are written
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   127
     * @exception SocketException If an I/O error has occurred.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   128
     */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   129
    public void write(byte b[], int off, int len) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   130
        rdmaSocketWrite(b, off, len);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   131
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   132
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   133
    /**
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   134
     * Closes the stream.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   135
     */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   136
    private boolean closing = false;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   137
    public void close() throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   138
        if (closing)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   139
            return;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   140
        closing = true;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   141
        if (socket != null) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   142
            if (!socket.isClosed())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   143
                socket.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   144
        } else
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   145
            impl.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   146
        closing = false;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   147
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   148
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   149
    /**
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   150
     * Perform class load-time initializations.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   151
     */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   152
    private static native void init();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   153
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   154
}