package eu.toolchain.serializer.io;

import eu.toolchain.serializer.Serializer;
import eu.toolchain.serializer.SharedPool;

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

public class CoreByteChannelSerialWriter extends AbstractSerialWriter {
    final ByteBuffer one = ByteBuffer.allocate(1);

    final WritableByteChannel channel;

    public CoreByteChannelSerialWriter(
        final SharedPool pool, final Serializer<Integer> scopeSize,
        final WritableByteChannel channel
    ) {
        super(pool, scopeSize);
        this.channel = channel;
    }

    @Override
    public void write(byte b) throws IOException {
        one.put(b);
        one.flip();
        channel.write(one);
        one.flip();
    }

    @Override
    public void write(byte[] bytes, int offset, int length) throws IOException {
        channel.write(ByteBuffer.wrap(bytes, offset, length));
    }
}

@@ -116,17 +116,16 @@
 edPool;%0A
-%0A
 import j
@@ -652,14 +652,31 @@
 ite(
-byte b
+final ByteBuffer buffer
 ) th
@@ -706,65 +706,196 @@
     
-one.put(b);%0A        one.flip();%0A        channel.write(one
+while (buffer.remaining() %3E 0) %7B%0A            channel.write(buffer);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void write(byte b) throws IOException %7B%0A        one.reset();%0A        one.put(b
 );%0A 
@@ -909,24 +909,44 @@
 one.flip();%0A
+        write(one);%0A
     %7D%0A%0A    @
@@ -1039,32 +1039,24 @@
 n %7B%0A        
-channel.
 write(ByteBu
@@ -1095,11 +1095,102 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void close() throws IOException %7B%0A        channel.close();%0A    %7D
 %0A%7D%0A

fix CoreByteChannelSerialWriter