package com.alibaba.otter.canal.parse.driver.mysql.utils;

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

import com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket;
import com.alibaba.otter.canal.parse.driver.mysql.socket.SocketChannel;

public abstract class PacketManager {

    public static HeaderPacket readHeader(SocketChannel ch, int len) throws IOException {
        HeaderPacket header = new HeaderPacket();
        header.fromBytes(readBytesAsBuffer(ch, len).array());
        return header;
    }

    public static ByteBuffer readBytesAsBuffer(SocketChannel ch, int len) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(len);
        while (buffer.hasRemaining()) {
            int readNum = ch.read(buffer);
            if (readNum == -1) {
                throw new IOException("Unexpected End Stream");
            }
        }
        return buffer;
    }

    public static byte[] readBytes(SocketChannel ch, int len) throws IOException {
        return readBytesAsBuffer(ch, len).array();
    }

    /**
     * Since We r using blocking IO, so we will just write once and assert the
     * length to simplify the read operation.<br>
     * If the block write doesn't work as we expected, we will change this
     * implementation as per the result.
     * 
     * @param ch
     * @param len
     * @return
     * @throws IOException
     */
    public static void write(SocketChannel ch, byte[]... srcs) throws IOException {
        ch.writeChannel(srcs);
    }

    public static void write(SocketChannel ch, byte[] body) throws IOException {
        write(ch, body, (byte) 0);
    }

    public static void write(SocketChannel ch, byte[] body, byte packetSeqNumber) throws IOException {
        HeaderPacket header = new HeaderPacket();
        header.setPacketBodyLength(body.length);
        header.setPacketSequenceNumber(packetSeqNumber);
        write(ch, header.toBytes(),body);
    }
}

@@ -86,37 +86,8 @@
 on;%0D
-%0Aimport java.nio.ByteBuffer;%0D
 %0A%0D%0Ai
@@ -445,459 +445,43 @@
 tes(
-readBytesAsBuffer(ch, len).array());%0D%0A        return header;%0D%0A    %7D%0D%0A%0D%0A    public static ByteBuffer readBytesAsBuffer(SocketChannel ch, int len) throws IOException %7B%0D%0A        ByteBuffer buffer = ByteBuffer.allocate(len);%0D%0A        while (buffer.hasRemaining()) %7B%0D%0A            int readNum = ch.read(buffer);%0D%0A            if (readNum == -1) %7B%0D%0A                throw new IOException(%22Unexpected End Stream%22);%0D%0A            %7D%0D%0A        %7D%0D%0A        return buff
+ch.read(len));%0D%0A        return head
 er;%0D
@@ -593,409 +593,30 @@
 urn 
+ch.
 read
-BytesAsBuffer(ch, len).array();%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Since We r using blocking IO, so we will just write once and assert the%0D%0A     * length to simplify the read operation.%3Cbr%3E%0D%0A     * If the block write doesn't work as we expected, we will change this%0D%0A     * implementation as per the result.%0D%0A     * %0D%0A     * @param ch%0D%0A     * @param len%0D%0A     * @return%0D%0A     * @throws IOException%0D%0A     */
+(len);%0D%0A    %7D%0D%0A
 %0D%0A  
@@ -633,32 +633,35 @@
 tatic void write
+Pkg
 (SocketChannel c
@@ -732,32 +732,36 @@
 (srcs);%0D%0A    %7D%0D%0A
+    
 %0D%0A    public sta
@@ -766,32 +766,36 @@
 tatic void write
+Body
 (SocketChannel c
@@ -832,33 +832,35 @@
 tion %7B%0D%0A    
-    
+%09
 write
+Body0
 (ch, body, (
@@ -907,16 +907,21 @@
 id write
+Body0
 (SocketC
@@ -1159,18 +1159,24 @@
     
+ch.
 write
-(ch, 
+Channel(
 head

Update PacketManager.java