Proto { public static byte[] build_fixed_int(int size, long value) { byte[] packet = new byte[size]; if (size == 8) { packet[0] = (byte) ((value >> 0) & 0xFF); packet[1] = (byte) ((value >> 8) & 0xFF); packet[2] = (byte) ((value >> 16) & 0xFF); packet[3] = (byte) ((value >> 24) & 0xFF); packet[4] = (byte) ((value >> 32) & 0xFF); packet[5] = (byte) ((value >> 40) & 0xFF); packet[6] = (byte) ((value >> 48) & 0xFF); packet[7] = (byte) ((value >> 56) & 0xFF); } else if (size == 6) { packet[0] = (byte) ((value >> 0) & 0xFF); packet[1] = (byte) ((value >> 8) & 0xFF); packet[2] = (byte) ((value >> 16) & 0xFF); packet[3] = (byte) ((value >> 24) & 0xFF); packet[4] = (byte) ((value >> 32) & 0xFF); packet[5] = (byte) ((value >> 40) & 0xFF); } else if (size == 4) { packet[0] = (byte) ((value >> 0) & 0xFF); packet[1] = (byte) ((value >> 8) & 0xFF); packet[2] = (byte) ((value >> 16) & 0xFF); packet[3] = (byte) ((value >> 24) & 0xFF); } else if (size == 3) { packet[0] = (byte) ((value >> 0) & 0xFF); packet[1] = (byte) ((value >> 8) & 0xFF); packet[2] = (byte) ((value >> 16) & 0xFF); } else if (size == 2) { packet[0] = (byte) ((value >> 0) & 0xFF); packet[1] = (byte) ((value >> 8) & 0xFF); } else if (size == 1) { packet[0] = (byte) ((value >> 0) & 0xFF); } else { Logger.getLogger("MySQL.Proto").fatal("Encoding int["+size+"] "+value+" failed!"); return null; } return packet; } Proto(byte[] packet);  Proto(byte[] packet, int offset); boolean has_remaining_data(); static byte[] build_fixed_int(int size, long value); static byte[] build_lenenc_int(long value); static byte[] build_lenenc_str(String str); static byte[] build_lenenc_str(String str, boolean base64); static byte[] build_null_str(String str); static byte[] build_null_str(String str, boolean base64); static byte[] build_fixed_str(long size, String str); static byte[] build_fixed_str(long size, String str, boolean base64); static byte[] build_fixed_str(int size, String str); static byte[] build_fixed_str(int size, String str, boolean base64); static byte[] build_eop_str(String str); static byte[] build_eop_str(String str, boolean base64); static byte[] build_filler(int len); static byte[] build_filler(int len, int filler_value); static byte[] build_filler(int len, byte filler_value); static byte[] build_byte(byte value); static char int2char(byte i); static byte char2int(char i); long get_fixed_int(int size); static long get_fixed_int(byte[] bytes); void get_filler(int size); long get_lenenc_int(); String get_fixed_str(long len); String get_fixed_str(long len, boolean base64); String get_fixed_str(int len); String get_fixed_str(int len, boolean base64); String get_null_str(); String get_null_str(boolean base64); String get_eop_str(); String get_eop_str(boolean base64); String get_lenenc_str(); String get_lenenc_str(boolean base64); static byte[] arraylist_to_array(ArrayList<byte[]> input); static byte[] packet_string_to_bytes(String str); public byte[] packet; public int offset; }
@Test public void test1() { byte[] expected = new byte[] { (byte)0x00 }; assertArrayEquals(expected, Proto.build_fixed_int(1, 0)); }
@Test public void test2() { byte[] expected = new byte[] { (byte)0xFF }; assertArrayEquals(expected, Proto.build_fixed_int(1, 255)); }
@Test public void test3() { byte[] expected = new byte[] { (byte)0x00, (byte)0x00 }; assertArrayEquals(expected, Proto.build_fixed_int(2, 0)); }
@Test public void test4() { byte[] expected = new byte[] { (byte)0xFF, (byte)0xFF }; assertArrayEquals(expected, Proto.build_fixed_int(2, 0xFFFF)); }
@Test public void test5() { byte[] expected = new byte[] { (byte)0x00, (byte)0x00, (byte)0x00 }; assertArrayEquals(expected, Proto.build_fixed_int(3, 0)); }
@Test public void test6() { byte[] expected = new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }; assertArrayEquals(expected, Proto.build_fixed_int(4, 0)); }
@Test public void test7() { byte[] expected = new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, }; assertArrayEquals(expected, Proto.build_fixed_int(8, 0)); }
@Test public void test8() { byte[] expected = new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, }; assertArrayEquals(expected, Proto.build_fixed_int(8, 255)); }