Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
i am getting error while reading stream. Stream also contain images along with string data.My code is,
static String convertStreamToString(java.io.InputStream is) {
Reader reader = new InputStreamReader(is);
BufferedReader r = new BufferedReader(reader);
StringBuilder total = new StringBuilder();
String line = null;
try {
while ((line = r.readLine()) != null) {
total.append(line);
} catch (IOException e) {
e.printStackTrace();
System.out.println(total);
return line;
In while loop it results in consol as its heap memory is increasing and at last give error "out of memory"
Hopes for your suggestion
–
You are simply reading your whole file and storing it into the StringBuilder. Your file just too big to fit in RAM, and just results in an OutOfMemory error when the JVM is unable to allocate any more space to your StringBuilder.
Either find a way not to store the whole file in memory or use a smaller file (the former solution clearly being the better).
Also, note that you are not closing your buffered reader after use. The correct way to do this is to declare your resources using the try-with resources, for example :
try(Reader reader = new InputStreamReader(is);
BufferedReader r = new BufferedReader(reader)){
//your code here
You do need to close your stream, as others have noted. But you have a couple of other things to think about:
What do you mean when you say that Stream also contain images along with string data? You're not doing anything to deal with this. You're reading it all as String
s. If the file really does contain images and strings, then it's quite likely that the images are much bigger than the strings; and if you just want the strings then you need to find a way to filter out the images. That will probably solve your out-of-memory problem, and also prevent you from ending up with nonsense in your output. Unfortunately, you haven't given us enough information to help with the detail of this: it will depend on the format of the file/stream.
If the file is huge, and contains more string data than you can store in memory, then no amount of careful closing of streams and the like will help. You'll need to process the file as you go, rather than storing it in RAM. But how you do this will rather depend on what you're trying to do with the data.
I'd start by attacking the first problem if I were you. At the moment, it sounds as though even if you solve the memory problem, you'll still end up with nonsensical output because the images will get decoded as String
s.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.