FileDocCategorySizeDatePackage
LocalSocketImpl.javaAPI DocAndroid 5.1 API16221Thu Mar 12 22:22:10 GMT 2015android.net

LocalSocketImpl

public class LocalSocketImpl extends Object
Socket implementation used for android.net.LocalSocket and android.net.LocalServerSocket. Supports only AF_LOCAL sockets.

Fields Summary
private SocketInputStream
fis
private SocketOutputStream
fos
private Object
readMonitor
private Object
writeMonitor
private FileDescriptor
fd
null if closed or not yet created
private boolean
mFdCreatedInternally
whether fd is created internally
FileDescriptor[]
inboundFileDescriptors
file descriptor array received during a previous read
FileDescriptor[]
outboundFileDescriptors
file descriptor array that should be written during next write
Constructors Summary
LocalSocketImpl()
Create a new instance.

    
LocalSocketImpl(FileDescriptor fd)
Create a new instance from a file descriptor representing a bound socket. The state of the file descriptor is not checked here but the caller can verify socket state by calling listen().

param
fd non-null; bound file descriptor

        this.fd = fd;
    
Methods Summary
private native java.io.FileDescriptoraccept(java.io.FileDescriptor fd, android.net.LocalSocketImpl s)
Accepts a connection on a server socket.

param
fd file descriptor of server socket
param
s socket implementation that will become the new socket
return
file descriptor of new socket

protected voidaccept(android.net.LocalSocketImpl s)
Accepts a new connection to the socket. Blocks until a new connection arrives.

param
s a socket that will be used to represent the new connection.
throws
IOException

        if (fd == null) {
            throw new IOException("socket not created");
        }

        s.fd = accept(fd, s);
        s.mFdCreatedInternally = true;
    
protected intavailable()
Returns the number of bytes available for reading without blocking.

return
>= 0 count bytes available
throws
IOException

        return getInputStream().available();
    
private native intavailable_native(java.io.FileDescriptor fd)

public voidbind(LocalSocketAddress endpoint)
Binds this socket to an endpoint name. May only be called on an instance that has not yet been bound.

param
endpoint endpoint address
throws
IOException

        if (fd == null) {
            throw new IOException("socket not created");
        }

        bindLocal(fd, endpoint.getName(), endpoint.getNamespace().getId());
    
private native voidbindLocal(java.io.FileDescriptor fd, java.lang.String name, int namespace)

public voidclose()
Closes the socket.

throws
IOException

        synchronized (LocalSocketImpl.this) {
            if ((fd == null) || (mFdCreatedInternally == false)) {
                fd = null;
                return;
            }
            try {
                Os.close(fd);
            } catch (ErrnoException e) {
                e.rethrowAsIOException();
            }
            fd = null;
        }
    
protected voidconnect(LocalSocketAddress address, int timeout)
note timeout presently ignored

        
        if (fd == null) {
            throw new IOException("socket not created");
        }

        connectLocal(fd, address.getName(), address.getNamespace().getId());
    
private native voidconnectLocal(java.io.FileDescriptor fd, java.lang.String name, int namespace)

public voidcreate(int sockType)
Creates a socket in the underlying OS.

param
sockType either {@link LocalSocket#SOCKET_DGRAM}, {@link LocalSocket#SOCKET_STREAM} or {@link LocalSocket#SOCKET_SEQPACKET}
throws
IOException

        // no error if socket already created
        // need this for LocalServerSocket.accept()
        if (fd == null) {
            int osType;
            switch (sockType) {
                case LocalSocket.SOCKET_DGRAM:
                    osType = OsConstants.SOCK_DGRAM;
                    break;
                case LocalSocket.SOCKET_STREAM:
                    osType = OsConstants.SOCK_STREAM;
                    break;
                case LocalSocket.SOCKET_SEQPACKET:
                    osType = OsConstants.SOCK_SEQPACKET;
                    break;
                default:
                    throw new IllegalStateException("unknown sockType");
            }
            try {
                fd = Os.socket(OsConstants.AF_UNIX, osType, 0);
                mFdCreatedInternally = true;
            } catch (ErrnoException e) {
                e.rethrowAsIOException();
            }
        }
    
protected voidfinalize()

        close();
    
public java.io.FileDescriptor[]getAncillaryFileDescriptors()
Retrieves a set of file descriptors that a peer has sent through an ancillary message. This method retrieves the most recent set sent, and then returns null until a new set arrives. File descriptors may only be passed along with regular data, so this method can only return a non-null after a read operation.

return
null or file descriptor array
throws
IOException

        synchronized(readMonitor) {
            FileDescriptor[] result = inboundFileDescriptors;

            inboundFileDescriptors = null;
            return result;
        }
    
protected java.io.FileDescriptorgetFileDescriptor()

        return fd;
    
protected java.io.InputStreamgetInputStream()
Retrieves the input stream for this instance.

return
input stream
throws
IOException if socket has been closed or cannot be created.

        if (fd == null) {
            throw new IOException("socket not created");
        }

        synchronized (this) {
            if (fis == null) {
                fis = new SocketInputStream();
            }

            return fis;
        }
    
public java.lang.ObjectgetOption(int optID)

        if (fd == null) {
            throw new IOException("socket not created");
        }

        if (optID == SocketOptions.SO_TIMEOUT) {
            return 0;
        }
        
        int value = getOption_native(fd, optID);
        switch (optID)
        {
            case SocketOptions.SO_RCVBUF:
            case SocketOptions.SO_SNDBUF:
                return value;
            case SocketOptions.SO_REUSEADDR:
            default:
                return value;
        }
    
private native intgetOption_native(java.io.FileDescriptor fd, int optID)

protected java.io.OutputStreamgetOutputStream()
Retrieves the output stream for this instance.

return
output stream
throws
IOException if socket has been closed or cannot be created.

 
        if (fd == null) {
            throw new IOException("socket not created");
        }

        synchronized (this) {
            if (fos == null) {
                fos = new SocketOutputStream();
            }

            return fos;
        }
    
public CredentialsgetPeerCredentials()
Retrieves the credentials of this socket's peer. Only valid on connected sockets.

return
non-null; peer credentials
throws
IOException

        return getPeerCredentials_native(fd);
    
private native CredentialsgetPeerCredentials_native(java.io.FileDescriptor fd)

public LocalSocketAddressgetSockAddress()
Retrieves the socket name from the OS.

return
non-null; socket name
throws
IOException on failure

        return null;
        //TODO implement this
        //return getSockName_native(fd);
    
protected voidlisten(int backlog)

        if (fd == null) {
            throw new IOException("socket not created");
        }

        listen_native(fd, backlog);
    
private native voidlisten_native(java.io.FileDescriptor fd, int backlog)

private native intpending_native(java.io.FileDescriptor fd)

private native intread_native(java.io.FileDescriptor fd)

private native intreadba_native(byte[] b, int off, int len, java.io.FileDescriptor fd)

protected voidsendUrgentData(int data)

        throw new RuntimeException ("not impled");
    
public voidsetFileDescriptorsForSend(java.io.FileDescriptor[] fds)
Enqueues a set of file descriptors to send to the peer. The queue is one deep. The file descriptors will be sent with the next write of normal data, and will be delivered in a single ancillary message. See "man 7 unix" SCM_RIGHTS on a desktop Linux machine.

param
fds non-null; file descriptors to send.
throws
IOException

        synchronized(writeMonitor) {
            outboundFileDescriptors = fds;
        }
    
public voidsetOption(int optID, java.lang.Object value)

        /*
         * Boolean.FALSE is used to disable some options, so it
         * is important to distinguish between FALSE and unset.
         * We define it here that -1 is unset, 0 is FALSE, and 1
         * is TRUE.
         */
        int boolValue = -1;
        int intValue = 0;

        if (fd == null) {
            throw new IOException("socket not created");
        }

        if (value instanceof Integer) {
            intValue = (Integer)value;
        } else if (value instanceof Boolean) {
            boolValue = ((Boolean) value)? 1 : 0;
        } else {
            throw new IOException("bad value: " + value);
        }

        setOption_native(fd, optID, boolValue, intValue);
    
private native voidsetOption_native(java.io.FileDescriptor fd, int optID, int b, int value)

private native voidshutdown(java.io.FileDescriptor fd, boolean shutdownInput)

protected voidshutdownInput()
Shuts down the input side of the socket.

throws
IOException

        if (fd == null) {
            throw new IOException("socket not created");
        }

        shutdown(fd, true);
    
protected voidshutdownOutput()
Shuts down the output side of the socket.

throws
IOException

        if (fd == null) {
            throw new IOException("socket not created");
        }

        shutdown(fd, false);
    
protected booleansupportsUrgentData()

        return false;
    
public java.lang.StringtoString()

        return super.toString() + " fd:" + fd;
    
private native voidwrite_native(int b, java.io.FileDescriptor fd)

private native voidwriteba_native(byte[] b, int off, int len, java.io.FileDescriptor fd)