java.lang.OutOfMemoryError (new thread)
Hello friends,
I need an urgent assistance with a project that am working on presently at the moment.
I am trying to read and write data in byte arrays from a file with the use of FileInputStream and then write it to another file with the use of FileOutputStream.
My Code works very fine when handling files lesser than 30MB. When i intend to do such with files above 30MB i get a java.lang.OutOfMemoryError exception and the program stops reading and writing.
In order to be able to avoid this error, i then modified the amount of byte that is being read through FileInputStream to 1MB at a time, so that it keeps reading and writing 1MB of data. But i still get the java.lang.OutOfMemoryError exception and the process stops.
I really need to have this project completed asap. Please help rectify the problem. Below is my code:
I anticipate your prompt response guys.
publicclass Encodeextends Thread
{
File f;
File dir;
privateint MAX_ARRAY_COPY=1048576;
int fileSize;
int startPoint=1;
int startReadPoint=1;
String newFile;
int execCount=0;
public Encode(File ft)
{
f=ft;
dir=new File("SecuredMedia/"+f.getName().substring(0,f.getName().lastIndexOf(".")));
dir.mkdirs();
newFile=f.getName();
newFile=dir+"/"+newFile;
}
public File getEncodedDirectory()
{
return dir;
}
publicvoid run()
{
encodeFile();
}
publicvoid encodeFile()
{
try
{
FileInputStream fis=new FileInputStream(f);
fileSize=fis.available();
execCount++;
System.out.println("Execution Count: "+execCount);
SecureFile.encodeBtn.setEnabled(false);
SecureFile.status2.setText("Encoding");
byte[] red=newbyte[MAX_ARRAY_COPY];
byte[] reversed=newbyte[MAX_ARRAY_COPY];
System.out.println("MAX_ARRAY_COPY : "+MAX_ARRAY_COPY);
System.out.println("red : "+red.length);
int b=fis.read(red,startReadPoint,MAX_ARRAY_COPY);
fis.close();
System.out.println("Data Read : "+b);
int rcount=0;
for(int y=MAX_ARRAY_COPY-1;y>-1;y--)
{
reversed[rcount]=red[y];
rcount++;
}
FileOutputStream fos=new FileOutputStream(newFile);
fos.write(reversed,startPoint,MAX_ARRAY_COPY);
fos.close();
int tempFileSize=new FileInputStream(newFile).available();
System.out.println("Written File Size : "+tempFileSize);
if(tempFileSize < fileSize)
{
if((fileSize - tempFileSize) < MAX_ARRAY_COPY)
{
MAX_ARRAY_COPY=fileSize - tempFileSize;
}
startReadPoint=tempFileSize;
startPoint=tempFileSize;
System.out.println("Finished Logical Execution: "+execCount+"\n");
encodeFile();
}
else
{
}
SecureFile.status2.setText("Completed");
SecureFile.encodeBtn.setEnabled(true);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}

