// Path: src/main/java/cc/agentx/util/KeyHelper.java
// public class KeyHelper {
//     private static SecureRandom randomizer = new SecureRandom();
// 
//     private KeyHelper() {
//     }
// 
//     public static int generateRandomInteger(int min, int max) {
//         return min + randomizer.nextInt(max - min);
//     }
// 
//     public static byte[] generateRandomBytes(int length) {
//         byte[] bytes = new byte[length];
//         randomizer.nextBytes(bytes);
//         return bytes;
//     }
// 
//     public static byte[] generateKeyDigest(int keyLength, String password) {
//         try {
//             MessageDigest digester = MessageDigest.getInstance("MD5");
//             int length = (keyLength + 15) / 16 * 16;
//             byte[] passwordBytes = password.getBytes("UTF-8");
//             byte[] temp = digester.digest(passwordBytes);
//             byte[] key = Arrays.copyOf(temp, length);
//             for (int i = 1; i < length / 16; i++) {
//                 temp = Arrays.copyOf(temp, 16 + passwordBytes.length);
//                 System.arraycopy(passwordBytes, 0, temp, 16, passwordBytes.length);
//                 System.arraycopy(digester.digest(temp), 0, key, i * 16, 16);
//             }
//             return Arrays.copyOf(key, keyLength);
//         } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
//             e.printStackTrace();
//         }
//         return new byte[keyLength];
//     }
// 
//     public static byte[] getCompressedBytes(int value) {
//         byte[] bytes = new byte[4];
//         bytes[0] = (byte) ((value >> 24) & 0xff);
//         bytes[1] = (byte) ((value >> 16) & 0xff);
//         bytes[2] = (byte) ((value >> 8) & 0xff);
//         bytes[3] = (byte) (value & 0xff);
//         if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0)
//             return Arrays.copyOfRange(bytes, 3, 4);
//         if (bytes[0] == 0 && bytes[1] == 0)
//             return Arrays.copyOfRange(bytes, 2, 4);
//         if (bytes[0] == 0)
//             return Arrays.copyOfRange(bytes, 1, 4);
//         return bytes;
//     }
// 
//     public static byte[] getBytes(int length, int value) {
//         if (length < 1 || length > 4)
//             throw new RuntimeException("bad length");
//         byte[] bytes = new byte[4];
//         bytes[0] = (byte) ((value >> 24) & 0xff);
//         bytes[1] = (byte) ((value >> 16) & 0xff);
//         bytes[2] = (byte) ((value >> 8) & 0xff);
//         bytes[3] = (byte) (value & 0xff);
//         if (length == 4)
//             return bytes;
//         else
//             return Arrays.copyOfRange(bytes, 4 - length, 4);
//     }
// 
//     public static byte[] getBytes(int value) {
//         return getBytes(4, value);
//     }
// 
//     public static int toBigEndianInteger(byte[] bytes) {
//         byte[] value = new byte[4];
//         System.arraycopy(bytes, 0, value, 4 - bytes.length, bytes.length);
//         return (value[0] & 0xff) << 24 | (value[1] & 0xff) << 16
//                 | (value[2] & 0xff) << 8 | (value[3] & 0xff);
//     }
// 
//     public int getRandomIdentifier(int length) {
//         int base = (int) Math.pow(10, length - 1);
//         return randomizer.nextInt(base * 9) + base;
//     }
// }


import cc.agentx.util.KeyHelper;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
            reservedHeaderLength = 4;
    }

    /**
     * frameHandler will be invoked before the frame re-concat into
     * data stream, see unwrap().
     */
    public FrameWrapper(int fixedFrameLength, Wrapper frameHandler) {
        this(fixedFrameLength);
        this.frameHandler = frameHandler;
    }

    /*
     * data will be wrapped into several frames, but marked as a whole
     * chunk (we call it a data-package)
     */
    @Override
    public byte[] wrap(final byte[] bytes) {
        byte[] payload;
        if (frameHandler == null) {
            payload = bytes;
        } else {
            payload = frameHandler.wrap(bytes);
        }
        int i, nof = (payload.length / frameLength) + (payload.length % frameLength == 0 ? 0 : 1);
        for (i = 0; i < nof - 1; i++) {
            wrapBuffer.write(1);
            wrapBuffer.write(payload, i * frameLength, frameLength);
        }
        wrapBuffer.write(0);
// Path: src/main/java/cc/agentx/util/KeyHelper.java
// public class KeyHelper {
//     private static SecureRandom randomizer = new SecureRandom();
// 
//     private KeyHelper() {
//     }
// 
//     public static int generateRandomInteger(int min, int max) {
//         return min + randomizer.nextInt(max - min);
//     }
// 
//     public static byte[] generateRandomBytes(int length) {
//         byte[] bytes = new byte[length];
//         randomizer.nextBytes(bytes);
//         return bytes;
//     }
// 
//     public static byte[] generateKeyDigest(int keyLength, String password) {
//         try {
//             MessageDigest digester = MessageDigest.getInstance("MD5");
//             int length = (keyLength + 15) / 16 * 16;
//             byte[] passwordBytes = password.getBytes("UTF-8");
//             byte[] temp = digester.digest(passwordBytes);
//             byte[] key = Arrays.copyOf(temp, length);
//             for (int i = 1; i < length / 16; i++) {
//                 temp = Arrays.copyOf(temp, 16 + passwordBytes.length);
//                 System.arraycopy(passwordBytes, 0, temp, 16, passwordBytes.length);
//                 System.arraycopy(digester.digest(temp), 0, key, i * 16, 16);
//             }
//             return Arrays.copyOf(key, keyLength);
//         } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
//             e.printStackTrace();
//         }
//         return new byte[keyLength];
//     }
// 
//     public static byte[] getCompressedBytes(int value) {
//         byte[] bytes = new byte[4];
//         bytes[0] = (byte) ((value >> 24) & 0xff);
//         bytes[1] = (byte) ((value >> 16) & 0xff);
//         bytes[2] = (byte) ((value >> 8) & 0xff);
//         bytes[3] = (byte) (value & 0xff);
//         if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0)
//             return Arrays.copyOfRange(bytes, 3, 4);
//         if (bytes[0] == 0 && bytes[1] == 0)
//             return Arrays.copyOfRange(bytes, 2, 4);
//         if (bytes[0] == 0)
//             return Arrays.copyOfRange(bytes, 1, 4);
//         return bytes;
//     }
// 
//     public static byte[] getBytes(int length, int value) {
//         if (length < 1 || length > 4)
//             throw new RuntimeException("bad length");
//         byte[] bytes = new byte[4];
//         bytes[0] = (byte) ((value >> 24) & 0xff);
//         bytes[1] = (byte) ((value >> 16) & 0xff);
//         bytes[2] = (byte) ((value >> 8) & 0xff);
//         bytes[3] = (byte) (value & 0xff);
//         if (length == 4)
//             return bytes;
//         else
//             return Arrays.copyOfRange(bytes, 4 - length, 4);
//     }
// 
//     public static byte[] getBytes(int value) {
//         return getBytes(4, value);
//     }
// 
//     public static int toBigEndianInteger(byte[] bytes) {
//         byte[] value = new byte[4];
//         System.arraycopy(bytes, 0, value, 4 - bytes.length, bytes.length);
//         return (value[0] & 0xff) << 24 | (value[1] & 0xff) << 16
//                 | (value[2] & 0xff) << 8 | (value[3] & 0xff);
//     }
// 
//     public int getRandomIdentifier(int length) {
//         int base = (int) Math.pow(10, length - 1);
//         return randomizer.nextInt(base * 9) + base;
//     }
// }


// Path: src/main/java/cc/agentx/wrapper/FrameWrapper.java
import cc.agentx.util.KeyHelper;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

            reservedHeaderLength = 4;
    }

    /**
     * frameHandler will be invoked before the frame re-concat into
     * data stream, see unwrap().
     */
    public FrameWrapper(int fixedFrameLength, Wrapper frameHandler) {
        this(fixedFrameLength);
        this.frameHandler = frameHandler;
    }

    /*
     * data will be wrapped into several frames, but marked as a whole
     * chunk (we call it a data-package)
     */
    @Override
    public byte[] wrap(final byte[] bytes) {
        byte[] payload;
        if (frameHandler == null) {
            payload = bytes;
        } else {
            payload = frameHandler.wrap(bytes);
        }
        int i, nof = (payload.length / frameLength) + (payload.length % frameLength == 0 ? 0 : 1);
        for (i = 0; i < nof - 1; i++) {
            wrapBuffer.write(1);
            wrapBuffer.write(payload, i * frameLength, frameLength);
        }
        wrapBuffer.write(0);
        wrapBuffer.write(KeyHelper.getBytes(reservedHeaderLength, payload.length - i * frameLength), 0, reservedHeaderLength);