User: chirino
Date: 01/08/16 20:04:04
Modified: src/main/org/jboss/mq/il/uil/multiplexor
DemuxInputStream.java MultiplexorTest.java
MuxOutputStream.java SocketMultiplexor.java
StreamDemux.java StreamMux.java
Log:
Used the ejbdoclet pretty task to auto-indent the source files to
the 3 space standard.
Revision Changes Path
1.2 +131 -126
jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/DemuxInputStream.java
Index: DemuxInputStream.java
===================================================================
RCS file:
/cvsroot/jboss/jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/DemuxInputStream.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DemuxInputStream.java 2001/08/11 20:59:14 1.1
+++ DemuxInputStream.java 2001/08/17 03:04:04 1.2
@@ -5,137 +5,142 @@
* See terms of license at gnu.org.
*/
package org.jboss.mq.il.uil.multiplexor;
-
-import java.io.InterruptedIOException;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InterruptedIOException;
+
/**
- * Objects of this class provide and an InputStream
- * from a StreamDemux.
+ * Objects of this class provide and an InputStream from a StreamDemux. Objects
+ * of this class are created by a StreamDemux object.
*
- * Objects of this class are created by a StreamDemux object.
- *
- * @author Hiram Chirino ([EMAIL PROTECTED])
- *
- * @version $Revision: 1.1 $
+ * @author Hiram Chirino ([EMAIL PROTECTED])
+ * @created August 16, 2001
+ * @version $Revision: 1.2 $
*/
class DemuxInputStream extends InputStream {
+
+ StreamDemux streamDemux;
+ short streamId;
+ boolean atEOF = false;
+
+ Object bufferMutex = new Object();
+ byte buffer[];
+ short bufferEndPos;
+ short bufferStartPos;
+
+ DemuxInputStream( StreamDemux demux, short id ) {
+ streamDemux = demux;
+ streamId = id;
+ buffer = new byte[1000];
+ bufferStartPos = 0;
+ bufferEndPos = 0;
+ }
+
+ public int available()
+ throws IOException {
+ return getBufferFillSize();
+ }
+
+ public void close()
+ throws IOException {
+ streamDemux.closeStream( streamId );
+ }
+
+ public void loadBuffer( byte data[], short dataLength )
+ throws IOException {
+ int freeSize = 0;
+ int dataPos = 0;
+ while ( dataPos < dataLength ) {
+ synchronized ( bufferMutex ) {
+ while ( ( freeSize = getBufferFreeSize() ) == 0 ) {
+ try {
+ // Wait till the consumer notifies us he has
+ // removed some data from the buffer.
+ bufferMutex.wait();
+ } catch ( InterruptedException e ) {
+ throw new InterruptedIOException( e.getMessage() );
+ }
+ }
+ // the buffer should have free space now.
+ freeSize = Math.min( freeSize, dataLength - dataPos );
+ for ( int i = 0; i < freeSize; i++ ) {
+ buffer[bufferEndPos++] = data[dataPos + i];
+ bufferEndPos = bufferEndPos == buffer.length ? 0 : bufferEndPos;
+ }
+ }
+ dataPos += freeSize;
+ // the consumer might be waiting for bytes to come in
+ synchronized ( bufferMutex ) {
+ bufferMutex.notify();
+ }
+ }
+ }
+
+ public int read()
+ throws IOException {
+ if ( bufferStartPos == bufferEndPos && atEOF ) {
+ return -1;
+ }
+ synchronized ( bufferMutex ) {
+ // Wait till the buffer has data
+ while ( !atEOF && bufferStartPos == bufferEndPos && !streamDemux.pumpData(
this ) ) {
+ try {
+ // Wait till the producer notifies us he has
+ // put some data in the buffer.
+ bufferMutex.wait();
+ } catch ( InterruptedException e ) {
+ throw new InterruptedIOException( e.getMessage() );
+ }
+ }
+ }
+ // We might break out due to EOF
+ if ( bufferStartPos == bufferEndPos ) {
+ return -1;
+ }
+ // the buffer should have data now.
+ byte result = buffer[bufferStartPos++];
+ bufferStartPos = bufferStartPos == buffer.length ? 0 : bufferStartPos;
+ // the producer might be waiting for free space in the
+ // buffer, we have to notify him.
+ synchronized ( bufferMutex ) {
+ bufferMutex.notify();
+ }
+ return result & 0xff;
+ }
+
+ public int read( byte b[], int off, int len )
+ throws IOException {
+ if ( b == null ) {
+ throw new NullPointerException();
+ } else if ( ( off < 0 ) || ( off > b.length ) || ( len < 0 ) || ( ( off + len
) > b.length ) || ( ( off + len ) < 0 ) ) {
+ throw new IndexOutOfBoundsException();
+ } else if ( len == 0 ) {
+ return 0;
+ }
+
+ int c = read();
+ if ( c == -1 ) {
+ return -1;
+ }
+ b[off] = ( byte )c;
+ len = Math.min( available(), len );
+ int i = 1;
+ try {
+ for ( ; i < len; i++ ) {
+ c = read();
+ b[off + i] = ( byte )c;
+ }
+ } catch ( IOException ee ) {
+ }
+ return i;
+ }
+
+ private int getBufferFillSize() {
+ return bufferStartPos <= bufferEndPos ? bufferEndPos - bufferStartPos :
buffer.length - ( bufferStartPos - bufferEndPos );
+ }
- StreamDemux streamDemux;
- short streamId;
- boolean atEOF = false;
-
- Object bufferMutex = new Object();
- byte buffer[];
- short bufferEndPos;
- short bufferStartPos;
-
- DemuxInputStream(StreamDemux demux, short id) {
- streamDemux = demux;
- streamId = id;
- buffer = new byte[1000];
- bufferStartPos = 0;
- bufferEndPos = 0;
- }
-
- public int available() throws IOException {
- return getBufferFillSize();
- }
-
- public void close() throws IOException {
- streamDemux.closeStream(streamId);
- }
-
- private int getBufferFillSize() {
- return bufferStartPos <= bufferEndPos ? bufferEndPos - bufferStartPos
: buffer.length - (bufferStartPos - bufferEndPos);
- }
-
- private int getBufferFreeSize() {
- return (buffer.length - 1) - getBufferFillSize();
- }
-
- public void loadBuffer(byte data[], short dataLength) throws IOException {
- int freeSize = 0;
- int dataPos = 0;
- while (dataPos < dataLength) {
- synchronized (bufferMutex) {
- while ((freeSize = getBufferFreeSize()) == 0) {
- try {
- // Wait till the consumer notifies us
he has
- // removed some data from the buffer.
- bufferMutex.wait();
- } catch (InterruptedException e) {
- throw new
InterruptedIOException(e.getMessage());
- }
- }
- // the buffer should have free space now.
- freeSize = Math.min(freeSize, dataLength - dataPos);
- for (int i = 0; i < freeSize; i++) {
- buffer[bufferEndPos++] = data[dataPos + i];
- bufferEndPos = bufferEndPos == buffer.length ?
0 : bufferEndPos;
- }
- }
- dataPos += freeSize;
- // the consumer might be waiting for bytes to come in
- synchronized (bufferMutex) {
- bufferMutex.notify();
- }
- }
- }
-
- public int read() throws IOException {
- if (bufferStartPos == bufferEndPos && atEOF)
- return -1;
- synchronized (bufferMutex) {
- // Wait till the buffer has data
- while (!atEOF && bufferStartPos == bufferEndPos &&
!streamDemux.pumpData(this)) {
- try {
- // Wait till the producer notifies us he has
- // put some data in the buffer.
- bufferMutex.wait();
- } catch (InterruptedException e) {
- throw new
InterruptedIOException(e.getMessage());
- }
- }
- }
- // We might break out due to EOF
- if (bufferStartPos == bufferEndPos)
- return -1;
- // the buffer should have data now.
- byte result = buffer[bufferStartPos++];
- bufferStartPos = bufferStartPos == buffer.length ? 0 : bufferStartPos;
- // the producer might be waiting for free space in the
- // buffer, we have to notify him.
- synchronized (bufferMutex) {
- bufferMutex.notify();
- }
- return result & 0xff;
- }
-
- public int read(byte b[], int off, int len) throws IOException {
- if (b == null) {
- throw new NullPointerException();
- } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len)
> b.length) || ((off + len) < 0)) {
- throw new IndexOutOfBoundsException();
- } else if (len == 0) {
- return 0;
- }
-
- int c = read();
- if (c == -1) {
- return -1;
- }
- b[off] = (byte) c;
- len = Math.min(available(), len);
- int i = 1;
- try {
- for (; i < len; i++) {
- c = read();
- b[off + i] = (byte) c;
- }
- } catch (IOException ee) {
- }
- return i;
- }
-}
\ No newline at end of file
+ private int getBufferFreeSize() {
+ return ( buffer.length - 1 ) - getBufferFillSize();
+ }
+}
1.2 +124 -114
jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/MultiplexorTest.java
Index: MultiplexorTest.java
===================================================================
RCS file:
/cvsroot/jboss/jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/MultiplexorTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MultiplexorTest.java 2001/08/11 20:59:14 1.1
+++ MultiplexorTest.java 2001/08/17 03:04:04 1.2
@@ -5,129 +5,139 @@
* See terms of license at gnu.org.
*/
package org.jboss.mq.il.uil.multiplexor;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.io.InterruptedIOException;
import java.io.ObjectInputStream;
-import java.io.OutputStream;
-import java.io.IOException;
-import java.io.BufferedInputStream;
-import java.io.PipedOutputStream;
import java.io.ObjectOutputStream;
-import java.io.InterruptedIOException;
-import java.io.BufferedOutputStream;
+import java.io.OutputStream;
import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
/**
- * This class is a unit tester of the
- * StreamMux and StreamDemux classes.
- *
- * Starts 3 concurent readers and 3 concurent
- * writers using 3 fully buffered object streams multiplexed over
- * a single Pipe(Input/Output)Stream. The
- * writers send a 10K message with a timestamp.
+ * This class is a unit tester of the StreamMux and StreamDemux classes. Starts
+ * 3 concurent readers and 3 concurent writers using 3 fully buffered object
+ * streams multiplexed over a single Pipe(Input/Output)Stream. The writers send
+ * a 10K message with a timestamp. Readers display how long the message took to
+ * arrive.
*
- * Readers display how long the message took to arrive.
- *
- * @author Hiram Chirino ([EMAIL PROTECTED])
- *
- * @version $Revision: 1.1 $
+ * @author Hiram Chirino ([EMAIL PROTECTED])
+ * @created August 16, 2001
+ * @version $Revision: 1.2 $
*/
public class MultiplexorTest {
-
- StreamMux mux;
- StreamDemux demux;
-
- public static final int PAY_LOAD_SIZE = 1024 * 10;
- public static char[] PAY_LOAD;
-
- class WriterThread extends Thread {
- ObjectOutputStream os;
- short id;
- WriterThread(short id) throws IOException {
- super("WriterThread");
- this.os = new ObjectOutputStream(new
BufferedOutputStream(mux.getStream(id)));
- this.os.flush();
- }
- public void run() {
- try {
- for (int i = 0; i < 1000; i++) {
- os.writeLong(System.currentTimeMillis());
- os.writeObject(PAY_LOAD);
- os.flush();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- class ReaderThread extends Thread {
- ObjectInputStream is;
- short id;
- ReaderThread(short id) throws IOException {
- super("ReaderThread");
- this.is = new ObjectInputStream(new
BufferedInputStream(demux.getStream(id)));
- this.id = id;
- }
-
- public void run() {
- try {
- for (int i = 0; i < 1000; i++) {
- long t = is.readLong();
- is.readObject();
- t = System.currentTimeMillis() - t;
- System.out.println("" + id + ": Packet " + i +
" Latency : " + ((double) t / (double) 1000));
- System.out.flush();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
- /**
- * MuxDemuxTester constructor
- */
- public MultiplexorTest() {
- super();
-
- char s[] = new char[PAY_LOAD_SIZE];
- char c = 'A';
- for (int i = 0; i < PAY_LOAD_SIZE; i++) {
- s[i] = c;
- c++;
- c = c > 'Z' ? 'A' : c;
- }
- PAY_LOAD = s;
- }
-
- public void connect() throws IOException {
- PipedInputStream pis = new PipedInputStream();
- PipedOutputStream pos = new PipedOutputStream(pis);
-
- mux = new StreamMux(pos);
- demux = new StreamDemux(pis);
- }
-
- public static void main(String args[]) throws Exception {
-
- System.out.println("Initializing");
- MultiplexorTest tester = new MultiplexorTest();
-
- System.out.println("Connecting the streams");
- tester.connect();
- System.out.println("Starting stream 1");
- tester.startStream((short) 1);
- System.out.println("Starting stream 2");
- tester.startStream((short) 2);
- System.out.println("Starting stream 3");
- tester.startStream((short) 3);
-
- }
- public void startStream(short id) throws IOException {
+ StreamMux mux;
+ StreamDemux demux;
- new WriterThread(id).start();
- new ReaderThread(id).start();
+ public final static int PAY_LOAD_SIZE = 1024 * 10;
+ public static char[] PAY_LOAD;
- }
-}
\ No newline at end of file
+ /**
+ * MuxDemuxTester constructor
+ */
+ public MultiplexorTest() {
+ super();
+
+ char s[] = new char[PAY_LOAD_SIZE];
+ char c = 'A';
+ for ( int i = 0; i < PAY_LOAD_SIZE; i++ ) {
+ s[i] = c;
+ c++;
+ c = c > 'Z' ? 'A' : c;
+ }
+ PAY_LOAD = s;
+ }
+
+ public void connect()
+ throws IOException {
+ PipedInputStream pis = new PipedInputStream();
+ PipedOutputStream pos = new PipedOutputStream( pis );
+
+ mux = new StreamMux( pos );
+ demux = new StreamDemux( pis );
+ }
+
+ public void startStream( short id )
+ throws IOException {
+
+ new WriterThread( id ).start();
+ new ReaderThread( id ).start();
+
+ }
+
+ public static void main( String args[] )
+ throws Exception {
+
+ System.out.println( "Initializing" );
+ MultiplexorTest tester = new MultiplexorTest();
+
+ System.out.println( "Connecting the streams" );
+ tester.connect();
+ System.out.println( "Starting stream 1" );
+ tester.startStream( ( short )1 );
+ System.out.println( "Starting stream 2" );
+ tester.startStream( ( short )2 );
+ System.out.println( "Starting stream 3" );
+ tester.startStream( ( short )3 );
+
+ }
+
+ /**
+ * @created August 16, 2001
+ */
+ class WriterThread extends Thread {
+ ObjectOutputStream os;
+ short id;
+
+ WriterThread( short id )
+ throws IOException {
+ super( "WriterThread" );
+ this.os = new ObjectOutputStream( new BufferedOutputStream( mux.getStream(
id ) ) );
+ this.os.flush();
+ }
+
+ public void run() {
+ try {
+ for ( int i = 0; i < 1000; i++ ) {
+ os.writeLong( System.currentTimeMillis() );
+ os.writeObject( PAY_LOAD );
+ os.flush();
+ }
+ } catch ( IOException e ) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * @created August 16, 2001
+ */
+ class ReaderThread extends Thread {
+ ObjectInputStream is;
+ short id;
+
+ ReaderThread( short id )
+ throws IOException {
+ super( "ReaderThread" );
+ this.is = new ObjectInputStream( new BufferedInputStream( demux.getStream(
id ) ) );
+ this.id = id;
+ }
+
+ public void run() {
+ try {
+ for ( int i = 0; i < 1000; i++ ) {
+ long t = is.readLong();
+ is.readObject();
+ t = System.currentTimeMillis() - t;
+ System.out.println( "" + id + ": Packet " + i + " Latency : " + ( (
double )t / ( double )1000 ) );
+ System.out.flush();
+ }
+ } catch ( Exception e ) {
+ e.printStackTrace();
+ }
+ }
+ }
+}
1.2 +71 -61
jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/MuxOutputStream.java
Index: MuxOutputStream.java
===================================================================
RCS file:
/cvsroot/jboss/jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/MuxOutputStream.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MuxOutputStream.java 2001/08/11 20:59:14 1.1
+++ MuxOutputStream.java 2001/08/17 03:04:04 1.2
@@ -5,71 +5,81 @@
* See terms of license at gnu.org.
*/
package org.jboss.mq.il.uil.multiplexor;
+import java.io.IOException;
+import java.io.InterruptedIOException;
import java.io.OutputStream;
-import java.io.InterruptedIOException;
-import java.io.IOException;
/**
- * Objects of this class provide and an OutputStream
- * to a StreamMux.
- *
- * Objects of this class are created by a StreamMux object.
+ * Objects of this class provide and an OutputStream to a StreamMux. Objects of
+ * this class are created by a StreamMux object.
*
- * @author Hiram Chirino ([EMAIL PROTECTED])
- *
- * @version $Revision: 1.1 $
+ * @author Hiram Chirino ([EMAIL PROTECTED])
+ * @created August 16, 2001
+ * @version $Revision: 1.2 $
*/
class MuxOutputStream extends OutputStream {
-
- StreamMux streamMux;
- short streamId;
-
- byte buffer[];
- short bufferLength;
-
- MuxOutputStream(StreamMux mux, short id) {
- streamMux = mux;
- streamId = id;
- buffer = new byte[streamMux.frameSize];
- bufferLength = 0;
- }
-
- /**
- * Closes this output stream and releases any system resources.
- */
- public void close() throws IOException {
- streamMux.closeStream(streamId);
- }
-
- /**
- * Flushes this output stream and forces any buffered output bytes
- * to be written out.
- */
- public void flush() throws IOException {
- if (bufferLength > 0)
- flushBuffer();
- streamMux.flush();
- }
-
- /**
- * Flushes the internal buffer to the multiplexor.
- */
- private void flushBuffer() throws IOException {
- streamMux.write(this, buffer, bufferLength);
- bufferLength = 0;
- }
-
- /**
- *
- */
- public void write(int data) throws IOException {
- byte b = (byte) data;
- buffer[bufferLength] = b;
- bufferLength++;
-
- if (bufferLength == streamMux.frameSize) {
- flushBuffer();
- }
- }
-}
\ No newline at end of file
+
+ StreamMux streamMux;
+ short streamId;
+
+ byte buffer[];
+ short bufferLength;
+
+ MuxOutputStream( StreamMux mux, short id ) {
+ streamMux = mux;
+ streamId = id;
+ buffer = new byte[streamMux.frameSize];
+ bufferLength = 0;
+ }
+
+ /**
+ * Closes this output stream and releases any system resources.
+ *
+ * @exception IOException Description of Exception
+ */
+ public void close()
+ throws IOException {
+ streamMux.closeStream( streamId );
+ }
+
+ /**
+ * Flushes this output stream and forces any buffered output bytes to be
+ * written out.
+ *
+ * @exception IOException Description of Exception
+ */
+ public void flush()
+ throws IOException {
+ if ( bufferLength > 0 ) {
+ flushBuffer();
+ }
+ streamMux.flush();
+ }
+
+ /**
+ * @param data Description of Parameter
+ * @exception IOException Description of Exception
+ */
+ public void write( int data )
+ throws IOException {
+ byte b = ( byte )data;
+ buffer[bufferLength] = b;
+ bufferLength++;
+
+ if ( bufferLength == streamMux.frameSize ) {
+ flushBuffer();
+ }
+ }
+
+ /**
+ * Flushes the internal buffer to the multiplexor.
+ *
+ * @exception IOException Description of Exception
+ */
+ private void flushBuffer()
+ throws IOException {
+ streamMux.write( this, buffer, bufferLength );
+ bufferLength = 0;
+ }
+}
1.2 +64 -60
jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/SocketMultiplexor.java
Index: SocketMultiplexor.java
===================================================================
RCS file:
/cvsroot/jboss/jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/SocketMultiplexor.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SocketMultiplexor.java 2001/08/11 20:59:14 1.1
+++ SocketMultiplexor.java 2001/08/17 03:04:04 1.2
@@ -5,71 +5,75 @@
* See terms of license at gnu.org.
*/
package org.jboss.mq.il.uil.multiplexor;
-
-import java.net.Socket;
-import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+
/**
- * Used to multiplex a socket's streams.
- *
- * With this this interface you can access the multiplexed
- * streams of the socket. The multiplexed streams are
- * identifed a stream id.
+ * Used to multiplex a socket's streams. With this this interface you can
+ * access the multiplexed streams of the socket. The multiplexed streams are
+ * identifed a stream id. Stream id 0 is reserved for internal use of the
+ * multiplexor.
*
- * Stream id 0 is reserved for internal use of the multiplexor.
- *
- * @author Hiram Chirino ([EMAIL PROTECTED])
- *
- * @version $Revision: 1.1 $
+ * @author Hiram Chirino ([EMAIL PROTECTED])
+ * @created August 16, 2001
+ * @version $Revision: 1.2 $
*/
public class SocketMultiplexor {
+ StreamMux mux;
+ StreamDemux demux;
+
+ private Socket socket;
+
+ public SocketMultiplexor( Socket s )
+ throws IOException {
+ socket = s;
+ mux = new StreamMux( s.getOutputStream() );
+ demux = new StreamDemux( s.getInputStream() );
+ }
+
+ /**
+ * Creation date: (11/16/00 1:15:01 PM)
+ *
+ * @return org.jboss.mq.connection.StreamDemux
+ */
+ public StreamDemux getDemux() {
+ return demux;
+ }
+
+ public InputStream getInputStream( int id )
+ throws IOException {
+ return demux.getStream( ( short )id );
+ }
+
+ /**
+ * Creation date: (11/16/00 1:15:01 PM)
+ *
+ * @return org.jboss.mq.connection.StreamMux
+ */
+ public StreamMux getMux() {
+ return mux;
+ }
+
+ public OutputStream getOutputStream( int id )
+ throws IOException {
+ return mux.getStream( ( short )id );
+ }
+
+ /**
+ * Creation date: (11/16/00 1:14:41 PM)
+ *
+ * @return java.net.Socket
+ */
+ public java.net.Socket getSocket() {
+ return socket;
+ }
+
+ public void close()
+ throws IOException {
+ getSocket().close();
+ }
- private Socket socket;
- StreamMux mux;
- StreamDemux demux;
-
- public SocketMultiplexor(Socket s) throws IOException {
- socket = s;
- mux = new StreamMux(s.getOutputStream());
- demux = new StreamDemux(s.getInputStream());
- }
-
- /**
- * Creation date: (11/16/00 1:15:01 PM)
- * @return org.jboss.mq.connection.StreamDemux
- */
- public StreamDemux getDemux() {
- return demux;
- }
-
- public InputStream getInputStream(int id) throws IOException {
- return demux.getStream((short) id);
- }
-
- /**
- * Creation date: (11/16/00 1:15:01 PM)
- * @return org.jboss.mq.connection.StreamMux
- */
- public StreamMux getMux() {
- return mux;
- }
-
- public OutputStream getOutputStream(int id) throws IOException {
- return mux.getStream((short) id);
- }
-
- /**
- * Creation date: (11/16/00 1:14:41 PM)
- * @return java.net.Socket
- */
- public java.net.Socket getSocket() {
- return socket;
- }
-
- public void close() throws IOException {
- getSocket().close();
- }
-
-}
\ No newline at end of file
+}
1.2 +183 -162
jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/StreamDemux.java
Index: StreamDemux.java
===================================================================
RCS file:
/cvsroot/jboss/jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/StreamDemux.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- StreamDemux.java 2001/08/11 20:59:14 1.1
+++ StreamDemux.java 2001/08/17 03:04:04 1.2
@@ -10,169 +10,190 @@
import java.util.*;
/**
- * This class is used to demultiplex from
- * a single stream into multiple streams.
+ * This class is used to demultiplex from a single stream into multiple
+ * streams.
*
- * @author Hiram Chirino ([EMAIL PROTECTED])
- *
- * @version $Revision: 1.1 $
+ * @author Hiram Chirino ([EMAIL PROTECTED])
+ * @created August 16, 2001
+ * @version $Revision: 1.2 $
*/
public class StreamDemux {
- short frameSize = 512;
- HashMap openStreams = new HashMap();
- InputStream in;
- DataInputStream objectIn;
-
- boolean pumpingData = false;
- byte inputBuffer[] = new byte[frameSize];
-
- /**
- * StreamMux constructor comment.
- * @param out java.io.OutputStream
- */
- public StreamDemux(InputStream in) throws IOException {
- this.in = in;
- this.objectIn = new DataInputStream(in);
- }
-
- void closeStream(short id) throws IOException {
- if (id == 0)
- throw new IOException("Stream id 0 is reserved for internal
use.");
-
- synchronized (openStreams) {
- openStreams.remove(new Short(id));
- }
-
- }
-
- /**
- * Creation date: (11/15/00 5:30:55 PM)
- * @return short
- */
- public short getFrameSize() {
- synchronized (openStreams) {
- return frameSize;
- }
- }
-
- public InputStream getStream(short id) throws IOException {
- if (id == 0)
- throw new IOException("Stream id 0 is reserved for internal
use.");
-
- InputStream s;
- synchronized (openStreams) {
- s = (InputStream) openStreams.get(new Short(id));
- ;
-
- if (s != null)
- return s;
-
- s = new DemuxInputStream(this, id);
- openStreams.put(new Short(id), s);
- }
- return s;
- }
-
- /**
- * Creation date: (11/15/00 5:30:55 PM)
- * @param newFrameSize short
- */
- public void setFrameSize(short newFrameSize) throws IOException {
- synchronized (openStreams) {
- if (openStreams.size() > 0)
- throw new IOException("Cannot change the frame size
while there are open streams.");
- frameSize = newFrameSize;
- inputBuffer = new byte[frameSize];
- }
- }
-
- public int available(DemuxInputStream s) throws IOException {
- return objectIn.available();
- }
-
- /**
- * Pumps data to all input streams until data for the
- * dest Stream arrives. Only on thread is allowed to pump
- * data at a time and this method returns true if it pumped
- * data into its input buffer. It returns false if another
- *thread is allready pumping data.
- */
- public boolean pumpData(DemuxInputStream dest) throws IOException {
-
- synchronized (this) {
- if (pumpingData)
- return false;
- else
- pumpingData = true;
- }
-
- // Start pumping the data
- short nextFrameSize = frameSize;
- while (true) {
- short streamId = objectIn.readShort();
- // Was it a command on the admin stream?
- if (streamId == 0) {
- // Next byte is the command.
- switch (objectIn.readByte()) {
- case StreamMux.OPEN_STREAM_COMMAND :
- getStream(objectIn.readShort());
- break;
- case StreamMux.CLOSE_STREAM_COMMAND :
- DemuxInputStream s;
- synchronized (openStreams) {
- s = (DemuxInputStream)
openStreams.get(new Short(objectIn.readShort()));
- }
- if (s != null) {
- closeStream(s.streamId);
- s.atEOF = true;
- if (s == dest) {
- break;
- } else {
- // Wake up the thread
that was waiting for input (it got a EOF)
- synchronized
(s.bufferMutex) {
-
s.bufferMutex.notify();
- }
- }
- }
- break;
- case StreamMux.NEXT_FRAME_SHORT_COMMAND :
- nextFrameSize = objectIn.readShort();
- break;
- }
-
- } else {
- objectIn.readFully(inputBuffer, 0, nextFrameSize);
- DemuxInputStream s;
- synchronized (openStreams) {
- s = (DemuxInputStream) openStreams.get(new
Short(streamId));
- }
- if (s == null)
- continue;
-
- s.loadBuffer(inputBuffer, nextFrameSize);
- if (s == dest)
- break;
-
- nextFrameSize = frameSize;
- }
- }
-
- synchronized (this) {
- pumpingData = false;
- }
-
- // we are done pumping but another thread may be
- // intrested in pumping.
- synchronized (openStreams) {
- Iterator iter = openStreams.values().iterator();
- while (iter.hasNext() && pumpingData == false) {
- DemuxInputStream s = (DemuxInputStream) iter.next();
- synchronized (s.bufferMutex) {
- s.bufferMutex.notify();
- }
- }
- }
- return true;
- }
-}
\ No newline at end of file
+ short frameSize = 512;
+ HashMap openStreams = new HashMap();
+ InputStream in;
+ DataInputStream objectIn;
+
+ boolean pumpingData = false;
+ byte inputBuffer[] = new byte[frameSize];
+
+ /**
+ * StreamMux constructor comment.
+ *
+ * @param in Description of Parameter
+ * @exception IOException Description of Exception
+ */
+ public StreamDemux( InputStream in )
+ throws IOException {
+ this.in = in;
+ this.objectIn = new DataInputStream( in );
+ }
+
+ /**
+ * Creation date: (11/15/00 5:30:55 PM)
+ *
+ * @param newFrameSize short
+ * @exception IOException Description of Exception
+ */
+ public void setFrameSize( short newFrameSize )
+ throws IOException {
+ synchronized ( openStreams ) {
+ if ( openStreams.size() > 0 ) {
+ throw new IOException( "Cannot change the frame size while there are
open streams." );
+ }
+ frameSize = newFrameSize;
+ inputBuffer = new byte[frameSize];
+ }
+ }
+
+ /**
+ * Creation date: (11/15/00 5:30:55 PM)
+ *
+ * @return short
+ */
+ public short getFrameSize() {
+ synchronized ( openStreams ) {
+ return frameSize;
+ }
+ }
+
+ public InputStream getStream( short id )
+ throws IOException {
+ if ( id == 0 ) {
+ throw new IOException( "Stream id 0 is reserved for internal use." );
+ }
+
+ InputStream s;
+ synchronized ( openStreams ) {
+ s = ( InputStream )openStreams.get( new Short( id ) );
+ ;
+
+ if ( s != null ) {
+ return s;
+ }
+
+ s = new DemuxInputStream( this, id );
+ openStreams.put( new Short( id ), s );
+ }
+ return s;
+ }
+
+ public int available( DemuxInputStream s )
+ throws IOException {
+ return objectIn.available();
+ }
+
+ /**
+ * Pumps data to all input streams until data for the dest Stream arrives.
+ * Only on thread is allowed to pump data at a time and this method returns
+ * true if it pumped data into its input buffer. It returns false if another
+ * thread is allready pumping data.
+ *
+ * @param dest Description of Parameter
+ * @return Description of the Returned Value
+ * @exception IOException Description of Exception
+ */
+ public boolean pumpData( DemuxInputStream dest )
+ throws IOException {
+
+ synchronized ( this ) {
+ if ( pumpingData ) {
+ return false;
+ } else {
+ pumpingData = true;
+ }
+ }
+
+ // Start pumping the data
+ short nextFrameSize = frameSize;
+ while ( true ) {
+ short streamId = objectIn.readShort();
+ // Was it a command on the admin stream?
+ if ( streamId == 0 ) {
+ // Next byte is the command.
+ switch ( objectIn.readByte() ) {
+ case StreamMux.OPEN_STREAM_COMMAND:
+ getStream( objectIn.readShort() );
+ break;
+ case StreamMux.CLOSE_STREAM_COMMAND:
+ DemuxInputStream s;
+ synchronized ( openStreams ) {
+ s = ( DemuxInputStream )openStreams.get( new Short(
objectIn.readShort() ) );
+ }
+ if ( s != null ) {
+ closeStream( s.streamId );
+ s.atEOF = true;
+ if ( s == dest ) {
+ break;
+ } else {
+ // Wake up the thread that was waiting for input (it got a
EOF)
+ synchronized ( s.bufferMutex ) {
+ s.bufferMutex.notify();
+ }
+ }
+ }
+ break;
+ case StreamMux.NEXT_FRAME_SHORT_COMMAND:
+ nextFrameSize = objectIn.readShort();
+ break;
+ }
+
+ } else {
+ objectIn.readFully( inputBuffer, 0, nextFrameSize );
+ DemuxInputStream s;
+ synchronized ( openStreams ) {
+ s = ( DemuxInputStream )openStreams.get( new Short( streamId ) );
+ }
+ if ( s == null ) {
+ continue;
+ }
+
+ s.loadBuffer( inputBuffer, nextFrameSize );
+ if ( s == dest ) {
+ break;
+ }
+
+ nextFrameSize = frameSize;
+ }
+ }
+
+ synchronized ( this ) {
+ pumpingData = false;
+ }
+
+ // we are done pumping but another thread may be
+ // intrested in pumping.
+ synchronized ( openStreams ) {
+ Iterator iter = openStreams.values().iterator();
+ while ( iter.hasNext() && pumpingData == false ) {
+ DemuxInputStream s = ( DemuxInputStream )iter.next();
+ synchronized ( s.bufferMutex ) {
+ s.bufferMutex.notify();
+ }
+ }
+ }
+ return true;
+ }
+
+ void closeStream( short id )
+ throws IOException {
+ if ( id == 0 ) {
+ throw new IOException( "Stream id 0 is reserved for internal use." );
+ }
+
+ synchronized ( openStreams ) {
+ openStreams.remove( new Short( id ) );
+ }
+
+ }
+}
1.2 +138 -119 jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/StreamMux.java
Index: StreamMux.java
===================================================================
RCS file:
/cvsroot/jboss/jbossmq/src/main/org/jboss/mq/il/uil/multiplexor/StreamMux.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- StreamMux.java 2001/08/11 20:59:14 1.1
+++ StreamMux.java 2001/08/17 03:04:04 1.2
@@ -10,125 +10,144 @@
import java.util.*;
/**
- * This class is used to multiplex from
- * multiple streams into a single stream.
- *
- * @author Hiram Chirino ([EMAIL PROTECTED])
- *
- * @version $Revision: 1.1 $
+ * This class is used to multiplex from multiple streams into a single stream.
+ *
+ * @author Hiram Chirino ([EMAIL PROTECTED])
+ * @created August 16, 2001
+ * @version $Revision: 1.2 $
*/
public class StreamMux {
- short frameSize = 512;
- HashMap openStreams = new HashMap();
- OutputStream out;
- DataOutputStream objectOut;
-
- // Commands that can be sent over the admin stream.
- static final byte OPEN_STREAM_COMMAND = 0;
- static final byte CLOSE_STREAM_COMMAND = 1;
- static final byte NEXT_FRAME_SHORT_COMMAND = 2;
-
- /**
- * StreamMux constructor comment.
- * @param out java.io.OutputStream
- */
- public StreamMux(OutputStream out) throws IOException {
- this.out = out;
- this.objectOut = new DataOutputStream(out);
- }
-
- void closeStream(short id) throws IOException {
- if (id == 0)
- throw new IOException("Stream id 0 is reserved for internal
use.");
-
- MuxOutputStream s;
-
- synchronized (openStreams) {
- s = (MuxOutputStream) openStreams.remove(new Short(id));
- }
-
- synchronized (objectOut) {
- objectOut.writeShort(0); // admin stream
- objectOut.writeByte(CLOSE_STREAM_COMMAND); // command
- objectOut.writeShort(id); // argument
- }
- }
-
- public void flush() throws IOException {
-
- synchronized (objectOut) {
- objectOut.flush();
- out.flush();
- }
-
- }
-
- /**
- * Insert the method's description here.
- * Creation date: (11/15/00 5:30:55 PM)
- * @return short
- */
- public short getFrameSize() {
- synchronized (openStreams) {
- return frameSize;
- }
- }
-
- public OutputStream getStream(short id) throws IOException {
- if (id == 0)
- throw new IOException("Stream id 0 is reserved for internal
use.");
-
- OutputStream s;
- synchronized (openStreams) {
- s = (OutputStream) openStreams.get(new Short(id));
- if (s != null) {
- return s;
- }
-
- s = new MuxOutputStream(this, id);
- openStreams.put(new Short(id), s);
- }
-
- synchronized (objectOut) {
- objectOut.writeShort(0); // admin stream
- objectOut.writeByte(OPEN_STREAM_COMMAND); // command
- objectOut.writeShort(id); // argument
- }
- return s;
- }
-
- /**
- * Insert the method's description here.
- * Creation date: (11/15/00 5:30:55 PM)
- * @param newFrameSize short
- */
- public void setFrameSize(short newFrameSize) throws IOException {
- synchronized (openStreams) {
- if (openStreams.size() > 0)
- throw new IOException("Cannot change the frame size
while there are open streams.");
- frameSize = newFrameSize;
- }
- }
-
- void write(MuxOutputStream s, byte b[], int len) throws IOException {
-
- if (b == null) {
- throw new NullPointerException();
- } else if ((len < 0) || (len > b.length) || (len > frameSize)) {
- throw new IndexOutOfBoundsException();
- } else if (len == 0) {
- return;
- }
-
- synchronized (objectOut) {
- if (len < frameSize) {
- objectOut.writeShort(0);
- objectOut.writeByte(NEXT_FRAME_SHORT_COMMAND);
- objectOut.writeShort(len);
- }
- objectOut.writeShort(s.streamId);
- objectOut.write(b, 0, len);
- }
- }
-}
\ No newline at end of file
+ short frameSize = 512;
+ HashMap openStreams = new HashMap();
+ OutputStream out;
+ DataOutputStream objectOut;
+
+ // Commands that can be sent over the admin stream.
+ final static byte OPEN_STREAM_COMMAND = 0;
+ final static byte CLOSE_STREAM_COMMAND = 1;
+ final static byte NEXT_FRAME_SHORT_COMMAND = 2;
+
+ /**
+ * StreamMux constructor comment.
+ *
+ * @param out java.io.OutputStream
+ * @exception IOException Description of Exception
+ */
+ public StreamMux( OutputStream out )
+ throws IOException {
+ this.out = out;
+ this.objectOut = new DataOutputStream( out );
+ }
+
+ /**
+ * Insert the method's description here. Creation date: (11/15/00 5:30:55
+ * PM)
+ *
+ * @param newFrameSize short
+ * @exception IOException Description of Exception
+ */
+ public void setFrameSize( short newFrameSize )
+ throws IOException {
+ synchronized ( openStreams ) {
+ if ( openStreams.size() > 0 ) {
+ throw new IOException( "Cannot change the frame size while there are
open streams." );
+ }
+ frameSize = newFrameSize;
+ }
+ }
+
+ /**
+ * Insert the method's description here. Creation date: (11/15/00 5:30:55
+ * PM)
+ *
+ * @return short
+ */
+ public short getFrameSize() {
+ synchronized ( openStreams ) {
+ return frameSize;
+ }
+ }
+
+ public OutputStream getStream( short id )
+ throws IOException {
+ if ( id == 0 ) {
+ throw new IOException( "Stream id 0 is reserved for internal use." );
+ }
+
+ OutputStream s;
+ synchronized ( openStreams ) {
+ s = ( OutputStream )openStreams.get( new Short( id ) );
+ if ( s != null ) {
+ return s;
+ }
+
+ s = new MuxOutputStream( this, id );
+ openStreams.put( new Short( id ), s );
+ }
+
+ synchronized ( objectOut ) {
+ objectOut.writeShort( 0 );
+ // admin stream
+ objectOut.writeByte( OPEN_STREAM_COMMAND );
+ // command
+ objectOut.writeShort( id );
+ // argument
+ }
+ return s;
+ }
+
+ public void flush()
+ throws IOException {
+
+ synchronized ( objectOut ) {
+ objectOut.flush();
+ out.flush();
+ }
+
+ }
+
+ void closeStream( short id )
+ throws IOException {
+ if ( id == 0 ) {
+ throw new IOException( "Stream id 0 is reserved for internal use." );
+ }
+
+ MuxOutputStream s;
+
+ synchronized ( openStreams ) {
+ s = ( MuxOutputStream )openStreams.remove( new Short( id ) );
+ }
+
+ synchronized ( objectOut ) {
+ objectOut.writeShort( 0 );
+ // admin stream
+ objectOut.writeByte( CLOSE_STREAM_COMMAND );
+ // command
+ objectOut.writeShort( id );
+ // argument
+ }
+ }
+
+ void write( MuxOutputStream s, byte b[], int len )
+ throws IOException {
+
+ if ( b == null ) {
+ throw new NullPointerException();
+ } else if ( ( len < 0 ) || ( len > b.length ) || ( len > frameSize ) ) {
+ throw new IndexOutOfBoundsException();
+ } else if ( len == 0 ) {
+ return;
+ }
+
+ synchronized ( objectOut ) {
+ if ( len < frameSize ) {
+ objectOut.writeShort( 0 );
+ objectOut.writeByte( NEXT_FRAME_SHORT_COMMAND );
+ objectOut.writeShort( len );
+ }
+ objectOut.writeShort( s.streamId );
+ objectOut.write( b, 0, len );
+ }
+ }
+}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development