1 packagecom.jjh.common;2
3
4 importjava.io.BufferedInputStream;5 importjava.io.ByteArrayOutputStream;6 importjava.io.IOException;7 importjava.io.InputStream;8 importjava.net.URL;9 importjava.net.URLDecoder;10 importjava.nio.file.FileSystem;11 importjava.nio.file.FileVisitResult;12 importjava.nio.file.Path;13 importjava.nio.file.Paths;14 importjava.nio.file.SimpleFileVisitor;15 importjava.nio.file.attribute.BasicFileAttributes;16 importjava.nio.file.spi.FileSystemProvider;17 importjava.util.ArrayList;18 importjava.util.HashMap;19 importjava.util.List;20 importjava.util.Objects;21
22 /**
23 *@authorAdministrator24 *25 */
26 public final classFiles {27 /**
28 *29 */
30 privateFiles() {31 //TODO Auto-generated constructor stub
32 }33
34 static public byte[] read(InputStream in) throwsIOException35 {36 byte[] temp=new byte[4096];37 ByteArrayOutputStream buffer=newByteArrayOutputStream();38 BufferedInputStream bin=newBufferedInputStream(in);39 for(;;)40 {41 int len=bin.read(temp);42 if(len>-1)43 buffer.write(temp, 0, len);44 else
45 break;46 }47 returnbuffer.toByteArray();48 }49
50
51 static public String[] scanPackage(String packageName) throwsException {52 packageName=packageName.replaceAll("\\.","/");53 URL url =Thread.currentThread().getContextClassLoader().getResource(packageName);54 url=new URL(URLDecoder.decode(url.toString(), "UTF-8"));55 FileSystemProvider provider =null;56 if(url.getProtocol().equals("jar"))57 {58 provider =getZipFSProvider();59 if (provider != null) {60 try (FileSystem fs = provider.newFileSystem(Paths.get(url.getPath().replaceFirst("file:/", "").replaceFirst("!.*", "")),new HashMap<>())) {61 return walkFileTree(fs.getPath(packageName),null).toArray(new String[0]);62 } catch(Exception e) {63 throwe;64 }65 }66 }67 else if(url.getProtocol().equals("file"))68 {69 int end=url.getPath().lastIndexOf(packageName);70 String basePath=url.getPath().substring(1, end);71 return walkFileTree(Paths.get(url.getPath().replaceFirst("/","")),Paths.get(basePath)).toArray(new String[0]);72 }73 return null;74 }75
76 private static List walkFileTree(Path path,Path basePath) throwsIOException{77 //TODO Auto-generated method stub
78 final List result=new ArrayList<>();79 java.nio.file.Files.walkFileTree(path, new SimpleFileVisitor(){80 private String packageName=Objects.isNull(basePath)?"":basePath.toString();81 /*(non-Javadoc)82 * @see java.nio.file.SimpleFileVisitor#visitFile(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)83 */
84 @Override85 public FileVisitResult visitFile(Path arg0, BasicFileAttributes arg1) throwsIOException {86 //TODO Auto-generated method stub
87 if(arg0.toString().endsWith(".class"))88 {89 result.add(arg0.toString().replace(packageName, "").substring(1).replace("\\", "/").replace(".class", "").replace("/", "."));90 }91 returnFileVisitResult.CONTINUE;92 }93
94 /*(non-Javadoc)95 * @see java.nio.file.SimpleFileVisitor#preVisitDirectory(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)96 */
97 @Override98 public FileVisitResult preVisitDirectory(Path arg0, BasicFileAttributes arg1) throwsIOException {99 //TODO Auto-generated method stub100 //packageName=basePath==null?arg0.toString().substring(1, arg0.toString().length()-1).replace("/", "."):basePath.relativize(arg0).toString().replace("\\", ".");
101 returnFileVisitResult.CONTINUE;102 }103
104 });105 returnresult;106 }107
108 static publicFileSystemProvider getZipFSProvider() {109 for(FileSystemProvider provider : FileSystemProvider.installedProviders()) {110 if ("jar".equals(provider.getScheme()))111 returnprovider;112 }113 return null;114 }115
116 public static void main(String[] args) throwsException {117 //C:\Program Files\Apache Software Foundation\Tomcat 8.0\wtpwebapps\j2ee\WEB-INF\hawk.xml118 //System.out.println(new119 //URL("jar:file:/home/whf/foo.jar!/cn/fh").getPath());
120 for(String c:scanPackage("com.mysql.jdbc"))121 System.out.println(c);122 //System.out.println(Paths.get("file://C:/Users/Administrator/Desktop/pecker/build/classes/com/jjh/common"));
123
124 }125
126 }127