/*
 * Copyright 2013 The Netty Project
 *
 * The Netty Project licenses this file to you 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.netty.channel.oio;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.FileRegion;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.NotYetConnectedException;
import java.nio.channels.WritableByteChannel;

/**
 * Abstract base class for OIO Channels that are based on streams.
 */
public abstract class OioByteStreamChannel extends AbstractOioByteChannel {

    private InputStream is;
    private OutputStream os;
    private WritableByteChannel outChannel;

    /**
     * Create a new instance
     *
     * @param parent    the parent {@link Channel} which was used to create this instance. This can be null if the
     *                  {@link} has no parent as it was created by your self.
     * @param id        the id which should be used for this instance or {@code null} if a new one should be generated
     */
    protected OioByteStreamChannel(Channel parent, Integer id) {
        super(parent, id);
    }

    /**
     * Activate this instance. After this call {@link #isActive()} will return {@code true}.
     */
    protected final void activate(InputStream is, OutputStream os) {
        if (this.is != null) {
            throw new IllegalStateException("input was set already");
        }
        if (this.os != null) {
            throw new IllegalStateException("output was set already");
        }
        if (is == null) {
            throw new NullPointerException("is");
        }
        if (os == null) {
            throw new NullPointerException("os");
        }
        this.is = is;
        this.os = os;
    }

    @Override
    public boolean isActive() {
        return is != null && os != null;
    }

    @Override
    protected int available() {
        try {
            return is.available();
        } catch (IOException e) {
            return 0;
        }
    }

    @Override
    protected int doReadBytes(ByteBuf buf) throws Exception {
        int length = Math.max(1, Math.min(available(), buf.maxWritableBytes()));
        return buf.writeBytes(is, length);
    }

    @Override
    protected void doWriteBytes(ByteBuf buf) throws Exception {
        OutputStream os = this.os;
        if (os == null) {
            throw new NotYetConnectedException();
        }
        buf.readBytes(os, buf.readableBytes());
    }

    @Override
    protected void doWriteFileRegion(FileRegion region) throws Exception {
        OutputStream os = this.os;
        if (os == null) {
            throw new NotYetConnectedException();
        }
        if (outChannel == null) {
            outChannel = Channels.newChannel(os);
        }

        long written = 0;
        for (;;) {
            long localWritten = region.transferTo(outChannel, written);
            if (localWritten == -1) {
                checkEOF(region);
                return;
            }
            written += localWritten;

            if (written >= region.count()) {
                return;
            }
        }
    }

    @Override
    protected void doClose() throws Exception {
        IOException ex = null;

        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            ex = e;
        }

        try {
            if (os != null) {
                os.close();
            }
        } catch (IOException e) {
            ex = e;
        }

        is = null;
        os = null;

        if (ex != null) {
            throw ex;
        }
    }
}

@@ -879,16 +879,65 @@
 annels;%0A
+import java.nio.channels.ClosedChannelException;%0A
 import j
@@ -1178,16 +1178,382 @@
 nnel %7B%0A%0A
+    private static final InputStream CLOSED_IN = new InputStream() %7B%0A        @Override%0A        public int read() %7B%0A            return -1;%0A        %7D%0A    %7D;%0A%0A    private static final OutputStream CLOSED_OUT = new OutputStream() %7B%0A        @Override%0A        public void write(int b) throws IOException %7B%0A            throw new ClosedChannelException();%0A        %7D%0A    %7D;%0A%0A
     priv
@@ -2791,39 +2791,253 @@
     
-return is !
+InputStream is = this.is;%0A        if (is == null %7C%7C is == CLOSED_IN) %7B%0A            return false;%0A        %7D%0A%0A        OutputStream os = this.os;%0A        if (os =
 = null 
-&&
+%7C%7C
  os 
-!= null
+== CLOSED_OUT) %7B%0A            return false;%0A        %7D%0A%0A        return true
 ;%0A  
@@ -4415,180 +4415,126 @@
    I
-OException ex = null;%0A%0A        try %7B%0A            if (i
+nputStream is = this.is;%0A        OutputStream o
 s 
-!
 = 
-null) %7B%0A                is.close();%0A            %7D%0A        %7D catch (IOException e) %7B%0A            ex = e;%0A        %7D
+this.os;%0A        this.is = CLOSED_IN;%0A        this.os = CLOSED_OUT;
 %0A%0A  
@@ -4561,17 +4561,17 @@
     if (
-o
+i
 s != nul
@@ -4591,17 +4591,17 @@
         
-o
+i
 s.close(
@@ -4631,81 +4631,47 @@
   %7D 
-catch (IOException e) %7B%0A            ex = e;%0A        %7D%0A%0A        i
+finally %7B%0A            if (o
 s 
+!
 = null
-;
+) %7B
 %0A   
@@ -4679,45 +4679,27 @@
     
-os = null;%0A%0A        if (ex != null) %7B
+        os.close();
 %0A   
@@ -4711,17 +4711,9 @@
     
-throw ex;
+%7D
 %0A   

Fix NPE in OioByteStreamChannel