importorg.jacoco.core.data.ExecutionDataWriter;importorg.jacoco.core.runtime.RemoteControlReader;importorg.jacoco.core.runtime.RemoteControlWriter;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.net.InetAddress;importjava.net.Socket;importjava.util.List;/*** This example connects to a coverage agent that run in output mode
* tcpserver
and requests execution data. The collected data is
* dumped to a local file.*/
public classExecutionDataClient {public String downLoadDump(String path, ListipPort) {int flag = 0;
String getDirName=getDirNum(path);for(String getIPPost : ipPort){
String dirName= getDirName + "/" + getIPPost.replace(":", "_") + "_jacoco.exec";
String ip= getIPPost.split(":")[0];int port = Integer.parseInt(getIPPost.split(":")[1]);try{final FileOutputStream localFile = newFileOutputStream(dirName);final ExecutionDataWriter localWriter = newExecutionDataWriter(
localFile);//Open a socket to the coverage agent:
final Socket socket = newSocket(InetAddress.getByName(ip), port);final RemoteControlWriter writer = newRemoteControlWriter(
socket.getOutputStream());final RemoteControlReader reader = newRemoteControlReader(
socket.getInputStream());
reader.setSessionInfoVisitor(localWriter);
reader.setExecutionDataVisitor(localWriter);//Send a dump command and read the response:
writer.visitDumpCommand(true, false);if (!reader.read()) {throw new IOException("Socket closed unexpectedly.");
}
socket.close();
localFile.close();
System.out.println("Download file success....");
System.out.println("File path is : " +dirName);
flag++;
}catch(IOException ioe) {
ioe.printStackTrace();
}
}if (flag != 0) {returngetDirName;
}else{return "path";
}
}/***
*@parampath
*@return
*/
privateString getDirNum(String path){
File file= null;
String newPath= "";for (int i = 0 ; ; i++) {
newPath= path + "/" +i;
file= newFile(newPath);
System.out.println("new path = "+newPath);if (!file.isDirectory()) {
System.out.println(newPath);
file.mkdirs();break;
}
}returnnewPath;
}
}