package org.boilit.bsl.encoding;

import org.boilit.bsl.xio.ByteArrayBuffer;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;

/**
 * @author Boilit
 * @see
 */
public abstract class AbstractEncoder implements IEncoder {
    private ThreadLocal<ByteArrayBuffer> localFixedByteArray;

    private final String encoding;

    public AbstractEncoder(final String encoding) {
        this.encoding = encoding;
        this.localFixedByteArray = new ThreadLocal<ByteArrayBuffer>();
        this.localFixedByteArray.set(new ByteArrayBuffer(1024));
    }

    @Override
    public final void write(final OutputStream outputStream, final byte[] bytes) throws IOException {
        outputStream.write(bytes);
    }

    @Override
    public final void write(final Writer writer, final byte[] bytes) throws IOException {
        writer.write(new String(bytes, encoding));
    }

    @Override
    public final void write(final Writer writer, final String string) throws IOException {
        writer.write(string);
    }

    public final String getEncoding() {
        return encoding;
    }

    protected final ByteArrayBuffer getFixedByteArray() {
        return localFixedByteArray.get();
    }
}

@@ -258,16 +258,23 @@
 private 
+static 
 ThreadLo
@@ -313,16 +313,53 @@
 yteArray
+ = new ThreadLocal%3CByteArrayBuffer%3E()
 ;%0A%0A    p
@@ -479,144 +479,8 @@
 ng;%0A
-        this.localFixedByteArray = new ThreadLocal%3CByteArrayBuffer%3E();%0A        this.localFixedByteArray.set(new ByteArrayBuffer(1024));%0A
     
@@ -1082,22 +1082,212 @@
         
-return
+final ByteArrayBuffer buffer = localFixedByteArray.get();%0A        if (buffer != null) %7B%0A            return buffer;%0A        %7D%0A        final ByteArrayBuffer next = new ByteArrayBuffer(1024);%0A       
  localFi
@@ -1295,27 +1295,52 @@
 edByteArray.
-get()
+set(next);%0A        return next
 ;%0A    %7D%0A%7D%0A

update to 1.1.0