Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOUtils.toByteArray() OutOfMemoryError

i try to compress large video file with more than 100Mb in size.

public static void compress(File input, File output) throws    IOException {
        InputStream fis = new FileInputStream(input);
        byte[] bFile = IOUtils.toByteArray(fis);
        FileOutputStream fos = new FileOutputStream(output);
        GZIPOutputStream gzipStream = new GZIPOutputStream(fos);
        try {
            gzipStream.write(bFile);
            // IOUtils.copy(fis, gzipStream);
        } finally {
            gzipStream.close();
            fis.close();
            fos.close();
        }           
    }

every time i got outofmemoryerror.

like image 212
user3056448 Avatar asked Jul 13 '26 10:07

user3056448


1 Answers

You should copy the data progressively and you won't run out of memory.

public static void compress(File input, File output) throws IOException {
    try(InputStream in = new FileInputStream(input);
        OutputStream out = new GZIPOutputStream(new FileOutputStream(output))) {
        byte[] bytes = new byte[4096];
        for(int len; (len = in.read(bytes)) > 0; )
            out.write(bytes, 0, len);
    }           
}

This will use about 4 KB as a buffer at once, regardless of the size of the file. (I suspect the GZIP uses about the same to do it's work)

like image 140
Peter Lawrey Avatar answered Jul 15 '26 00:07

Peter Lawrey