FileDocCategorySizeDatePackage
LocalSocket.javaAPI DocAndroid 5.1 API9393Thu Mar 12 22:22:10 GMT 2015android.net

LocalSocket

public class LocalSocket extends Object implements Closeable
Creates a (non-server) socket in the UNIX-domain namespace. The interface here is not entirely unlike that of java.net.Socket

Fields Summary
private LocalSocketImpl
impl
private volatile boolean
implCreated
private LocalSocketAddress
localAddress
private boolean
isBound
private boolean
isConnected
private final int
sockType
static final int
SOCKET_UNKNOWN
unknown socket type (used for constructor with existing file descriptor)
public static final int
SOCKET_DGRAM
Datagram socket type
public static final int
SOCKET_STREAM
Stream socket type
public static final int
SOCKET_SEQPACKET
Sequential packet socket type
Constructors Summary
public LocalSocket()
Creates a AF_LOCAL/UNIX domain stream socket.


               
      
        this(SOCKET_STREAM);
    
public LocalSocket(int sockType)
Creates a AF_LOCAL/UNIX domain stream socket with given socket type

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

        this(new LocalSocketImpl(), sockType);
        isBound = false;
        isConnected = false;
    
public LocalSocket(FileDescriptor fd)
Creates a AF_LOCAL/UNIX domain stream socket with FileDescriptor.

hide

        this(new LocalSocketImpl(fd), SOCKET_UNKNOWN);
        isBound = true;
        isConnected = true;
    
LocalSocket(LocalSocketImpl impl, int sockType)
for use with AndroidServerSocket

param
impl a SocketImpl

        this.impl = impl;
        this.sockType = sockType;
        this.isConnected = false;
        this.isBound = false;
    
Methods Summary
public voidbind(LocalSocketAddress bindpoint)
Binds this socket to an endpoint name. May only be called on an instance that has not yet been bound.

param
bindpoint endpoint address
throws
IOException

        implCreateIfNeeded();

        synchronized (this) {
            if (isBound) {
                throw new IOException("already bound");
            }

            localAddress = bindpoint;
            impl.bind(localAddress);
            isBound = true;
        }
    
public voidclose()
Closes the socket.

throws
IOException

        implCreateIfNeeded();
        impl.close();
    
public voidconnect(LocalSocketAddress endpoint, int timeout)

        throw new UnsupportedOperationException();
    
public voidconnect(LocalSocketAddress endpoint)
Connects this socket to an endpoint. May only be called on an instance that has not yet been connected.

param
endpoint endpoint address
throws
IOException if socket is in invalid state or the address does not exist.

        synchronized (this) {
            if (isConnected) {
                throw new IOException("already connected");
            }

            implCreateIfNeeded();
            impl.connect(endpoint, 0);
            isConnected = true;
            isBound = true;
        }
    
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

        return impl.getAncillaryFileDescriptors();
    
public java.io.FileDescriptorgetFileDescriptor()
Returns file descriptor or null if not yet open/already closed

return
fd or null

        return impl.getFileDescriptor();
    
public java.io.InputStreamgetInputStream()
Retrieves the input stream for this instance.

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

        implCreateIfNeeded();
        return impl.getInputStream();
    
public LocalSocketAddressgetLocalSocketAddress()
Retrieves the name that this socket is bound to, if any.

return
Local address or null if anonymous

        return localAddress;
    
public java.io.OutputStreamgetOutputStream()
Retrieves the output stream for this instance.

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

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

return
non-null; peer credentials
throws
IOException

        return impl.getPeerCredentials();
    
public intgetReceiveBufferSize()

        return ((Integer) impl.getOption(SocketOptions.SO_RCVBUF)).intValue();
    
public LocalSocketAddressgetRemoteSocketAddress()

        throw new UnsupportedOperationException();
    
public intgetSendBufferSize()

        return ((Integer) impl.getOption(SocketOptions.SO_SNDBUF)).intValue();
    
public intgetSoTimeout()

        return ((Integer) impl.getOption(SocketOptions.SO_TIMEOUT)).intValue();
    
private voidimplCreateIfNeeded()
It's difficult to discern from the spec when impl.create() should be called, but it seems like a reasonable rule is "as soon as possible, but not in a context where IOException cannot be thrown"

throws
IOException from SocketImpl.create()

        if (!implCreated) {
            synchronized (this) {
                if (!implCreated) {
                    try {
                        impl.create(sockType);
                    } finally {
                        implCreated = true;
                    }
                }
            }
        }
    
public synchronized booleanisBound()

        return isBound;
    
public booleanisClosed()

        throw new UnsupportedOperationException();
    
public synchronized booleanisConnected()

        return isConnected;
    
public booleanisInputShutdown()

        throw new UnsupportedOperationException();
    
public booleanisOutputShutdown()

        throw new UnsupportedOperationException();
    
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.

        impl.setFileDescriptorsForSend(fds);
    
public voidsetReceiveBufferSize(int size)

        impl.setOption(SocketOptions.SO_RCVBUF, Integer.valueOf(size));
    
public voidsetSendBufferSize(int n)

        impl.setOption(SocketOptions.SO_SNDBUF, Integer.valueOf(n));
    
public voidsetSoTimeout(int n)

        impl.setOption(SocketOptions.SO_TIMEOUT, Integer.valueOf(n));
    
public voidshutdownInput()
Shuts down the input side of the socket.

throws
IOException

        implCreateIfNeeded();
        impl.shutdownInput();
    
public voidshutdownOutput()
Shuts down the output side of the socket.

throws
IOException

        implCreateIfNeeded();
        impl.shutdownOutput();
    
public java.lang.StringtoString()
{@inheritDoc}

        return super.toString() + " impl:" + impl;