Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-08-2009, 07:29 PM
Member
 
Join Date: Oct 2008
Posts: 36
Rep Power: 0
arnab321 is on a distinguished road
Wink Which InputStream takes the least memory?
i need to buffer big files like those of 10-15 mb. What should i use to prevent OutOfMemoryErrors? InputStream, ByteArrayInputStream or DataInputStream?

Also, is the difference between memory consumed by an instance of InputStream and a ByteArrayInputStream carrying the same buffer data, too large?

well, its gonna run on a mobile device, but i just posted this here since very less no. of ppl visit the j2me forum.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-08-2009, 08:19 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
Default
Don't do it that way.

Read a chunk, do something, read the next chunk.

Don't worry about memory, that is not your job.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-08-2009, 08:32 PM
Senior Member
 
Join Date: Nov 2008
Posts: 265
Rep Power: 1
neilcoffey is on a distinguished road
Default
And if you really want to cache the entire file in memory, keep the contents in a byte array unless you've a strong reason to do otherwise (remembering you can wrap a ByteArrayInputStream around the array at any moment).
__________________
Neil Coffey

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-08-2009, 09:24 PM
Member
 
Join Date: Oct 2008
Posts: 36
Rep Power: 0
arnab321 is on a distinguished road
Default
reading by chunks...u mean....reading from an inputstream or something else? if i open an a StreamCommection, i have to open an InputStream to read the data. and the inputstream will become larger as it fetches more data.


and well, i wanted to use ByteArrayInputStream, since u can navigate to any part of the file without an IOException. but i was asking how that is different from InputStream in terms od memmory usage ?

Last edited by arnab321; 01-08-2009 at 09:28 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-09-2009, 03:21 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
Default
Originally Posted by arnab321 View Post
and well, i wanted to use ByteArrayInputStream, since u can navigate to any part of the file without an IOException. but i was asking how that is different from InputStream in terms od memmory usage
I'm confused. ByteArrayInputStream is a pseudostream for reading from memory. I thought you wanted to read a real file.

To read it in chunks. allocate a buffer, say 4096 bytes, or 50,000 bytes,
do {
read a buffer;
do something;
} while ( not done)
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-09-2009, 07:34 PM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 798
Rep Power: 1
Steve11235 is on a distinguished road
Default
The purpose of streams is to allow you to read in data a chunk at a time. You can tell the stream how big to make its buffer; once it is created, it doesn't get larger.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-12-2009, 05:23 PM
Member
 
Join Date: Oct 2008
Posts: 36
Rep Power: 0
arnab321 is on a distinguished road
Default
Originally Posted by Steve11235 View Post
The purpose of streams is to allow you to read in data a chunk at a time. You can tell the stream how big to make its buffer; once it is created, it doesn't get larger.
u mean the ByteArrayInputStream ? i just read the javadoc of it. even if u specify an initial buffer size, it will get larger if data doesnt fit into it. if u initialize ByteArrayInputStream b= new ByteArrayInputStream (555);
and u r downloading a 1 mb song into it, it will definitely fit into it, but the buffer size will automatically increase.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-12-2009, 05:56 PM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 798
Rep Power: 1
Steve11235 is on a distinguished road
Default
All streams have a source, typically a file or a socket connection. In your song example, the song could come from an MP3 file on your hard drive or from a download service via the Internet. These streams will read in data to a buffer until it is full, then wait for you to read the contents. Once you read, the stream will put more data in the buffer.

ByteArrayInputStream is a *special* in-memory stream, where the input to the stream is a byte array that was already created. In your song example, you would have already have read the bytes into memory by some other means.

From what you are saying, I'm guessing you want to read a song from a file on your disk. In that case, use a FileInputStream wrapped by a BufferedInputStream. Then use the read(byte[] buffer) method to put bytes into the buffer (which you created previously).
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-13-2009, 03:37 PM
Member
 
Join Date: Oct 2008
Posts: 36
Rep Power: 0
arnab321 is on a distinguished road
Default
u had got it right but then denied. its a song via HTTPconnection.....well, what the actual problem is is that i want to enable the user to "seek" to any part of the downloaded portion of the song. if i put the entire stream into a byte array and to make it worse, do calculations on it, surely, it would crash, throwing OutOfMemoryError. what im currently doing is putting data into a ByteArrayOutputStream and then converting it into InputStream chunks of 8 seconds(its size depends on the bit rate) and playing them one by one, and also setting the played chunks to null, and then running garbage collector.

as im playing only a part of the entire stream at a time, it is possible seek within the chunk

ya, i have to take so much precautions of the deadly OutOfMemoryError on my pre fp device-nokia e50.


as i had already stated, do u have any suggestion for that "seek" function?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-13-2009, 05:45 PM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 798
Rep Power: 1
Steve11235 is on a distinguished road
Default
Quote:
u had got it right but then denied.
I think I guessed pretty well, given what you said! If you say what you want to do up front, you will get better answers...

A ByteArrayOutputStream will still store the entire contents in memory, as that is also a special in-memory stream.

Your HTTP connection is giving you an input stream; the question is, what to do with all the data as you read it. You could use ByteArrayOutputStream for convenience, if you want to keep the data in memory. It will just keep expanding the array until you have read everything.

Or you could write all the data to a File in chunks, close the output stream, and then open an input stream to the File. You might look at AudioInputStream, which seems to be designed for what your are doing.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do I increase memory allocated to code cache (Non Heap Memory) manibhat Advanced Java 2 08-21-2008 07:33 PM
Creating an InputStream c26354 New To Java 9 07-17-2008 03:24 AM
It takes very long time..... iresha Advanced Java 6 05-11-2008 02:31 AM
Converting InputStream to OutputStream Java Tip Java Tips 1 01-11-2008 10:13 PM
Reading bytes from InputStream Java Tip Java Tips 0 11-25-2007 07:51 PM


All times are GMT +2. The time now is 03:44 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org