// Path: src/main/java/com/wizzardo/epoll/readable/ReadableByteArray.java
// public class ReadableByteArray extends ReadableData {
//     protected byte[] bytes;
//     protected int offset, length, position;
// 
//     public ReadableByteArray(byte[] bytes) {
//         this(bytes, 0, bytes.length);
//     }
// 
//     public ReadableByteArray(byte[] bytes, int offset, int length) {
//         this.bytes = bytes;
//         this.offset = offset;
//         this.length = length;
//         position = offset;
//     }
// 
//     @Override
//     public int read(ByteBuffer byteBuffer) {
//         int r = Math.min(byteBuffer.remaining(), length + offset - position);
//         byteBuffer.put(bytes, position, r);
//         position += r;
//         return r;
//     }
// 
//     @Override
//     public void unread(int i) {
//         if (i < 0)
//             throw new IllegalArgumentException("can't unread negative value: " + i);
//         if (position - i < offset)
//             throw new IllegalArgumentException("can't unread value bigger than offset (" + offset + "): " + i);
//         position -= i;
//     }
// 
//     @Override
//     public boolean isComplete() {
//         return length == position - offset;
//     }
// 
//     @Override
//     public long complete() {
//         return position - offset;
//     }
// 
//     @Override
//     public long length() {
//         return length;
//     }
// 
//     public long remains() {
//         return length + offset - position;
//     }
// 
//     @Override
//     public String toString() {
//         return new String(bytes, offset, length);
//     }
// }


import com.wizzardo.epoll.readable.ReadableByteArray;
import com.wizzardo.tools.misc.Unchecked;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
package com.wizzardo.epoll;



/**
 * @author: wizzardo
 * Date: 5/5/14
 */
public class EpollClientTest {
    //    @Test
    public void simpleTest() throws UnknownHostException {
        final EpollCore epoll = new EpollCore<Connection>() {

            @Override
            protected IOThread createIOThread(int number, int divider) {
                return new IOThread(number, divider) {
                    @Override
                    public void onRead(Connection connection) {
                        System.out.println("onRead " + connection);
                        byte[] b = new byte[1024];
                        int r = 0;

                        try {
                            r = connection.read(b, 0, b.length, this);
                        } catch (IOException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }
                        System.out.println(new String(b, 0, r));
                    }
                };
            }
        };

        epoll.start();
        Connection connection = Unchecked.call(new Callable<Connection>() {
            @Override
            public Connection call() throws Exception {
                return epoll.connect("localhost", 8082);
            }
        });
// Path: src/main/java/com/wizzardo/epoll/readable/ReadableByteArray.java
// public class ReadableByteArray extends ReadableData {
//     protected byte[] bytes;
//     protected int offset, length, position;
// 
//     public ReadableByteArray(byte[] bytes) {
//         this(bytes, 0, bytes.length);
//     }
// 
//     public ReadableByteArray(byte[] bytes, int offset, int length) {
//         this.bytes = bytes;
//         this.offset = offset;
//         this.length = length;
//         position = offset;
//     }
// 
//     @Override
//     public int read(ByteBuffer byteBuffer) {
//         int r = Math.min(byteBuffer.remaining(), length + offset - position);
//         byteBuffer.put(bytes, position, r);
//         position += r;
//         return r;
//     }
// 
//     @Override
//     public void unread(int i) {
//         if (i < 0)
//             throw new IllegalArgumentException("can't unread negative value: " + i);
//         if (position - i < offset)
//             throw new IllegalArgumentException("can't unread value bigger than offset (" + offset + "): " + i);
//         position -= i;
//     }
// 
//     @Override
//     public boolean isComplete() {
//         return length == position - offset;
//     }
// 
//     @Override
//     public long complete() {
//         return position - offset;
//     }
// 
//     @Override
//     public long length() {
//         return length;
//     }
// 
//     public long remains() {
//         return length + offset - position;
//     }
// 
//     @Override
//     public String toString() {
//         return new String(bytes, offset, length);
//     }
// }


// Path: src/test/java/com/wizzardo/epoll/EpollClientTest.java
import com.wizzardo.epoll.readable.ReadableByteArray;
import com.wizzardo.tools.misc.Unchecked;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;

package com.wizzardo.epoll;



/**
 * @author: wizzardo
 * Date: 5/5/14
 */
public class EpollClientTest {
    //    @Test
    public void simpleTest() throws UnknownHostException {
        final EpollCore epoll = new EpollCore<Connection>() {

            @Override
            protected IOThread createIOThread(int number, int divider) {
                return new IOThread(number, divider) {
                    @Override
                    public void onRead(Connection connection) {
                        System.out.println("onRead " + connection);
                        byte[] b = new byte[1024];
                        int r = 0;

                        try {
                            r = connection.read(b, 0, b.length, this);
                        } catch (IOException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }
                        System.out.println(new String(b, 0, r));
                    }
                };
            }
        };

        epoll.start();
        Connection connection = Unchecked.call(new Callable<Connection>() {
            @Override
            public Connection call() throws Exception {
                return epoll.connect("localhost", 8082);
            }
        });
        connection.write(new ReadableByteArray(("GET /1 HTTP/1.1\r\n" +