1 /* LocalSocketImpl.java -- a unix domain client socket implementation. 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.FileDescriptor; 42 import java.io.InputStream; 43 import java.io.IOException; 44 import java.io.OutputStream; 45 46 import java.net.InetAddress; 47 import java.net.SocketAddress; 48 import java.net.SocketException; 49 import java.net.SocketImpl; 50 51 final class LocalSocketImpl extends SocketImpl 52 { 53 54 // Fields. 55 // ------------------------------------------------------------------------- 56 57 private boolean created; 58 private InputStream in; 59 private OutputStream out; 60 private int socket_fd; 61 private LocalSocketAddress local; 62 private LocalSocketAddress remote; 63 64 // Constructor. 65 // ------------------------------------------------------------------------- 66 LocalSocketImpl()67 LocalSocketImpl () 68 { 69 this (false); 70 } 71 LocalSocketImpl(boolean nocreate)72 LocalSocketImpl (boolean nocreate) 73 { 74 created = nocreate; 75 socket_fd = -1; 76 fd = new FileDescriptor (); 77 } 78 79 // Instance methods. 80 // ------------------------------------------------------------------------- 81 setOption(int opt, Object value)82 public void setOption (int opt, Object value) throws SocketException 83 { 84 throw new SocketException ("local sockets do not support options"); 85 } 86 getOption(int opt)87 public Object getOption (int opt) throws SocketException 88 { 89 throw new SocketException ("local sockets do not support options"); 90 } 91 create(boolean stream)92 protected void create (boolean stream) throws IOException { } listen(int timeout)93 protected void listen (int timeout) throws IOException { } accept(LocalSocketImpl socket)94 protected void accept (LocalSocketImpl socket) throws IOException { } available()95 protected int available () throws IOException { return -1; } close()96 protected void close () throws IOException { } sendUrgentData(int data)97 protected void sendUrgentData (int data) throws IOException { } shutdownInput()98 protected void shutdownInput () throws IOException { } shutdownOutput()99 protected void shutdownOutput () throws IOException { } 100 unlink()101 void unlink () throws IOException { } localBind(LocalSocketAddress addr)102 void localBind (LocalSocketAddress addr) throws IOException { } localConnect(LocalSocketAddress addr)103 void localConnect (LocalSocketAddress addr) throws IOException { } read(byte[] buf, int off, int len)104 int read (byte[] buf, int off, int len) throws IOException { return -1; } write(byte[] buf, int off, int len)105 void write (byte[] buf, int off, int len) throws IOException { } 106 doCreate()107 void doCreate () throws IOException 108 { 109 if (!created) 110 { 111 create (true); 112 } 113 } 114 getLocalAddress()115 LocalSocketAddress getLocalAddress () 116 { 117 return local; 118 } 119 getRemoteAddress()120 LocalSocketAddress getRemoteAddress () 121 { 122 return remote; 123 } 124 getInputStream()125 protected InputStream getInputStream() 126 { 127 if (in == null) 128 { 129 in = new LocalInputStream (this); 130 } 131 132 return in; 133 } 134 getOutputStream()135 protected OutputStream getOutputStream() 136 { 137 if (out == null) 138 { 139 out = new LocalOutputStream (this); 140 } 141 142 return out; 143 } 144 accept(SocketImpl impl)145 protected void accept (SocketImpl impl) throws IOException 146 { 147 if (! (impl instanceof LocalSocketImpl)) 148 { 149 throw new IllegalArgumentException ("not a local socket"); 150 } 151 accept ((LocalSocketImpl) impl); 152 } 153 connect(String host, int port)154 protected void connect (String host, int port) throws IOException 155 { 156 throw new SocketException ("this is a local socket"); 157 } 158 connect(InetAddress addr, int port)159 protected void connect (InetAddress addr, int port) throws IOException 160 { 161 throw new SocketException ("this is a local socket"); 162 } 163 connect(SocketAddress addr, int timeout)164 protected void connect(SocketAddress addr, int timeout) throws IOException 165 { 166 if (! (addr instanceof LocalSocketAddress)) 167 { 168 throw new SocketException ("address is not local"); 169 } 170 localConnect ((LocalSocketAddress) addr); 171 } 172 bind(InetAddress addr, int port)173 protected void bind (InetAddress addr, int port) throws IOException 174 { 175 throw new SocketException ("this is a local socket"); 176 } 177 bind(SocketAddress addr)178 protected void bind (SocketAddress addr) throws IOException 179 { 180 if (! (addr instanceof LocalSocketAddress)) 181 { 182 throw new SocketException ("address is not local"); 183 } 184 localBind ((LocalSocketAddress) addr); 185 } 186 187 // Inner classes. 188 // ------------------------------------------------------------------------- 189 190 class LocalInputStream extends InputStream 191 { 192 193 // Field. 194 // ----------------------------------------------------------------------- 195 196 private final LocalSocketImpl impl; 197 198 // Constructor. 199 // ----------------------------------------------------------------------- 200 LocalInputStream(LocalSocketImpl impl)201 LocalInputStream (LocalSocketImpl impl) 202 { 203 this.impl = impl; 204 } 205 206 // Instance methods. 207 // ----------------------------------------------------------------------- 208 available()209 public int available () throws IOException 210 { 211 return impl.available(); 212 } 213 markSupported()214 public boolean markSupported () 215 { 216 return false; 217 } 218 mark(int readLimit)219 public void mark (int readLimit) 220 { 221 } 222 reset()223 public void reset () throws IOException 224 { 225 throw new IOException ("mark/reset not supported"); 226 } 227 close()228 public void close () throws IOException 229 { 230 impl.close(); 231 } 232 read()233 public int read () throws IOException 234 { 235 byte[] buf = new byte[1]; 236 int ret = read (buf); 237 if (ret != -1) 238 { 239 return buf[0] & 0xFF; 240 } 241 else 242 { 243 return -1; 244 } 245 } 246 read(byte[] buf)247 public int read (byte[] buf) throws IOException 248 { 249 return read (buf, 0, buf.length); 250 } 251 read(byte[] buf, int off, int len)252 public int read (byte[] buf, int off, int len) throws IOException 253 { 254 int ret = impl.read (buf, off, len); 255 256 if (ret == 0) 257 { 258 return -1; 259 } 260 261 return ret; 262 } 263 } 264 265 class LocalOutputStream extends OutputStream 266 { 267 268 // Field. 269 // ----------------------------------------------------------------------- 270 271 private final LocalSocketImpl impl; 272 273 // Constructor. 274 // ----------------------------------------------------------------------- 275 LocalOutputStream(LocalSocketImpl impl)276 LocalOutputStream (LocalSocketImpl impl) 277 { 278 this.impl = impl; 279 } 280 281 // Instance methods. 282 // ----------------------------------------------------------------------- 283 close()284 public void close () throws IOException 285 { 286 impl.close (); 287 } 288 flush()289 public void flush () throws IOException 290 { 291 } 292 write(int b)293 public void write (int b) throws IOException 294 { 295 byte[] buf = new byte [1]; 296 buf[0] = (byte) b; 297 write (buf); 298 } 299 write(byte[] buf)300 public void write (byte[] buf) throws IOException 301 { 302 write (buf, 0, buf.length); 303 } 304 write(byte[] buf, int off, int len)305 public void write (byte[] buf, int off, int len) throws IOException 306 { 307 impl.write (buf, off, len); 308 } 309 } 310 } 311