/*
 * Copyright (C) 2009 Wayne Meissner
 * Copyright (C) 2016 Marcus Linke
 * 
 * (ported from https://github.com/softprops/unisockets/blob/master/unisockets-core/src/main/scala/Socket.scala)
 *
 * This file is part of the JNR project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package jnr.unixsocket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.util.concurrent.atomic.AtomicBoolean;

class UnixSocket extends java.net.Socket {

    private UnixSocketChannel chan;

    private AtomicBoolean closed = new AtomicBoolean(false);
    private AtomicBoolean indown = new AtomicBoolean(false);
    private AtomicBoolean outdown = new AtomicBoolean(false);

    private InputStream in;
    private OutputStream out;

    public UnixSocket(UnixSocketChannel chan) {
        this.chan = chan;
        in = Channels.newInputStream(chan);
        out = Channels.newOutputStream(chan);
    }

    public void bind(SocketAddress addr) {
        throw new UnsupportedOperationException("bind not supported");
    }

    @Override
    public void close() throws IOException {
        chan.close();
        closed.compareAndSet(false, true);
    }

    @Override
    public void connect(SocketAddress addr) throws IOException {
        connect(addr, 0);
    }

    public void connect(SocketAddress addr, Integer timeout) throws IOException {
        if (addr instanceof UnixSocketAddress) {
            chan.connect((UnixSocketAddress) addr);
        } else {
            throw new IllegalArgumentException("address of type "
                    + addr.getClass() + " are not supported. Use "
                    + UnixSocketAddress.class + " instead");
        }
    }

    @Override
    public SocketChannel getChannel() {
        return chan;
    }

    @Override
    public InetAddress getInetAddress() {
        return null;
    }

    public InputStream getInputStream() throws IOException {
        if (chan.isConnected()) {
            return in;
        } else {
            throw new IOException("not connected");
        }
    }

    @Override
    public SocketAddress getLocalSocketAddress() {
        UnixSocketAddress address = chan.getLocalSocketAddress();
        if (address != null) {
            return address;
        } else {
            return null;
        }
    }

    @Override
    public OutputStream getOutputStream() throws IOException {
        if (chan.isConnected()) {
            return out;
        } else {
            throw new IOException("not connected");
        }
    }

    @Override
    public SocketAddress getRemoteSocketAddress() {
        SocketAddress address = chan.getRemoteSocketAddress();

        if (address != null) {
            return address;
        } else {
            return null;
        }
    }

    @Override
    public boolean isBound() {
        return false;
    }

    @Override
    public boolean isClosed() {
        return closed.get();
    }

    @Override
    public boolean isConnected() {
        return chan.isConnected();
    }

    @Override
    public boolean isInputShutdown() {
        return indown.get();
    }

    @Override
    public boolean isOutputShutdown() {
        return outdown.get();
    }

    @Override
    public void shutdownInput() throws IOException {
        chan.shutdownInput();
        indown.compareAndSet(false, true);
    }

    @Override
    public void shutdownOutput() throws IOException {
        chan.shutdownOutput();
        outdown.compareAndSet(false, true);
    }

    /**
     * Retrieves the credentials for this UNIX socket. Clients calling this
     * method will receive the server's credentials, and servers will receive
     * the client's credentials. User ID, group ID, and PID are supplied.
     *
     * See man unix 7; SCM_CREDENTIALS
     *
     * @throws UnsupportedOperationException if the underlying socket library
     *         doesn't support the SO_PEERCRED option
     *
     * @return the credentials of the remote
     */
    public final Credentials getCredentials() {
        return chan.getCredentials();
    }
}

@@ -1778,30 +1778,12 @@
     
-chan.close();%0A        
+if (
 clos
@@ -1803,32 +1803,71 @@
 Set(false, true)
+) %7B%0A            chan.close();%0A        %7D
 ;%0A    %7D%0A%0A    @Ov
@@ -3976,38 +3976,12 @@
     
-chan.shutdownInput();%0A        
+if (
 indo
@@ -4001,33 +4001,88 @@
 Set(false, true)
-;
+) %7B%0A            chan.shutdownInput();%0A        %7D;        
 %0A    %7D%0A%0A    @Ove
@@ -4153,39 +4153,12 @@
     
-chan.shutdownOutput();%0A        
+if (
 outd
@@ -4187,17 +4187,71 @@
 e, true)
-;
+) %7B%0A            chan.shutdownOutput();%0A        %7D       
 %0A    %7D%0A%0A

Fix use of AtomicBooleans