有许多将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);