Source for gnu.java.net.local.LocalSocket

   1: /* LocalSocket.java -- a unix domain client socket.
   2:    Copyright (C) 2006  Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.java.net.local;
  40: 
  41: import java.io.IOException;
  42: import java.io.InputStream;
  43: import java.io.OutputStream;
  44: 
  45: import java.net.InetAddress;
  46: import java.net.Socket;
  47: import java.net.SocketAddress;
  48: import java.net.SocketException;
  49: 
  50: import java.nio.channels.IllegalBlockingModeException;
  51: import java.nio.channels.SocketChannel;
  52: 
  53: /**
  54:  * A local, or unix-domain socket. Unix domain sockets are connected on the
  55:  * local filesystem itself, rather than a remote address.
  56:  */
  57: public final class LocalSocket extends Socket
  58: {
  59: 
  60:   // Fields.
  61:   // -------------------------------------------------------------------------
  62: 
  63:   private final LocalSocketImpl localimpl;
  64:   boolean localClosed;
  65:   boolean localConnected;
  66: 
  67:   // Constructors.
  68:   // -------------------------------------------------------------------------
  69: 
  70:   public LocalSocket () throws SocketException
  71:   {
  72:     super ();
  73:     localimpl = new LocalSocketImpl ();
  74:   }
  75: 
  76:   public LocalSocket (LocalSocketAddress addr) throws SocketException
  77:   {
  78:     this ();
  79:     try
  80:       {
  81:         connect (addr);
  82:       }
  83:     catch (IOException ioe)
  84:       {
  85:         SocketException se = new SocketException ();
  86:         se.initCause (ioe);
  87:         throw se;
  88:       }
  89:   }
  90: 
  91:   LocalSocket (boolean nocreate) throws IOException
  92:   {
  93:     super ();
  94:     localimpl = new LocalSocketImpl (nocreate);
  95:   }
  96: 
  97:   // Instance methods.
  98:   // -------------------------------------------------------------------------
  99: 
 100:   public void bind (SocketAddress bindpoint) throws IOException
 101:   {
 102:     throw new SocketException ("binding local client sockets is nonsensical");
 103:   }
 104: 
 105:   public void connect (SocketAddress endpoint, int timeout) throws IOException
 106:   {
 107:     if (isClosed ())
 108:       {
 109:         throw new SocketException ("socket is closed");
 110:       }
 111:     if (! (endpoint instanceof LocalSocketAddress))
 112:       {
 113:         throw new IllegalArgumentException ("socket address is not a local address");
 114:       }
 115:     if (getChannel() != null && !getChannel().isBlocking())
 116:       {
 117:         throw new IllegalBlockingModeException ();
 118:       }
 119: 
 120:     try
 121:       {
 122:         localimpl.doCreate ();
 123:         localimpl.localConnect ((LocalSocketAddress) endpoint);
 124:       }
 125:     catch (IOException ioe)
 126:       {
 127:         close ();
 128:         throw ioe;
 129:       }
 130:     localConnected = true;
 131:   }
 132: 
 133:   public InetAddress getInetAddress ()
 134:   {
 135:     return null;
 136:   }
 137: 
 138:   public InetAddress getLocalAddress ()
 139:   {
 140:     return null;
 141:   }
 142: 
 143:   public int getPort ()
 144:   {
 145:     return -1;
 146:   }
 147: 
 148:   public int getLocalPort ()
 149:   {
 150:     return -1;
 151:   }
 152: 
 153:   public SocketChannel getChannel ()
 154:   {
 155:     return null;
 156:   }
 157: 
 158:   public SocketAddress getLocalSocketAddress ()
 159:   {
 160:     return localimpl.getLocalAddress ();
 161:   }
 162: 
 163:   public SocketAddress getRemoteSocketAddress ()
 164:   {
 165:     return localimpl.getRemoteAddress ();
 166:   }
 167: 
 168:   public InputStream getInputStream () throws IOException
 169:   {
 170:     return localimpl.getInputStream ();
 171:   }
 172: 
 173:   public OutputStream getOutputStream () throws IOException
 174:   {
 175:     return localimpl.getOutputStream ();
 176:   }
 177: 
 178:   public void sendUrgentData (int b) throws IOException
 179:   {
 180:     localimpl.sendUrgentData (b);
 181:   }
 182: 
 183:   public synchronized void close () throws IOException
 184:   {
 185:     localimpl.close ();
 186:     localClosed = true;
 187:   }
 188: 
 189:   public void shutdownInput () throws IOException
 190:   {
 191:     localimpl.shutdownInput ();
 192:   }
 193: 
 194:   public void shutdownOutput () throws IOException
 195:   {
 196:     localimpl.shutdownOutput ();
 197:   }
 198: 
 199:   public boolean isClosed ()
 200:   {
 201:     return localClosed;
 202:   }
 203: 
 204:   public boolean isBound ()
 205:   {
 206:     return false;
 207:   }
 208: 
 209:   public boolean isConnected ()
 210:   {
 211:     return localConnected;
 212:   }
 213: 
 214:   // Unsupported methods.
 215:   // -------------------------------------------------------------------------
 216: 
 217:   public void setTcpNoDelay (boolean b) throws SocketException
 218:   {
 219:     throw new SocketException ("local sockets do not support this option");
 220:   }
 221: 
 222:   public boolean getTcpNoDelay() throws SocketException
 223:   {
 224:     throw new SocketException ("local sockets do not support this option");
 225:   }
 226: 
 227:   public void setSoLinger (boolean b, int i) throws SocketException
 228:   {
 229:     throw new SocketException ("local sockets do not support this option");
 230:   }
 231: 
 232:   public int getSoLinger () throws SocketException
 233:   {
 234:     throw new SocketException ("local sockets do not support this option");
 235:   }
 236: 
 237:   public void setOOBInline (boolean b) throws SocketException
 238:   {
 239:     throw new SocketException ("local sockets do not support this option");
 240:   }
 241: 
 242:   public boolean getOOBInline () throws SocketException
 243:   {
 244:     throw new SocketException ("local sockets do not support this option");
 245:   }
 246: 
 247:   public void setSoTimeout (int i) throws SocketException
 248:   {
 249:     // Ignore.
 250:   }
 251: 
 252:   public int getSoTimeout () throws SocketException
 253:   {
 254:     // We don't support timeout, so we return 0.
 255:     return 0;
 256:   }
 257: 
 258:   public void setSendBufferSize (int i) throws SocketException
 259:   {
 260:     throw new SocketException ("local sockets do not support this option");
 261:   }
 262: 
 263:   public int getSendBufferSize() throws SocketException
 264:   {
 265:     throw new SocketException ("local sockets do not support this option");
 266:   }
 267: 
 268:   public void setReceiveBufferSize (int i) throws SocketException
 269:   {
 270:     throw new SocketException ("local sockets do not support this option");
 271:   }
 272: 
 273:   public int getReceiveBufferSize () throws SocketException
 274:   {
 275:     throw new SocketException ("local sockets do not support this option");
 276:   }
 277: 
 278:   public void setKeepAlive (boolean b) throws SocketException
 279:   {
 280:     throw new SocketException ("local sockets do not support this option");
 281:   }
 282: 
 283:   public boolean getKeepAlive () throws SocketException
 284:   {
 285:     throw new SocketException ("local sockets do not support this option");
 286:   }
 287: 
 288:   public void setTrafficClass (int i) throws SocketException
 289:   {
 290:     throw new SocketException ("local sockets do not support this option");
 291:   }
 292: 
 293:   public int getTrafficClass () throws SocketException
 294:   {
 295:     throw new SocketException ("local sockets do not support this option");
 296:   }
 297: 
 298:   public void setReuseAddress (boolean b) throws SocketException
 299:   {
 300:     throw new SocketException ("local sockets do not support this option");
 301:   }
 302: 
 303:   public boolean getReuseAddress () throws SocketException
 304:   {
 305:     throw new SocketException ("local sockets do not support this option");
 306:   }
 307: 
 308:   LocalSocketImpl getLocalImpl ()
 309:   {
 310:     return localimpl;
 311:   }
 312: }