修改主线程 写道
public static void main(String[] args) throws InterruptedException {
File input = new File("D:\\test\\demo01.html");
ReturnDigestThread thread = new ReturnDigestThread(input);
thread.start();
// 输出
StringBuffer result = new StringBuffer(input.toString());
result.append(": ");
// 进行轮询
int count = 0;
while (true) {
System.out.println(count++);
// 如果摘要为空,则进行获取
byte[] digest = thread.getDigest();
if (digest != null) {
for (int i = 0; i < digest.length; i++) {
result.append(digest[i] + " ");
}
System.out.println(result.toString());
// 然后退出循环
break;
}
}
}
结果 写道
...
659
660
661
662
663
664
665
666
667
D:\test\demo01.html: 84 56 116 -95 -96 -119 -123 -19 117 -125 22 48 53 13 -111 125 106 -66 44 69
子线程 写道
package network;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class CallbackDigestThread extends Thread {
private File input;
public CallbackDigestThread(File input) {
this.input = input;
}
public void run() {
try {
FileInputStream in = new FileInputStream(this.input);
// 消息摘要
MessageDigest sha = MessageDigest.getInstance("SHA");
DigestInputStream din = new DigestInputStream(in, sha);
// 要完成消息摘要计算,先要调用此摘要输入流的一个 read 方法,之后在关联的消息摘要上调用一个 digest 方法。
while ((din.read()) != -1);
din.close();
// 回调主线程方法进行输出结果
CallbackDigestUserInterface.reciveDigest(sha.digest(), this.input.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
主线程 写道
package network;
import java.io.File;
public class CallbackDigestUserInterface {
public static void main(String[] args) throws InterruptedException {
File input = new File("D:\\test\\demo01.html");
Thread thread = new CallbackDigestThread(input);
thread.start();
}
/**
* 通过提供该方法到子线程,子线程在得到digest后,回调该方法输出.
*
* @param digest
* @param name
*/
public static void reciveDigest(byte[] digest, String name) {
// 输出
StringBuffer result = new StringBuffer(name);
result.append(": ");
for (int i = 0; i < digest.length; i++) {
result.append(digest[i] + " ");
}
System.out.println(result.toString());
}
}
子线程 写道
package network;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class InstanceCallbackDigestThread extends Thread {
private File input;
private InstanceCallbackDigestUserInterface callback;
// 通过构造方法,我们将实例对象传递过来
public InstanceCallbackDigestThread(File input, InstanceCallbackDigestUserInterface callback) {
this.input = input;
this.callback = callback;
}
public void run() {
try {
FileInputStream in = new FileInputStream(this.input);
// 消息摘要
MessageDigest sha = MessageDigest.getInstance("SHA");
DigestInputStream din = new DigestInputStream(in, sha);
// 要完成消息摘要计算,先要调用此摘要输入流的一个 read 方法,之后在关联的消息摘要上调用一个 digest 方法。
while ((din.read()) != -1);
din.close();
// 回调主线程方法进行输出结果
主类 写道
package network;
import java.io.File;
public class InstanceCallbackDigestUserInterface {
public static void main(String[] args) throws InterruptedException {
File input = new File("D:\\test\\demo01.html");
InstanceCallbackDigestUserInterface callback = new InstanceCallbackDigestUserInterface();
// 将实例对象传递到子线程中
Thread thread = new InstanceCallbackDigestThread(input, callback);
thread.start();
}
/**
* 通过提供该方法到子线程,子线程在得到digest后,回调该方法输出.
*
* @param digest
* @param name
*/
public void reciveDigest(byte[] digest, String name) {
// 输出
StringBuffer result = new StringBuffer(name);
result.append(": ");
for (int i = 0; i < digest.length; i++) {
result.append(digest[i] + " ");
}
System.out.println(result.toString());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
listener 写道
package network;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class ListCallbackDigestRunnable implements Runnable {
private File input;
private DigestListener listener;
public ListCallbackDigestRunnable(File input) {
this.input = input;
}
private void sendDigest(byte[] digest) {
listener.digestCalculdated(digest);
}
public void run() {
try {
FileInputStream in = new FileInputStream(this.input);
// 消息摘要
MessageDigest sha = MessageDigest.getInstance("SHA");
DigestInputStream din = new DigestInputStream(in, sha);
// 要完成消息摘要计算,先要调用此摘要输入流的一个 read 方法,之后在关联的消息摘要上调用一个 digest 方法。
// read方法会因为文件的大小而阻塞
while ((din.read()) != -1)
;
din.close();
byte[] digest = sha.digest();
// 当前线程获取到当前文件的digest后,会通过listener将摘要信息通过主线程的接口方法进行输出.
this.sendDigest(digest);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public DigestListener getListener() {
return listener;
}
public void setListener(DigestListener listener) {
this.listener = listener;
}
}
主程序 写道
package network;
import java.io.File;
public class ListCallbackDigestUserInterface implements DigestListener {
private File input;
public ListCallbackDigestUserInterface(File input) {
this.input = input;
}
/**
* 启动不同的线程,对消息摘要进行处理
*/
private void digestCalculated() {
ListCallbackDigestRunnable cb = new ListCallbackDigestRunnable(input);
// 为当前线程设置自己的listener
cb.setListener(this);
Thread t = new Thread(cb);
t.start();
}
public static void main(String[] args) throws InterruptedException {
// 进行三个文件的摘要信息输出
String[] filenames = { "D:\\test\\demo01 - 副本.html", "D:\\test\\demo01 - 副本 (2).html",
"D:\\test\\demo01.html" };
for (String filename : filenames) {
File input = new File(filename);
// 创建当前对象实例
ListCallbackDigestUserInterface d = new ListCallbackDigestUserInterface(input);
d.digestCalculated();
}
}
@Override
public void digestCalculdated(byte[] digest) {
// 输出
StringBuffer result = new StringBuffer(input.toString());
result.append(": ");
for (int i = 0; i < digest.length; i++) {
result.append(digest[i] + " ");
}
System.out.println(result.toString());
}
}
接口 写道
package network;
public interface DigestListener {
/**
* 对摘要进行处理.
*
* @param digest
*/
public void digestCalculdated(byte [] digest);
}