KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > mplex > MultiplexSocket


1 package org.sapia.ubik.net.mplex;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.PushbackInputStream JavaDoc;
7
8 import java.net.Socket JavaDoc;
9 import java.net.SocketException JavaDoc;
10 import java.net.SocketImpl JavaDoc;
11
12
13 /**
14  * The <code>MultiplesSocket</code> is the implementation of client socket that is
15  * used in Ubik's multiplex logic. It extends the traditionnal <code>Socket</code>
16  * of the <code>java.net</code> package to add a read-ahead functionality on the
17  * input stream of the socket.<p>
18  *
19  * This class has the following limitation were a maximum number of bytes that
20  * can be read in advanced and put back in the input stream of the socket has to
21  * be set when creating a new <code>MultiplexSocket</code> instance.
22  *
23  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
24  * <dl>
25  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">
26  * Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
27  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
28  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a>
29  * at the Sapia OSS web site</dd></dt>
30  * </dl>
31  */

32 public class MultiplexSocket extends Socket JavaDoc {
33   /** The input stream to provides the read-ahead logic. */
34   private PushbackInputStream JavaDoc _theInput;
35
36   /** The size of the number of the maximum number of bytes that can be read-ahead. */
37   private int _thePushbackBufferSize;
38
39   /**
40    * Creates a new MultiplexSocket instance. The socket will not be connected.
41    *
42    * @param impl The socket impl to use.
43    * @param bufferSize The maximum read-ahead buffer size.
44    * @throws SocketException If an error occurs with the socket impl.
45    */

46   public MultiplexSocket(SocketImpl JavaDoc impl, int bufferSize)
47     throws SocketException JavaDoc {
48     super(impl);
49     _thePushbackBufferSize = bufferSize;
50   }
51
52   /**
53    * Returns the input stream of this socket.
54    *
55    * @return The input stream of this socket.
56    * @exception IOException If an I/O error occurs when creating the input stream,
57    * the socket is closed, the socket is not connected, or the socket
58    * input has been shutdown using {@link #shutdownInput()}.
59    */

60   public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
61     if (_theInput == null) {
62       _theInput = new PushbackInputStream JavaDoc(new BufferedInputStream JavaDoc(
63             super.getInputStream()), 256);
64     }
65
66     return _theInput;
67   }
68
69   /**
70    * Returns the input stream of this socket as a <code>PushbackInputStream</code>.
71    *
72    * @return The input stream of this socket as a <code>PushbackInputStream</code>.
73    */

74   public PushbackInputStream JavaDoc getPushbackInputStream() throws IOException JavaDoc {
75     return (PushbackInputStream JavaDoc) getInputStream();
76   }
77
78   /**
79    * Returns the implementation address and implementation port of
80    * this socket as a <code>String</code>.
81    *
82    * @return A string representation of this socket.
83    */

84   public String JavaDoc toString() {
85     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
86     aBuffer.append("MultiplexSocket[").append(super.toString()).append("]");
87
88     return aBuffer.toString();
89   }
90 }
91
Popular Tags