jdk/src/java.rmi/share/classes/sun/rmi/transport/tcp/MultiplexOutputStream.java
author rriggs
Wed, 11 May 2016 14:01:29 -0400
changeset 37892 55da13d60938
parent 25859 3317bb8137f4
permissions -rw-r--r--
8155978: Remove HTTP proxy implementation and tests from RMI Reviewed-by: smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 1996, 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
package sun.rmi.transport.tcp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
    30
 * MultiplexOutputStream manages sending data over a connection managed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * by a ConnectionMultiplexer object.  Data written is buffered until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * internal buffer is full or the flush() method is called, at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * point it attempts to push a packet of bytes through to the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * endpoint.  This will never push more bytes than the amount already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * requested by the remote endpoint (to prevent receive buffer from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * overflowing), so if the write() and flush() methods will block
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * until their operation can complete if enough bytes cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * pushed immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
final class MultiplexOutputStream extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /** object managing multiplexed connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private ConnectionMultiplexer manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /** information about the connection this is the output stream for */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private MultiplexConnectionInfo info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /** output buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private byte buffer[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /** current position to write to in output buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** pending number of bytes requested by remote endpoint */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int requested = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** true if this connection has been disconnected */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean disconnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * lock acquired to access shared variables:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * requested & disconnected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * WARNING:  Any of the methods manager.send*() should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * invoked while this lock is held, since they could potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * block if the underlying connection's transport buffers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * full, and the manager may need to acquire this lock to process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * and consume data coming over the underlying connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Create a new MultiplexOutputStream for the given manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param manager object that manages this connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param info structure for connection this stream writes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param bufferLength length of output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    MultiplexOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        ConnectionMultiplexer    manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        MultiplexConnectionInfo  info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int                      bufferLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.manager = manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.info    = info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        buffer = new byte[bufferLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Write a byte over connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param b byte of data to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public synchronized void write(int b) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        while (pos >= buffer.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            push();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        buffer[pos ++] = (byte) b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Write a subarray of bytes over connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param b array containing bytes to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param off offset of beginning of bytes to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param len number of bytes to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public synchronized void write(byte b[], int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (len <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // if enough free space in output buffer, just copy into there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int freeSpace = buffer.length - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (len <= freeSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.arraycopy(b, off, buffer, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            pos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // else, flush buffer and send rest directly to avoid array copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int local_requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                while ((local_requested = requested) < 1 && !disconnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        lock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                if (disconnected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    throw new IOException("Connection closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (local_requested < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                manager.sendTransmit(info, b, off, local_requested);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                off += local_requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                len -= local_requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    requested -= local_requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                manager.sendTransmit(info, b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    requested -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
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
     * Guarantee that all data written to this stream has been pushed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * over and made available to the remote endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public synchronized void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        while (pos > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            push();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Close this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public void close() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        manager.sendClose(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   174
     * Take note of more bytes requested by connection at remote endpoint.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param num number of additional bytes requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    void request(int num)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            requested += num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            lock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Disconnect this stream from all connection activity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    void disconnect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            disconnected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            lock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Push bytes in output buffer to connection at remote endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * This method blocks until at least one byte has been pushed across.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private void push() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int local_requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            while ((local_requested = requested) < 1 && !disconnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    lock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (disconnected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                throw new IOException("Connection closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (local_requested < pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            manager.sendTransmit(info, buffer, 0, local_requested);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            System.arraycopy(buffer, local_requested,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                             buffer, 0, pos - local_requested);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            pos -= local_requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                requested -= local_requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            manager.sendTransmit(info, buffer, 0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                requested -= pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
}