[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Out of memory



Read in 4k-8k chunks of the file at a time instead of the entire size of
the file.


byte buff[]=new byte[4096];
while(true)
{
   int c=input.read(buff);
   if (c<0)
   { 
    break;
   }
   fileOut.write(buff,0,c);
}

-----Original Message-----
From: Jason R. Kretzer [mailto:jason@OpinionOne.com] 
Sent: Monday, November 18, 2002 1:29 PM
To: 'ajug-members@ajug.org'
Subject: Out of memory


Hello all,

I am using the following code to add a file to a jar.  It works fine for

smaller files, but if the file is over 56MB, it throws and out of memory

exception.  Any ideas on how I can keep this from happening?

thanks,

-Jason


private File jarFile;
private JarOutputStream jarOut;
private FileOutputStream fileOut;
jarFile = jarFile;
fileOut = new FileOutputStream(jarFile);
jarOut = new JarOutputStream(fileOut, man);

JarEntry entry = new JarEntry(name); jarOut.putNextEntry(entry);
FileInputStream input = new FileInputStream(file); byte[] buffer = new
byte[(int) file.length()]; if (file.length() != input.read(buffer, 0,
(int) file.length())) {
            throw new IOException("The amount of bytes read from the
file "
                     + "didn't match the file size for "
                     + file.getName());
}
jarOut.write(buffer, 0, (int) file.length());
input.close();