Results 1 to 10 of 10
  1. #1
    arnab321 is offline Member
    Join Date
    Oct 2008
    Posts
    40
    Rep Power
    0

    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.

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    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.

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    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).

  4. #4
    arnab321 is offline Member
    Join Date
    Oct 2008
    Posts
    40
    Rep Power
    0

    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.

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Quote 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)

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    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.

  7. #7
    arnab321 is offline Member
    Join Date
    Oct 2008
    Posts
    40
    Rep Power
    0

    Default

    Quote 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.

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    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).

  9. #9
    arnab321 is offline Member
    Join Date
    Oct 2008
    Posts
    40
    Rep Power
    0

    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?

  10. #10
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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.

Similar Threads

  1. Creating an InputStream
    By c26354 in forum New To Java
    Replies: 10
    Last Post: 07-12-2011, 04:48 AM
  2. Replies: 2
    Last Post: 08-21-2008, 07:33 PM
  3. It takes very long time.....
    By iresha in forum Advanced Java
    Replies: 6
    Last Post: 05-11-2008, 02:31 AM
  4. Converting InputStream to OutputStream
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-11-2008, 10:13 PM
  5. Reading bytes from InputStream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-25-2007, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •