Results 1 to 8 of 8
  1. #1
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default [SOLVED] InputStream into ByteBuffer

    I have an InputStream. I want to put the entire contents of the stream into a ByteBuffer. Is there a better way than this? At least there must be a nicer way to have a growable array.

    Java Code:
    InputStream in;
    int BUFSIZE = 1024;
    
    Vector<Byte> byteV = new Vector<Byte>();
    byte[] tmp1 = new byte[BUFSIZE];
    while (true) {
        int r = in.read(tmp1, 0, BUFSIZE);
        if (r == -1) break;
        for (int i=0; i<r; i++) {
            byteV.add(tmp1[i]);
        }
    }
    byte[] tmp2 = new byte[byteV.size()];
    for (int i=0; i<byteV.size(); i++) {
        tmp2[i] = byteV.elementAt(i);
    }
    ByteBuffer buf = ByteBuffer.wrap(tmp2);
    Last edited by OrangeDog; 04-28-2009 at 04:32 AM. Reason: corrections
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

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

    Default

    Well, I would start by wrapping the byte[] with a ByteBuffer before you read anything into the array.

    I would also use the read(byte[], 0, 1024) method, which will attempt to fill the buffer at each read.

    You can read the contents out of the ByteBuffer after each read. ByteBuffer is not intuitive, so make sure you go through the proper motions of resetting the ByteBuffer before each read.

    Note also that you code does not take into account the fact that the input stream could have more than 1024 bytes. You will have to read, process, loop until read returns -1.

    You might also consider wrapping the input stream with BufferedInputStream, depending on the source.

  3. #3
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Quote Originally Posted by Steve11235 View Post
    Well, I would start by wrapping the byte[] with a ByteBuffer before you read anything into the array.
    I don't know how long the stream is, and once it's wrapped you can't change the array to a bigger one.

    Quote Originally Posted by Steve11235 View Post
    I would also use the read(byte[], 0, 1024) method, which will attempt to fill the buffer at each read.
    Ah yes, I forgot to put the arguments in. Corrected.

    Quote Originally Posted by Steve11235 View Post
    You can read the contents out of the ByteBuffer after each read. ByteBuffer is not intuitive, so make sure you go through the proper motions of resetting the ByteBuffer before each read.
    I want to finish with a ByteBuffer that contains the whole contents of the stream.

    Quote Originally Posted by Steve11235 View Post
    Note also that you code does not take into account the fact that the input stream could have more than 1024 bytes. You will have to read, process, loop until read returns -1.
    Yes it does. That's what the while loop is for.

    Quote Originally Posted by Steve11235 View Post
    You might also consider wrapping the input stream with BufferedInputStream, depending on the source.
    There's no point in this case.
    Last edited by OrangeDog; 04-28-2009 at 05:55 PM.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

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

    Default

    Sorry, I read your code cross-eyed.

    First, I suggest looking at ArrayList over Vector. Vector is synchronized, which doesn't serve any purpose here, and synchronization adds a lot of overhead.

    Another alternative, if you have some idea of the maximum size of the input, would be to store each input array in a byte[1024][0]. That would allow you to hold up to 1 MB without pre-allocating a bunch of memory, and it avoids the overhead of the inner workings of a list.

    At the end of the day, you will have to allocate one big byte[] and put all the accumulated bytes in it. Then you can wrap it with a ByteBuffer.

    Or, you can just byte the bullet (sorry) and allocate a byte[] large enough to hold all the input, again assuming you have some idea of the size of what you will get. Just read directly into the array, moving the start point each time. Since you want to get all the bytes together before you process them, this will end up being the most efficient, as long as their is an upper limit to the amount of data you plan to receive. And frankly, if you are receiving a potentially large transmission, you should process it in pieces, anyway...

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    I was hoping to do it using only one array, rather than two and a Collection.

    An answer to this would solve it.

    At least what I have works. It'll do until I decide to re-write this 3rd-party library instead of just retrofitting it for applet use.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Well, I suppose you could create A ByteArrayOutputStream, and do bulk writes to it of everything you read inside your while loop. It will grow automatically. Then when you're done, create a new ByteBuffer by wrapping the results of ByteArrayOutputStream.toByteArray().

    I don't know how efficient this would be, but it's reasonably clean.

    Java Code:
    ByteArrayOutputStream out = new ByteArrayOutputStream(BUFSIZE);
    
    byte[] tmp = new byte[BUFSIZE];
    
    while (true) {
        int r = in.read(tmp);
        if (r == -1) break;
        
        out.write(tmp,0,r);
    }
    
    ByteBuffer buf = ByteBuffer.wrap(out.toByteArray());

  7. #7
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    is there any universal variable in JAVA like in vb.net when we create variable of object type like dim val as object then we can use it for any purpose to take string value or int value or any kind of value, do we have such type of datatype in java..

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    we are not having the datatype like this in java.Its strongly typed and sec ure language.

Similar Threads

  1. Creating an InputStream
    By c26354 in forum New To Java
    Replies: 10
    Last Post: 07-12-2011, 04:48 AM
  2. Which InputStream takes the least memory?
    By arnab321 in forum New To Java
    Replies: 9
    Last Post: 01-13-2009, 05:45 PM
  3. Converting InputStream to OutputStream
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-11-2008, 10:13 PM
  4. Reading bytes from InputStream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-25-2007, 07:51 PM
  5. Un expected behaviour when reading from inputstream
    By adoorsarath in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2007, 05:02 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
  •