package com.wizzardo.http;

import com.wizzardo.epoll.readable.ReadableByteArray;
import com.wizzardo.tools.misc.pool.*;

import java.io.IOException;

/**
 * Created by wizzardo on 26/05/16.
 */
public class ReadableByteArrayPool {
    private static Pool<PooledReadableByteArray> pool = new ThreadLocalPool<PooledReadableByteArray>() {
        @Override
        public PooledReadableByteArray create() {
            return new PooledReadableByteArray(new byte[10240], this);
        }

        @Override
        protected Holder<PooledReadableByteArray> createHolder(PooledReadableByteArray value) {
            return value.holder;
        }
    };

    public static class PooledReadableByteArray extends ReadableByteArray {
        final Holder<PooledReadableByteArray> holder;

        PooledReadableByteArray(byte[] bytes, Pool<PooledReadableByteArray> pool) {
            super(bytes);
            holder = new SoftHolder<>(pool, this);
        }

        public byte[] bytes() {
            return bytes;
        }

        public void length(int length) {
            this.length = length;
        }

        @Override
        public void close() throws IOException {
            holder.close();
        }
    }

    public static PooledReadableByteArray get() {
        PooledReadableByteArray buffer = pool.get();
        buffer.unread((int) buffer.complete());
        return buffer;
    }
}

@@ -289,23 +289,19 @@
 new 
-ThreadLocalPool
+PoolBuilder
 %3CPoo
@@ -323,18 +323,16 @@
 Array%3E()
- %7B
 %0A       
@@ -336,158 +336,253 @@
     
-@Override%0A        public PooledReadableByteArray create() %7B%0A            return new PooledReadableByteArray(new byte%5B10240%5D, this);
+    .supplier(() -%3E new PooledReadableByteArray(new byte%5B10240%5D))%0A            .holder((pool, value, resetter) -%3E %7B%0A                SoftHolder%3CPooledReadableByteArray%3E holder = new SoftHolder%3CPooledReadableByteArray%3E(pool, value) %7B
 %0A      
-  %7D%0A%0A
         
 @Ove
@@ -581,114 +581,171 @@
     
-@Override%0A        protected Holder%3CPooledReadableByteArray%3E createHolder(PooledReadableByteArray value) %7B%0A
+      public PooledReadableByteArray get() %7B%0A                        PooledReadableByteArray t = super.get();%0A                        resetter.consume(t);%0A        
     
@@ -752,23 +752,87 @@
         
+    
 return 
+t;%0A                    %7D%0A                %7D;%0A                
 value.ho
@@ -835,16 +835,25 @@
 e.holder
+ = holder
 ;%0A      
@@ -854,23 +854,134 @@
         
-%7D%0A    %7D
+        return holder;%0A            %7D)%0A            .resetter(it -%3E it.unread((int) it.complete()))%0A            .build()
 ;%0A%0A    p
@@ -1063,13 +1063,24 @@
     
-final
+private volatile
  Hol
@@ -1165,121 +1165,34 @@
 ytes
-, Pool%3CPooledReadableByteArray%3E pool) %7B%0A            super(bytes);%0A            holder = new SoftHolder%3C%3E(pool, thi
+) %7B%0A            super(byte
 s);%0A
@@ -1531,122 +1531,25 @@
     
-PooledReadableByteArray buffer = pool.get();%0A        buffer.unread((int) buffer.complete());%0A        return buffer
+return pool.get()
 ;%0A  

update from tools-lib