diff --git a/src/org/ice4j/socket/MultiplexedSocket.java b/src/org/ice4j/socket/MultiplexedSocket.java index 560bd90..684f9f7 100644 --- a/src/org/ice4j/socket/MultiplexedSocket.java +++ b/src/org/ice4j/socket/MultiplexedSocket.java @@ -46,6 +46,11 @@ public class MultiplexedSocket */ final List received = new LinkedList(); + /** + * The custom InputStream for this MultiplexedSocket. + */ + private final InputStreamImpl inputStream = new InputStreamImpl(); + /** * Initializes a new MultiplexedSocket which is unbound and * filters DatagramPackets away from a specific @@ -70,7 +75,7 @@ public class MultiplexedSocket * Even if MultiplexingSocket allows MultiplexedSocket * to perform bind, binding in the super will not execute correctly this * early in the construction because the multiplexing field is not set - * yet. That is why MultiplexeSocket does not currently support + * yet. That is why MultiplexedSocket does not currently support * bind at construction time. */ super(multiplexing); @@ -97,6 +102,15 @@ public class MultiplexedSocket multiplexing.close(this); } + /** + * {@inheritDoc} + */ + @Override + public InputStream getInputStream() + { + return inputStream; + } + /** * Gets the DatagramPacketFilter which determines which * DatagramPackets read from the network are to be received through @@ -134,4 +148,121 @@ public class MultiplexedSocket { multiplexing.receive(this, p); } + + /** + * Implements an InputStream for this MultiplexedSocket, + * reading data using {@link #receive(java.net.DatagramPacket)}. + */ + private class InputStreamImpl + extends InputStream + { + /** + * A buffer to receive data into. + */ + private final byte[] buf = new byte[1500]; + + /** + * A DatagramPacket instance to receive data into. + */ + private final DatagramPacket packet = new DatagramPacket(buf, 1500); + + /** + * Synchronization object for read operation. + */ + private final Object readSyncRoot = new Object(); + + /** + * Initializes a new TCPInputStream. + */ + public InputStreamImpl() + { + } + + /** + * {@inheritDoc} + */ + @Override + public int available() + { + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean markSupported() + { + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public int read(byte[] b) + throws IOException + { + return read(b, 0, b.length); + } + + /** + * {@inheritDoc} + */ + @Override + public int read(byte[] b, int off, int len) + throws IOException + { + synchronized(readSyncRoot) + { + receive(packet); + + byte[] packetData = packet.getData(); + int packetOff = packet.getOffset(); + int packetLen = packet.getLength(); + + int lengthRead = Math.min(len, packetLen); + System.arraycopy( + packetData, packetOff, + b, off, + lengthRead); + + return lengthRead; + } + } + + /** + * {@inheritDoc} + */ + @Override + public void reset() + throws IOException + { + if(!markSupported()) + { + throw new IOException("InputStreamImpl does not support reset()"); + } + } + + /** + * {@inheritDoc} + */ + @Override + public long skip(long n) + throws IOException + { + throw new IOException("InputStreamImpl does not support skip."); + } + + /** + * {@inheritDoc} + */ + @Override + public int read() + throws IOException + { + //We don't support reading a single byte + return 0; + } + } }