java.lang.OutOfMemory错误: Java heap space on writing to outputstream

标签 java out-of-memory outputstream

我试图通过传递文件流、结果流和包含有关加密类型信息的密码对象来加密文件。这是我尝试过的:

private void processFile(Cipher cipher,InputStream inputStream, OutputStream outputStream){
   byte[] tempInputBuffer = new byte[1024];
   int len;
   try {
        while ((len = inputStream.read(tempInputBuffer)) != -1) {
            byte[] tempOutputBuffer = cipher.update(tempInputBuffer, 0, len);
            if ( tempOutputBuffer != null ) outputStream.write(tempOutputBuffer);
        }
        byte[] obuf = cipher.doFinal();
        if ( obuf != null ) outputStream.write(obuf);
        }catch (IOException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();    
        }catch (Exception e) {
            e.printStackTrace();        
        }
    }

这是我调用 processFile 的函数

public ByteArrayOutputStream encryptFile( InputStream fileStream, PublicKey publicKey ) throws EmprisException {

        ByteArrayOutputStream encryptedFileStream = null;
        KeyGenerator keyGenerator;
        try {

            //generate AES key
            keyGenerator = KeyGenerator.getInstance(FileUploadDownloadConstants.AES_ALGORITHM);
            keyGenerator.init(128);
            SecretKey secretKey = keyGenerator.generateKey();
            byte[] initializationVector = new byte[16];
            SecureRandom srandom = new SecureRandom();
            srandom.nextBytes(initializationVector);
            IvParameterSpec ivSpec = new IvParameterSpec(initializationVector);

            //encrypting the aes key using rsa public key and adding it to a file
            encryptedFileStream = new ByteArrayOutputStream();
            Cipher cipherRSA = Cipher.getInstance(FileUploadDownloadConstants.RSA_TRANSFORMATION);
            cipherRSA.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] secretKeyBytes = cipherRSA.doFinal(secretKey.getEncoded());
            encryptedFileStream.write(secretKeyBytes);
            encryptedFileStream.write(initializationVector);

            //call processFile to encrypt the file
            Cipher cipherAES = Cipher.getInstance(FileUploadDownloadConstants.AES_TRANSFORMATION);
            cipherAES.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
            processFile(cipherAES, fileStream, encryptedFileStream);
            encryptedFileStream.close();
            logger.logExiting(METHOD_NAME);
            return encryptedFileStream;             
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

这是我得到的堆栈跟踪:

java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3236)
    at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
    at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
    at java.io.OutputStream.write(OutputStream.java:75)

我在 if ( tempOutputBuffer != null ) outputStream.write(tempOutputBuffer); 行中遇到内存不足错误

写入输出流的方式有问题吗? 当我尝试处理更大的文件(例如 15 Mb 左右)时,就会发生这种情况。 非常感谢您的帮助并提前致谢。

最佳答案

您是否尝试过为 JVM 设置最小堆大小?请参阅此相关问题:

What are the Xms and Xmx parameters when starting JVMs?

对于您的示例来说,默认值可能太低。

关于java.lang.OutOfMemory错误: Java heap space on writing to outputstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49984232/

相关文章:

java - 无法在Apache Tomcat上运行JAX-WS Java Web服务

linux - 什么是tomcat内存堆提交?

java - 输入流在循环中崩溃

java - 如何将 .java 包文件放入 Eclipse 项目中

java - 尝试使用 firepropertychange、propertychangesupport 更改值

java - 监控tomcat permgen

java - 写入 jar 文件中的文本文件

java - 在可运行的 jar 中加载和保存资源?

java - 如何获得直线和多边形的交点?

java - 当 OOM 发生时,如何让 dalvik 转储整个应用程序堆内存