added port scanner support

This commit is contained in:
jomu
2014-07-10 10:02:42 +00:00
parent d19fe07b6f
commit dc1a947ad8
20 changed files with 365 additions and 28 deletions

View File

@ -4,18 +4,23 @@
*/
package de.muehlencord.shared.util.file;
import de.muehlencord.shared.util.StringUtil;
import static de.muehlencord.shared.util.StringUtil.getStackTraceString;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.Format;
import java.util.Arrays;
import static java.util.Arrays.asList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import static org.apache.log4j.Logger.getLogger;
@ -25,7 +30,9 @@ import static org.apache.log4j.Logger.getLogger;
*/
public abstract class FileUtil {
/** the logging object */
/**
* the logging object
*/
private static final Logger logger = getLogger(FileUtil.class);
/**
@ -70,7 +77,7 @@ public abstract class FileUtil {
*/
public static File getFileName(final String outputFolder, final Format ft, final int number) {
String folderName = outputFolder;
String fileName = ft.format(number);
String fileName = ft.format(number);
if (!outputFolder.endsWith(File.separator)) {
folderName += File.separator;
}
@ -153,4 +160,31 @@ public abstract class FileUtil {
File location = new File(path);
return location.exists();
}
public static String md5Sum(String fileName) throws IOException {
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException ex) {
throw new IOException("Cannot get MD5 MessageDigest instance");
}
try (InputStream is = Files.newInputStream(Paths.get(fileName))) {
DigestInputStream dis = new DigestInputStream(is, md);
int nread;
byte[] buffer = new byte[4096];
while ((nread = dis.read(buffer)) != -1) {
md.update(buffer);
}
}
byte[] digest = md.digest();
//convert the byte to hex format method 1
StringBuilder sb = new StringBuilder();
for (int i = 0; i < digest.length; i++) {
sb.append(Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
}

View File

@ -6,10 +6,13 @@ package de.muehlencord.shared.util.file;
import static de.muehlencord.shared.util.file.FileUtil.getFilesFromDirecotry;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
/**
@ -24,6 +27,7 @@ public class FileUtilTest {
* @throws URISyntaxException if the testfile specification is wrong
*/
@Test
@Ignore
public void testGetFilesFromDirecotry() throws FileHandlingException, URISyntaxException {
System.out.println("testGetFilesFromDirectory");
@ -35,7 +39,16 @@ public class FileUtilTest {
while (it.hasNext()) {
System.out.println(it.next());
}
}
@Test
public void testMd5Sum() throws IOException, URISyntaxException {
System.out.println("testGetFilesFromDirectory");
URL url = getClass().getResource("testfile.txt");
File testFile = new File(url.getFile());
String fileName = testFile.toString();
fileName = "h:/temp/01750229371_DVD001.iso";
System.out.println (FileUtil.md5Sum(fileName));
}
}