DanmakuPacket { public byte[] generate() { byte[] bodyBytes = body.getBytes(UTF8); calculateLength(bodyBytes); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(length); DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream); writeBigEndian(length, dataOutputStream); writeBigEndian(headerLength, dataOutputStream); writeBigEndian(protocolVersion, dataOutputStream); writeBigEndian(action.getID(), dataOutputStream); writeBigEndian(param, dataOutputStream); if (bodyBytes.length > 0) { byteArrayOutputStream.write(bodyBytes, 0, bodyBytes.length); } return byteArrayOutputStream.toByteArray(); } DanmakuPacket(Action action);  DanmakuPacket(Action action, String body); byte[] generate();  }
@Test public void generate() throws Exception { DanmakuPacket packet = new DanmakuPacket(DanmakuPacket.Action.HEARTBEAT, "sss"); byte[] buffer = packet.generate(); assertArrayEquals(new byte[]{0, 0, 0, 19, 0, 16, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 115, 115, 115}, buffer); }