1 52 53 package com.go.trove.net; 54 55 import java.net.*; 56 import java.io.*; 57 58 67 public class FilteredSocket extends Socket { 68 protected final Socket mSocket; 69 70 public FilteredSocket(Socket socket) throws SocketException { 71 super(new Impl(socket)); 72 mSocket = socket; 73 } 74 75 79 private static class Impl extends SocketImpl { 80 private Socket mSocket; 81 82 public Impl(Socket socket) { 83 if (socket == null) { 84 throw new IllegalArgumentException ("Socket is null"); 85 } 86 mSocket = socket; 87 } 88 89 public void setOption(int optId, Object value) throws SocketException { 90 switch (optId) { 91 case TCP_NODELAY: 92 mSocket.setTcpNoDelay(((Boolean )value).booleanValue()); 93 return; 94 case SO_LINGER: 95 if (value instanceof Boolean ) { 96 mSocket.setSoLinger(((Boolean )value).booleanValue(), 0); 97 } 98 else { 99 mSocket.setSoLinger(true, ((Integer )value).intValue()); 100 } 101 return; 102 case SO_TIMEOUT: 103 mSocket.setSoTimeout(((Integer )value).intValue()); 104 return; 105 case SO_SNDBUF: 106 mSocket.setSendBufferSize(((Integer )value).intValue()); 107 return; 108 case SO_RCVBUF: 109 mSocket.setReceiveBufferSize(((Integer )value).intValue()); 110 return; 111 case SO_BINDADDR: 112 case SO_REUSEADDR: 113 case IP_MULTICAST_IF: 114 default: 115 throw new SocketException("Invalid option: " + optId); 116 } 117 } 118 119 public Object getOption(int optId) throws SocketException { 120 switch (optId) { 121 case TCP_NODELAY: 122 return mSocket.getTcpNoDelay() ? Boolean.TRUE : Boolean.FALSE; 123 case SO_BINDADDR: 124 return mSocket.getLocalAddress(); 125 case SO_LINGER: 126 return new Integer (mSocket.getSoLinger()); 127 case SO_TIMEOUT: 128 return new Integer (mSocket.getSoTimeout()); 129 case SO_SNDBUF: 130 return new Integer (mSocket.getSendBufferSize()); 131 case SO_RCVBUF: 132 return new Integer (mSocket.getReceiveBufferSize()); 133 case SO_REUSEADDR: 134 case IP_MULTICAST_IF: 135 default: 136 throw new SocketException("Invalid option: " + optId); 137 } 138 } 139 140 protected void create(boolean stream) throws IOException { 141 error(); 142 } 143 144 protected void connect(String host, int port) throws IOException { 145 error(); 146 } 147 148 protected void connect(InetAddress host, int port) throws IOException { 149 error(); 150 } 151 152 protected void bind(InetAddress host, int port) throws IOException { 153 error(); 154 } 155 156 protected void listen(int backlog) throws IOException { 157 error(); 158 } 159 160 protected void accept(SocketImpl s) throws IOException { 161 error(); 162 } 163 164 protected InputStream getInputStream() throws IOException { 165 return mSocket.getInputStream(); 166 } 167 168 protected OutputStream getOutputStream() throws IOException { 169 return mSocket.getOutputStream(); 170 } 171 172 protected int available() throws IOException { 173 return getInputStream().available(); 174 } 175 176 protected void close() throws IOException { 177 mSocket.close(); 178 } 179 180 protected InetAddress getInetAddress() { 181 return mSocket.getInetAddress(); 182 } 183 184 protected int getPort() { 185 return mSocket.getPort(); 186 } 187 188 protected int getLocalPort() { 189 return mSocket.getLocalPort(); 190 } 191 192 private void error() throws IOException { 193 throw new IOException("Socket already connected"); 194 } 195 } 196 } 197 | Popular Tags |