如何使用PipedInputStream和PipedOutputStream

时间:2018-09-11 08:34:38

标签: java encryption inputstream outputstream

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

0 个答案:

没有答案