gpt4 book ai didi

java - 关闭 ByteArrayOutputStream 的最佳方法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:35:16 24 4
gpt4 key购买 nike

我需要优化一个使用过多堆内存的应用程序。我在使用 ByteArrayOutputStream 变量后关闭它时遇到问题。我试过使用 close() 来做,但它不起作用。这是代码:

ByteArrayOutputStream zipOutTempStream = new ByteArrayOutputStream();
//arquivo.getZipStream() has the XML received by FTP.
//STreamEtils is the function who transfers the XML to zipOutTempStream
StreamUtils.copiarStream(arquivo.getZipStream(), zipOutTempStream);

//Creating a new XML to write over this.
File arquivo1 = new File("C:/XML.xml");
if (arquivo1.exists()) {
System.out.println("ele existe");
} else {
if (arquivo1.createNewFile()) {
System.out.println("arquivo criado");
} else {
System.out.println("arquivo não criado");
}
}

FileOutputStream arquivo2 = new FileOutputStream(arquivo1);
//Copy the unziped XML to the new xml created.
StreamUtils.copiarStream(StreamUtils .uncompressXmlFromZipStream(new ByteArrayInputStream(zipOutTempStream.toByteArray())), arquivo2);
arquivo.setZipStream(null);
arquivo.setXmlStream(null)
return arquivo;

最佳答案

您不能关闭 ByteArrayOutputStream,因为它是 close()方法记录为

Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

这个输出流由一个数组支持;它不是缓冲流。如果您觉得它使用了太多内存,您应该使用适当的 OutputStream 将字节直接输出到某个端点,例如文件或套接字。

关于java - 关闭 ByteArrayOutputStream 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10095379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com