/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* MultiplexOutputStream manages sending data over a conection managed
* by a ConnectionMultiplexer object. Data written is buffered until the
* internal buffer is full or the flush() method is called, at which
* point it attempts to push a packet of bytes through to the remote
* endpoint. This will never push more bytes than the amount already
* requested by the remote endpoint (to prevent receive buffer from
* overflowing), so if the write() and flush() methods will block
* until their operation can complete if enough bytes cannot be
* pushed immediately.
*
* @author Peter Jones
*/
/** object managing multiplexed connection */
/** information about the connection this is the output stream for */
/** output buffer */
private byte buffer[];
/** current position to write to in output buffer */
/** pending number of bytes requested by remote endpoint */
/** true if this connection has been disconnected */
private boolean disconnected = false;
/**
* lock acquired to access shared variables:
* requested & disconnected
* WARNING: Any of the methods manager.send*() should not be
* invoked while this lock is held, since they could potentially
* block if the underlying connection's transport buffers are
* full, and the manager may need to acquire this lock to process
* and consume data coming over the underlying connection.
*/
/**
* Create a new MultiplexOutputStream for the given manager.
* @param manager object that manages this connection
* @param info structure for connection this stream writes to
* @param bufferLength length of output buffer
*/
int bufferLength)
{
buffer = new byte[bufferLength];
pos = 0;
}
/**
* Write a byte over connection.
* @param b byte of data to write
*/
{
push();
}
/**
* Write a subarray of bytes over connection.
* @param b array containing bytes to write
* @param off offset of beginning of bytes to write
* @param len number of bytes to write
*/
throws IOException
{
if (len <= 0)
return;
// if enough free space in output buffer, just copy into there
return;
}
// else, flush buffer and send rest directly to avoid array copy
flush();
int local_requested;
while (true) {
synchronized (lock) {
try {
} catch (InterruptedException e) {
}
}
if (disconnected)
throw new IOException("Connection closed");
}
if (local_requested < len) {
off += local_requested;
len -= local_requested;
synchronized (lock) {
}
}
else {
synchronized (lock) {
}
// len = 0;
break;
}
}
}
/**
* Guarantee that all data written to this stream has been pushed
* over and made available to the remote endpoint.
*/
while (pos > 0)
push();
}
/**
* Close this connection.
*/
{
}
/**
* Take note of more bytes requested by conection at remote endpoint.
* @param num number of additional bytes requested
*/
{
synchronized (lock) {
}
}
/**
* Disconnect this stream from all connection activity.
*/
void disconnect()
{
synchronized (lock) {
disconnected = true;
}
}
/**
* Push bytes in output buffer to connection at remote endpoint.
* This method blocks until at least one byte has been pushed across.
*/
{
int local_requested;
synchronized (lock) {
try {
} catch (InterruptedException e) {
}
}
if (disconnected)
throw new IOException("Connection closed");
}
if (local_requested < pos) {
pos -= local_requested;
synchronized (lock) {
}
}
else {
synchronized (lock) {
}
pos = 0;
}
}
}