/*
 * Copyright 2014 Red Hat, Inc.
 *
 * 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 io.nodyn.netty.pipe;

import io.netty.buffer.ByteBuf;
import io.netty.channel.FileRegion;
import io.nodyn.NodeProcess;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

/**
 * @author Bob McWhirter
 */
public class NioOutputStreamChannel extends AbstractNioStreamChannel {

    private final OutputStream out;
    private final Pipe pipe;

    public static NioOutputStreamChannel create(NodeProcess process, OutputStream out) throws IOException {
        Pipe pipe = Pipe.open();
        return new NioOutputStreamChannel(process, out, pipe);
    }

    protected NioOutputStreamChannel(NodeProcess process, OutputStream out, Pipe pipe) {
        super(process, pipe);
        this.pipe = pipe;
        try {
            pipe.sink().configureBlocking(false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.out = out;
        startPump();
    }

    @Override
    protected Pipe.SinkChannel javaChannel() {
        return pipe.sink();
    }

    protected void startPump() {
        this.process.getEventLoop().submitBlockingTask(new Runnable() {
            @Override
            public void run() {
                ByteBuffer buf = ByteBuffer.allocate(1024);
                int numRead = 0;
                try {
                    while ((numRead = NioOutputStreamChannel.this.pipe.source().read(buf)) >= 0) {
                        if (numRead > 0) {
                            byte[] writeMe = buf.array();
                            NioOutputStreamChannel.this.out.write(writeMe, 0, numRead);
                            NioOutputStreamChannel.this.out.flush();
                            buf.clear();
                        }
                    }
                } catch (IOException e) {
                    //e.printStackTrace();
                    try {
                        NioOutputStreamChannel.this.pipe.source().close();
                    } catch (IOException e1) {
                        //e1.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    protected long doWriteFileRegion(FileRegion region) throws Exception {
        return 0;
    }

    @Override
    protected int doReadBytes(ByteBuf byteBuf) throws Exception {
        return 0;
    }

    @Override
    protected int doWriteBytes(ByteBuf buf) throws Exception {
        final int expectedWrittenBytes = buf.readableBytes();
        final int writtenBytes = buf.readBytes(javaChannel(), expectedWrittenBytes);
        return writtenBytes;
    }

    @Override
    protected void doClose() throws Exception {
        this.pipe.source().close();
    }

    public boolean isActive() {
        return this.pipe.source().isOpen();
    }

}

@@ -1364,16 +1364,27 @@
 = pipe;%0A
+        /*%0A
         
@@ -1381,32 +1381,32 @@
 *%0A        try %7B%0A
-
             pipe
@@ -1508,32 +1508,43 @@
 ce();%0A        %7D%0A
+        */%0A
         this.out
@@ -1559,16 +1559,18 @@
         
+//
 startPum
@@ -1575,1178 +1575,8 @@
 ump(
-);%0A    %7D%0A%0A    @Override%0A    protected Pipe.SinkChannel javaChannel() %7B%0A        return pipe.sink();%0A    %7D%0A%0A    protected void startPump() %7B%0A        this.process.getEventLoop().submitBlockingTask(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                ByteBuffer buf = ByteBuffer.allocate(1024);%0A                int numRead = 0;%0A                try %7B%0A                    while ((numRead = NioOutputStreamChannel.this.pipe.source().read(buf)) %3E= 0) %7B%0A                        if (numRead %3E 0) %7B%0A                            byte%5B%5D writeMe = buf.array();%0A                            NioOutputStreamChannel.this.out.write(writeMe, 0, numRead);%0A                            NioOutputStreamChannel.this.out.flush();%0A                            buf.clear();%0A                        %7D%0A                    %7D%0A                %7D catch (IOException e) %7B%0A                    //e.printStackTrace();%0A                    try %7B%0A                        NioOutputStreamChannel.this.pipe.source().close();%0A                    %7D catch (IOException e1) %7B%0A                        //e1.printStackTrace();%0A                    %7D%0A                %7D%0A            %7D%0A        %7D
 );%0A 
@@ -1957,13 +1957,42 @@
 nal 
-int w
+byte%5B%5D bytes = new byte%5B expectedW
 ritt
@@ -1991,33 +1991,42 @@
 tedWrittenBytes 
-=
+%5D;%0A       
  buf.readBytes(j
@@ -2028,43 +2028,74 @@
 tes(
-javaChannel(), expectedWrittenBytes
+ bytes );%0A        this.out.write( bytes );%0A        this.out.flush(
 );%0A 
@@ -2112,20 +2112,20 @@
 urn 
-writtenBytes
+bytes.length
 ;%0A  

Remove the tight loop in writing to OIO from NIO via Netty.