/*
* 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.
*/
/**
* MultiplexInputStream manages receiving data over a connection managed
* by a ConnectionMultiplexer object. This object is responsible for
* requesting more bytes of data as space in its internal buffer becomes
* available.
*
* @author Peter Jones
*/
/** object managing multiplexed connection */
/** information about the connection this is the input stream for */
/** input buffer */
private byte buffer[];
/** number of real data bytes present in buffer */
/** current position to read from in input buffer */
/** pending number of bytes this stream has requested */
/** true if this connection has been disconnected */
private boolean disconnected = false;
/**
* lock acquired to access shared variables:
* buffer, present, pos, 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.
*/
/** level at which more data is requested when read past */
private int waterMark;
/** data structure for holding reads of one byte */
/**
* Create a new MultiplexInputStream for the given manager.
* @param manager object that manages this connection
* @param info structure for connection this stream reads from
* @param bufferLength length of input buffer
*/
int bufferLength)
{
buffer = new byte[bufferLength];
}
/**
* Read a byte from the connection.
*/
{
if (n != 1)
return -1;
}
/**
* Read a subarray of bytes from connection. This method blocks for
* at least one byte, and it returns the number of bytes actually read,
* or -1 if the end of the stream was detected.
* @param b array to read bytes into
* @param off offset of beginning of bytes to read into
* @param len number of bytes to read
*/
{
if (len <= 0)
return 0;
int moreSpace;
synchronized (lock) {
pos = 0;
}
}
if (moreSpace > 0)
synchronized (lock) {
try {
} catch (InterruptedException e) {
}
}
return -1;
return len;
}
else {
// could send another request here, if len > available??
return available;
}
}
}
/**
* Return the number of bytes immediately available for reading.
*/
{
synchronized (lock) {
}
}
/**
* Close this connection.
*/
{
}
/**
* Receive bytes transmitted from connection at remote endpoint.
* @param length number of bytes transmitted
* @param in input stream with those bytes ready to be read
*/
throws IOException
{
/* TO DO: Optimize so that data received from stream can be loaded
* directly into user's buffer if there is a pending read().
*/
synchronized (lock) {
pos = 0;
}
throw new IOException("Receive buffer overflow");
}
}
/**
* Disconnect this stream from all connection activity.
*/
void disconnect()
{
synchronized (lock) {
disconnected = true;
}
}
}