java - 如何使用 PipedInputStream 和 PipedOutputStream
问题描述
有很多使用 while 转换OutputStream
为InputStream
.
我怎样才能完成下面缺少的部分?
我是说putDataOnOutputStream(out)
方法。
如果仍然需要使用ByteArrays
,在这种情况下,使用 PipeLines 的内存优势是什么没有意义
我需要完成 convertToInputStream ;
public static InputStream decrypt(String keyCode, String file) throws IOException, InterruptedException {
OutputStream out;
try {
Cipher cipher = Cipher.getInstance("AES/CBC/Pkcs5Padding");
/****** decyption code
**/
out = new BufferedOutputStream(new FileOutputStream("C:decyrptedText.txt"));
for(int readBytes = in.read(buffer); readBytes>-1; readBytes = in.read(buffer)) {
byte[] decrypted = cipher.update(buffer, 0, readBytes);
out.write(decrypted);
}
byte[] decrypted = cipher.doFinal();
out.write(decrypted);
out.flush();
out.close();
in.close();
} catch (Exception e) {
throw new IllegalStateException(e);
}
return convertToInputStream(out); //
}
public static InputStream convertToInputStream(OutputStream outputStream) throws IOException, InterruptedException {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);
new Thread(
new Runnable(){
public void run(){
//put your code that writes data to the outputstream here.
putDataOnOutputStream(outputStream);
}
}
).start();
//data can be read from the pipedInputStream here.
processDataFromInputStream(in);
解决方案
推荐阅读
- javascript - getElementsByTagName() 和其他人没有返回他们应该返回的所有元素?
- html - 无法在其他页面上重定向给 MultiValueDictKeyError Django
- android - 片段内的Textview显示不相关的文本
- angular - PrimeNG饼图随机显示,分辨率变化显示
- python - 防止 Flask OSError: [Errno 98] 地址已在使用中
- python - 给出 json.decoder.JSONDecodeError 的有效 json: Expecting ',' delimiter
- yaml - 如何使用 openshift 中的参数更新 configmap 数据?
- python - 将基于统计信息的新列添加到 DataFrame
- python - 将 python 脚本转换为 Jupyter 并将输出保存为 html
- java - Java 泛型 - 为什么返回超类实例会产生编译错误