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: import java.net.SocketImpl;
  50: 
  51: import java.nio.channels.IllegalBlockingModeException;
  52: import java.nio.channels.SocketChannel;
  53: 
  54: /**
  55:  * A local, or unix-domain socket. Unix domain sockets are connected on the
  56:  * local filesystem itself, rather than a remote address.
  57:  */
  58: public final class LocalSocket extends Socket
  59: {
  60: 
  61:   // Fields.
  62:   // -------------------------------------------------------------------------
  63: 
  64:   private final LocalSocketImpl localimpl;
  65:   boolean localClosed;
  66:   boolean localConnected;
  67: 
  68:   // Constructors.
  69:   // -------------------------------------------------------------------------
  70: 
  71:   public LocalSocket () throws SocketException
  72:   {
  73:     super ();
  74:     localimpl = new LocalSocketImpl ();
  75:   }
  76: 
  77:   public LocalSocket (LocalSocketAddress addr) throws SocketException
  78:   {
  79:     this ();
  80:     try
  81:       {
  82:         connect (addr);
  83:       }
  84:     catch (IOException ioe)
  85:       {
  86:         SocketException se = new SocketException ();
  87:         se.initCause (ioe);
  88:         throw se;
  89:       }
  90:   }
  91: 
  92:   LocalSocket (boolean nocreate) throws IOException
  93:   {
  94:     super ();
  95:     localimpl = new LocalSocketImpl (nocreate);
  96:   }
  97: 
  98:   // Instance methods.
  99:   // -------------------------------------------------------------------------
 100: 
 101:   public void bind (SocketAddress bindpoint) throws IOException
 102:   {
 103:     throw new SocketException ("binding local client sockets is nonsensical");
 104:   }
 105: 
 106:   public void connect (SocketAddress endpoint, int timeout) throws IOException
 107:   {
 108:     if (isClosed ())
 109:       {
 110:         throw new SocketException ("socket is closed");
 111:       }
 112:     if (! (endpoint instanceof LocalSocketAddress))
 113:       {
 114:         throw new IllegalArgumentException ("socket address is not a local address");
 115:       }
 116:     if (getChannel() != null && !getChannel().isBlocking())
 117:       {
 118:         throw new IllegalBlockingModeException ();
 119:       }
 120: 
 121:     try
 122:       {
 123:         localimpl.doCreate ();
 124:         localimpl.localConnect ((LocalSocketAddress) endpoint);
 125:       }
 126:     catch (IOException ioe)
 127:       {
 128:         close ();
 129:         throw ioe;
 130:       }
 131:     localConnected = true;
 132:   }
 133: 
 134:   public InetAddress getInetAddress ()
 135:   {
 136:     return null;
 137:   }
 138: 
 139:   public InetAddress getLocalAddress ()
 140:   {
 141:     return null;
 142:   }
 143: 
 144:   public int getPort ()
 145:   {
 146:     return -1;
 147:   }
 148: 
 149:   public int getLocalPort ()
 150:   {
 151:     return -1;
 152:   }
 153: 
 154:   public SocketChannel getChannel ()
 155:   {
 156:     return null;
 157:   }
 158: 
 159:   public SocketAddress getLocalSocketAddress ()
 160:   {
 161:     return localimpl.getLocalAddress ();
 162:   }
 163: 
 164:   public SocketAddress getRemoteSocketAddress ()
 165:   {
 166:     return localimpl.getRemoteAddress ();
 167:   }
 168: 
 169:   public InputStream getInputStream () throws IOException
 170:   {
 171:     return localimpl.getInputStream ();
 172:   }
 173: 
 174:   public OutputStream getOutputStream () throws IOException
 175:   {
 176:     return localimpl.getOutputStream ();
 177:   }
 178: 
 179:   public void sendUrgentData (int b) throws IOException
 180:   {
 181:     localimpl.sendUrgentData (b);
 182:   }
 183: 
 184:   public synchronized void close () throws IOException
 185:   {
 186:     localimpl.close ();
 187:     localClosed = true;
 188:   }
 189: 
 190:   public void shutdownInput () throws IOException
 191:   {
 192:     localimpl.shutdownInput ();
 193:   }
 194: 
 195:   public void shutdownOutput () throws IOException
 196:   {
 197:     localimpl.shutdownOutput ();
 198:   }
 199: 
 200:   public boolean isClosed ()
 201:   {
 202:     return localClosed;
 203:   }
 204: 
 205:   public boolean isBound ()
 206:   {
 207:     return false;
 208:   }
 209: 
 210:   public boolean isConnected ()
 211:   {
 212:     return localConnected;
 213:   }
 214: 
 215:   // Unsupported methods.
 216:   // -------------------------------------------------------------------------
 217: 
 218:   public void setTcpNoDelay (boolean b) throws SocketException
 219:   {
 220:     throw new SocketException ("local sockets do not support this option");
 221:   }
 222: 
 223:   public boolean getTcpNoDelay() throws SocketException
 224:   {
 225:     throw new SocketException ("local sockets do not support this option");
 226:   }
 227: 
 228:   public void setSoLinger (boolean b, int i) throws SocketException
 229:   {
 230:     throw new SocketException ("local sockets do not support this option");
 231:   }
 232: 
 233:   public int getSoLinger () throws SocketException
 234:   {
 235:     throw new SocketException ("local sockets do not support this option");
 236:   }
 237: 
 238:   public void setOOBInline (boolean b) throws SocketException
 239:   {
 240:     throw new SocketException ("local sockets do not support this option");
 241:   }
 242: 
 243:   public boolean getOOBInline () throws SocketException
 244:   {
 245:     throw new SocketException ("local sockets do not support this option");
 246:   }
 247: 
 248:   public void setSoTimeout (int i) throws SocketException
 249:   {
 250:     // Ignore.
 251:   }
 252: 
 253:   public int getSoTimeout () throws SocketException
 254:   {
 255:     // We don't support timeout, so we return 0.
 256:     return 0;
 257:   }
 258: 
 259:   public void setSendBufferSize (int i) throws SocketException
 260:   {
 261:     throw new SocketException ("local sockets do not support this option");
 262:   }
 263: 
 264:   public int getSendBufferSize() throws SocketException
 265:   {
 266:     throw new SocketException ("local sockets do not support this option");
 267:   }
 268: 
 269:   public void setReceiveBufferSize (int i) throws SocketException
 270:   {
 271:     throw new SocketException ("local sockets do not support this option");
 272:   }
 273: 
 274:   public int getReceiveBufferSize () throws SocketException
 275:   {
 276:     throw new SocketException ("local sockets do not support this option");
 277:   }
 278: 
 279:   public void setKeepAlive (boolean b) throws SocketException
 280:   {
 281:     throw new SocketException ("local sockets do not support this option");
 282:   }
 283: 
 284:   public boolean getKeepAlive () throws SocketException
 285:   {
 286:     throw new SocketException ("local sockets do not support this option");
 287:   }
 288: 
 289:   public void setTrafficClass (int i) throws SocketException
 290:   {
 291:     throw new SocketException ("local sockets do not support this option");
 292:   }
 293: 
 294:   public int getTrafficClass () throws SocketException
 295:   {
 296:     throw new SocketException ("local sockets do not support this option");
 297:   }
 298: 
 299:   public void setReuseAddress (boolean b) throws SocketException
 300:   {
 301:     throw new SocketException ("local sockets do not support this option");
 302:   }
 303: 
 304:   public boolean getReuseAddress () throws SocketException
 305:   {
 306:     throw new SocketException ("local sockets do not support this option");
 307:   }
 308: 
 309:   LocalSocketImpl getLocalImpl ()
 310:   {
 311:     return localimpl;
 312:   }
 313: }