Reputation: 140
I have written a program to get data from the file. Whenever I am trying to find data from small files my program works properly but error occurs when file is large. I tried to increase heap size in eclipse but nothing waorks.Below is my code.
public static String getHiddenDataFromImage(String imagePath)
{
String dataContents = null;
try
{
File file = new File(imagePath);
byte[] fileData = new byte[(int)file.length()];
InputStream inStream = new FileInputStream(file);
inStream.read(fileData);
inStream.close();
String tempFileData = new String(fileData);
String finalData = tempFileData.substring(tempFileData.indexOf(extraStr) + extraStr.length(), tempFileData.length());
byte[] bytefinalData=finalData.getBytes();
byte[] temp = (byte[]) new Base64().decode(bytefinalData);
dataContents = new String(temp);
}
catch (Exception e)
{
e.printStackTrace();
}
return dataContents;
}
public static void main(String[] args){
System.out.println(ImageAnalyzerUtil.getHiddenDataFromImage2("C:/example.m4b"));
}
Output
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at ImageAnalyzerUtil.getHiddenDataFromImage(ImageAnalyzerUtil.java:199)
at ImageAnalyzerUtil.main(ImageAnalyzerUtil.java:332)
line 199 is byte[] fileData = new byte[(int)file.length()];
Upvotes: 2
Views: 1636
Reputation: 57
As suggested by @Maurice and @EJP, avoid reading the whole file at once in the program. But if you have to read the whole file, you can specify runtime parameters to java to give you a larger heap size as follows.
-Xms - Set initial Java heap size -Xmx - Set maximum Java heap size
java -Xms512m -Xmx1024m JavaApp
I am not familiar with Eclipse so I don't know how to increase the heap size from within the IDE. But you can test your program on command line using 'java' programs as mentioned above to see if your program works with larger heap size.
Upvotes: -1
Reputation: 311039
String
as a container for binary data.All you need to do here is seek()
to the required offset, read that many bytes, Base64-decode them, and return them.
Upvotes: 2
Reputation: 32831
It seems that your program is doing a base 64 decoding of a file. It doesn't need to read the whole file in memory at once: there are libraries that can decode an input stream into an output stream.
Upvotes: 1