在传统Java编程中, 我们可以另用ProcessBuilder类来创建系统进程或者Runtime类来取得JVM的接口对象运行我们指定的系统命令, 并且可以通过读入器来获取系统的out以及err输出.这里有一个简单的例子, 没有用过的同学可以参考一下.
http://wuhongyu.iteye.com/blog/461477
由于前段时间有这样的一个需求, 尽可能快的算出Hadoop系统里的MD码, 我曾经想过另用Java的Digest类来读取文件, 然后工作交给Digest类来完成, 可没想到的是Java的这个类算MD校验码,效率及其的低下,我在XP上算50M的文件, 居然花费8秒钟的时间.直接放弃. 这个Demo我也顺便推荐一下:
http://www.javablogging.com/?s=Java+Digest+SHA1+MD5
后来google该问题的解决方案, 有一个比较有意思的贴子Fast MD5 Implementation in Java, 呵呵, 由于时间关系, 没来的急看,分享一下:
http://www.twmacinta.com/myjava/fast_md5.php
Fast MD5.. 总给我一个不太舒服的感觉, 最后还是决定放弃使用. 由于Hadoop运行于Linux 环境, 回过神来我想到了Linux上的openssl库. 大伙都知道openssl 里其中就有计算数据的安全通信命令. $cat XXX | openssl sha1.
这里有一个问题, 由于hadoop的文件是存在于集群的, 而openssl 是接受本地文件作为参数的, 我可以把文件从HDFS里读出, 然后再构造文件供openssl 计算. 不过这种方式太囧.
我的解决方法是另用Runtime获取运行对象, 直接执行openssl sha1命令, 其实当系统执行该命令的时候会等待输入的. 此时我可以通过我的文件输入流把数据打印到等待输入的输出流上。
// TODO this is a job. not a filedescriptor
protected void handle(FileDescriptor job) throws IOException {
if (job == null || job.path == null) {
log.warn("Invalid job: " + job);
}
// wait for GC.
Configuration conf = new Configuration();
Path path = new Path(job.path);
FileSystem fs = path.getFileSystem(conf);
if (!fs.exists(path) || !fs.isFile(path)) {
log.warn("*HADLEJOB file does not exist for path: " + path);
return;
}
InputStream is = fs.open(path);
// Run a command
String cmd = "openssl sha1";
boolean completed = false;
Process process = null; // Sub process used to execute the command
int exitCode;
process = Runtime.getRuntime().exec(cmd);
BufferedOutputStream os = new BufferedOutputStream(process.getOutputStream(), 4096);
byte buf[] = new byte[4096];
while (true) {
int bytesRead = is.read(buf);
if (bytesRead == -1) {
break;
}
os.write(buf, 0, bytesRead);
}
os.flush();
os.close();
final BufferedReader errReader =
new BufferedReader(new InputStreamReader(process.getErrorStream()));
BufferedReader inReader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
final StringBuffer errMsg = new StringBuffer();
// read error and input streams as this would free up the buffers
// free the error stream buffer
Thread errThread = new Thread(){
public void run() {
try {
String line = errReader.readLine();
while ((line != null) && !isInterrupted()) {
errMsg.append(line);
errMsg.append(System.getProperty("line.separator"));
line = errReader.readLine();
}
} catch (IOException ioe) {
log.warn("Error reading the error stream", ioe);
}
}
};
try {
errThread.start();
} catch (IllegalStateException ise) {
}
try {
String line = inReader.readLine();
if (line == null) {
throw new IOException("Exception a line not the end of stream");
}
job.set_sha1(line.trim());
// clear the input stream buffer
line = inReader.readLine();
while (line != null) {
line = inReader.readLine();
}
// wait for the process to finish and check the exit code
exitCode = process.waitFor();
try {
errThread.join();
} catch (InterruptedException ie) {
log.warn("Interrupted while reading the error stream", ie);
}
completed = true;
if (exitCode != 0) {
throw new IOException(exitCode + errMsg.toString());
}
} catch (InterruptedException ie) {
throw new IOException(ie.toString());
} finally {
// close the input stream
try {
inReader.close();
} catch (IOException e) {
log.warn("Error whilke closing the input stream", e);
}
if (!completed) {
errThread.interrupt();
}
try {
errReader.close();
}catch(IOException ioe){
log.warn("Error while closing the error stream", ioe);
}
process.destroy();
}
}
该执行方法运行于线程池中, 下一个贴子我将记录真正应用中线程池的实现, 提供线程池的大小设置, shrink等实现.
分享到:
相关推荐
linux升级openssl9.3,linux升级openssl9.3,linux升级openssl9.3,linux升级opelinux升级openssl9.3,linux升级openssl9.3,linux升级openssl9.3,linux升级opelinux升级openssl9.3,linux升级openssl9.3,linux...
SSL 过程中,需要使用SSL头文件以及DLL,LIB库文件,因此个人整理这里共享在这里,解决大家自己动手编译SSL的痛苦过程 具体文件如下: libeay32.dll, libeay32.lib libssl32.dll, libssl32.lib 以及 openssl头文件
基于openssl的ftp文件传输系统源码基于openssl的ftp文件传输系统源码基于openssl的ftp文件传输系统源码基于openssl的ftp文件传输系统源码基于openssl的ftp文件传输系统源码基于openssl的ftp文件传输系统源码基于...
java的ssl客户端编程与LINUX的OpenSSL服务器通讯的实现及有关文档 附件中包含本人开发工程中使用过的DEMO程序及制作证书的解本文件
开发项目需要使用1.0.0版本的Openssl的linux上的静态库,使用1.0.2版本Openssl会存在版本问题。开始试着在网上寻找此1.0.0版本已编译出静态库,然而此版本不是源代码,动态库,就是windows编译出的静态库,即使最终...
total commander 8 x64在连接ftps或者https的时候可能会提示“找不到openssl库文件”,把这个下下来解压到tc8 x64的安装目录下就可以解决了。
由Visual Studio 2015编译生成的,包括openssl库生成的静态库版本(32位/64位)、动态库版本(32位/64位); 1、静态库版本生成文件:头文件、libcrypto.lib、libssl.lib 2、动态库版本(32位)生成文件:头文件、...
linuxOpenssl-install安装包,内含一键安装脚本,一键执行自动安装
linux下的openssl安装包,版本是0.9.81
利用openssl和curl库获取https证书
openssl的库文件,方便大家,自己搞太麻烦了。
基于iPhone15.5.sdk版本的OpenSSL库
linux7升级到 openssl3.0.2 openssh8.9.p1 linux7升级到 openssl3.0.2 openssh8.9.p1 linux7升级到 openssl3.0.2 openssh8.9.p1 linux7升级到 openssl3.0.2 openssh8.9.p1 linux7升级到 openssl3.0.2 openssh8.9...
curl需要的openssl库文件,支持https,希望对大家有帮助。
openssl库文件libcrypto.a、libssl.a静态库文件,在编译iOS 版本中的ijkplayer中使用过,值得下载!
博客请参考:https://blog.csdn.net/ManagerUser/article/details/124878693#comments_22867458
OPENSSL 静态库 libcrypto.lib 和 libssl.lib VS2008 x64/x86 Release/Debug 版本 VS2010 x64/x86 Release/Debug 版本 VS2012 x64/x86 Release/Debug 版本 VS2015 x64/x86 Release/Debug 版本 VS2019 x64/x86 ...
对sha256在openssl库中调用和组装生成可以执行的基于openssl库的sha256模块,可供之后的生日攻击和长度扩展攻击等使用该模块。 可以运行test.cpp对该模块的散列加密功能进行简单测试。 运行指导 将源码clone到...
里面包含openssl库和编译的方法,在winxp上是通过了的。库里包含了丰富的加密签名算法