/*
 * Copyright (C) 2016 Fritz Elfert
 *
 * 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.enxio.channels;

import java.io.IOException;
import java.nio.ByteBuffer;

import jnr.constants.platform.Errno;

/**
 * Helper class, providing common methods.
 */
final class Common {

    private int _fd = -1;

    Common(int fd) {
        _fd = fd;
    }

    void setFD(int fd) {
        _fd = fd;
    }

    int getFD() {
        return _fd;
    }

    int read(ByteBuffer dst) throws IOException {

        ByteBuffer buffer = ByteBuffer.allocate(dst.remaining());

        int n = Native.read(_fd, buffer);

        buffer.flip();

        dst.put(buffer);

        switch (n) {
            case 0:
                return -1;

            case -1:
                Errno lastError = Native.getLastError();
                switch (lastError) {
                    case EAGAIN:
                    case EWOULDBLOCK:
                        return 0;

                    default:
                        throw new IOException(Native.getLastErrorString());
                }

            default: {

                         return n;
            }
        }
    }

    long read(ByteBuffer[] dsts, int offset, int length)
        throws IOException {
        long total = 0;

        for (int i = 0; i < length; i++) {
            ByteBuffer dst = dsts[offset + i];
            long read = read(dst);
            if (read == -1) {
                return read;
            }
            total += read;
        }

        return total;
    }

    int write(ByteBuffer src) throws IOException {

        ByteBuffer buffer = ByteBuffer.allocate(src.remaining());

        buffer.put(src);

        buffer.position(0);

        int n = Native.write(_fd, buffer);

        if (n < 0) {
            switch (Native.getLastError()) {
                case EAGAIN:
                case EWOULDBLOCK:
                    return 0;
            default:
                throw new IOException(Native.getLastErrorString());
            }
        }

        return n;
    }

    long write(ByteBuffer[] srcs, int offset, int length)
        throws IOException {

        long result = 0;
        int index = 0;

        for (index = offset; index < length; index++) {
            result += write(srcs[index]);
        }

        return result;
    }

}

@@ -2137,32 +2137,74 @@
  IOException %7B%0A%0A
+        int r = src.remaining();%0A        %0A
         ByteBuff
@@ -2239,26 +2239,20 @@
 ate(
-src.remaining());%0A
+r);%0A        
 %0A   
@@ -2273,16 +2273,24 @@
 t(src);%0A
+        
 %0A       
@@ -2347,32 +2347,58 @@
 (_fd, buffer);%0A%0A
+        if (n %3E=0 ) %7B%0A    
         if (n %3C 
@@ -2401,13 +2401,96 @@
 n %3C 
-0
+r
 ) %7B%0A
+                src.position(src.position()-(r-n));%0A            %7D%0A        %7D else %7B%0A
     
@@ -2585,32 +2585,84 @@
 se EWOULDBLOCK:%0A
+                    src.position(src.position()-r);%0A
                 

Fix #50 buffer consumed on flow controlled write