@Override protected void accept(SocketImpl socket) throws IOException { final AFUNIXSocketImpl si = (AFUNIXSocketImpl) socket; NativeUnixSocket.accept(socketFile, fd, si.fd); si.socketFile = socketFile; si.connected = true; }
@Override protected int available() throws IOException { return NativeUnixSocket.available(fd); }
protected void bind(int backlog, SocketAddress addr) throws IOException { if (!(addr instanceof AFUNIXSocketAddress)) { throw new SocketException("Cannot bind to this type of address: " + addr.getClass()); } final AFUNIXSocketAddress socketAddress = (AFUNIXSocketAddress) addr; socketFile = socketAddress.getSocketFile(); NativeUnixSocket.bind(socketFile, fd, backlog); bound = true; this.localport = socketAddress.getPort(); }
@Override protected void shutdownOutput() throws IOException { if (!closed && fd.valid()) { NativeUnixSocket.shutdown(fd, SHUT_WR); } }
@Override protected void connect(SocketAddress addr, int timeout) throws IOException { if (!(addr instanceof AFUNIXSocketAddress)) { throw new SocketException("Cannot bind to this type of address: " + addr.getClass()); } final AFUNIXSocketAddress socketAddress = (AFUNIXSocketAddress) addr; socketFile = socketAddress.getSocketFile(); NativeUnixSocket.connect(socketFile, fd); this.address = socketAddress.getAddress(); this.port = socketAddress.getPort(); this.localport = 0; this.connected = true; }
@Override protected void listen(int backlog) throws IOException { NativeUnixSocket.listen(fd, backlog); }
@Override public int read(byte[] buf, int off, int len) throws IOException { if (streamClosed) { throw new IOException("This InputStream has already been closed."); } if (len == 0) { return 0; } if (closed) { return -1; } int maxRead = buf.length - off; if (len > maxRead) { len = maxRead; } try { return NativeUnixSocket.read(fd, buf, off, len); } catch (final IOException e) { throw (IOException) new IOException(e.getMessage() + " at " + AFUNIXSocketImpl.this.toString()).initCause(e); } }
@Override public Object getOption(int optID) throws SocketException { try { switch (optID) { case SocketOptions.SO_KEEPALIVE: case SocketOptions.TCP_NODELAY: return NativeUnixSocket.getSocketOptionInt(fd, optID) != 0 ? true : false; case SocketOptions.SO_LINGER: case SocketOptions.SO_TIMEOUT: case SocketOptions.SO_RCVBUF: case SocketOptions.SO_SNDBUF: return NativeUnixSocket.getSocketOptionInt(fd, optID); default: throw new AFUNIXSocketException("Unsupported option: " + optID); } } catch (final AFUNIXSocketException e) { throw e; } catch (final Exception e) { throw new AFUNIXSocketException("Error while getting option", e); } }
throw new SocketException("Only accepting Boolean.FALSE here"); NativeUnixSocket.setSocketOptionInt(fd, optID, -1); return; NativeUnixSocket.setSocketOptionInt(fd, optID, expectInteger(value)); return; case SocketOptions.SO_RCVBUF: case SocketOptions.SO_SNDBUF: case SocketOptions.SO_TIMEOUT: NativeUnixSocket.setSocketOptionInt(fd, optID, expectInteger(value)); return; case SocketOptions.SO_KEEPALIVE: case SocketOptions.TCP_NODELAY: NativeUnixSocket.setSocketOptionInt(fd, optID, expectBoolean(value)); return; default:
@Override public void close() throws IOException { if (streamClosed) { return; } streamClosed = true; if (fd.valid()) { NativeUnixSocket.shutdown(fd, SHUT_WR); } closedOutputStream = true; checkClose(); } }
@Override protected void connect(SocketAddress addr, int timeout) throws IOException { if (!(addr instanceof AFUNIXSocketAddress)) { throw new SocketException("Cannot bind to this type of address: " + addr.getClass()); } final AFUNIXSocketAddress socketAddress = (AFUNIXSocketAddress) addr; socketFile = socketAddress.getSocketFile(); NativeUnixSocket.connect(socketFile, fd); this.address = socketAddress.getAddress(); this.port = socketAddress.getPort(); this.localport = 0; this.connected = true; }
@Override protected void listen(int backlog) throws IOException { NativeUnixSocket.listen(fd, backlog); }
@Override public int read(byte[] buf, int off, int len) throws IOException { if (streamClosed) { throw new IOException("This InputStream has already been closed."); } FileDescriptor fdesc = validFdOrException(); if (len == 0) { return 0; } else if (off < 0 || len < 0 || (len > buf.length - off)) { throw new IndexOutOfBoundsException(); } try { return NativeUnixSocket.read(fdesc, buf, off, len); } catch (final IOException e) { throw (IOException) new IOException(e.getMessage() + " at " + AFUNIXSocketImpl.this .toString()).initCause(e); } }
@Override public Object getOption(int optID) throws SocketException { try { switch (optID) { case SocketOptions.SO_KEEPALIVE: case SocketOptions.TCP_NODELAY: return NativeUnixSocket.getSocketOptionInt(fd, optID) != 0 ? true : false; case SocketOptions.SO_LINGER: case SocketOptions.SO_TIMEOUT: case SocketOptions.SO_RCVBUF: case SocketOptions.SO_SNDBUF: return NativeUnixSocket.getSocketOptionInt(fd, optID); default: throw new AFUNIXSocketException("Unsupported option: " + optID); } } catch (final AFUNIXSocketException e) { throw e; } catch (final Exception e) { throw new AFUNIXSocketException("Error while getting option", e); } }
throw new SocketException("Only accepting Boolean.FALSE here"); NativeUnixSocket.setSocketOptionInt(fd, optID, -1); return; NativeUnixSocket.setSocketOptionInt(fd, optID, expectInteger(value)); return; case SocketOptions.SO_RCVBUF: case SocketOptions.SO_SNDBUF: case SocketOptions.SO_TIMEOUT: NativeUnixSocket.setSocketOptionInt(fd, optID, expectInteger(value)); return; case SocketOptions.SO_KEEPALIVE: case SocketOptions.TCP_NODELAY: NativeUnixSocket.setSocketOptionInt(fd, optID, expectBoolean(value)); return; default:
@Override public void close() throws IOException { if (streamClosed) { return; } streamClosed = true; if (fd.valid()) { NativeUnixSocket.shutdown(fd, SHUT_RD); } closedInputStream = true; checkClose(); }
@Override public int available() throws IOException { final int av = NativeUnixSocket.available(fd); return av; } }