Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing large files using BufferedOutputStream

I am trying read and write large files (larger than 100 MBs) using BufferedInputStream & BufferedOutputStream. I am getting Heap Memory issue & OOM exception.
The code looks like :

BufferedInputStream buffIn = new BufferedInputStream(iStream);
/** iStream is the InputStream object  **/

BufferedOutputStream buffOut=new BufferedOutputStream(new FileOutputStream(file));
byte []arr = new byte [1024 * 1024];
int available  = -1;
while((available = buffIn.read(arr)) > 0) {   
    buffOut.write(arr, 0, available); 
}      
buffOut.flush();
buffOut.close();        

My question is when we use the BufferedOutputStreeam is it holding the memory till the full file is written out ?
What is the best way to write large files using BufferedOutputStream?

like image 987
user1229894 Avatar asked Feb 24 '12 04:02

user1229894


People also ask

How do you write BufferedOutputStream?

Create a BufferedOutputStream BufferedOutputStream package first. Once we import the package here is how we can create the output stream. // Creates a FileOutputStream FileOutputStream file = new FileOutputStream(String path); // Creates a BufferedOutputStream BufferedOutputStream buffer = new BufferOutputStream(file);

What is BufferedOutputStream in Java?

BufferedOutputStream(OutputStream out) Creates a new buffered output stream to write data to the specified underlying output stream. BufferedOutputStream(OutputStream out, int size) Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.


1 Answers

there is nothing wrong with the code you have provided. your memory issues must lie elsewhere. the buffered streams have a fixed memory usage limit.

the easiest way to determine what has caused an OOME, of course, is to have the OOME generate a heap dump and then examine that heap dump in a memory profiler.

like image 155
jtahlborn Avatar answered Oct 12 '22 22:10

jtahlborn