Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Bulk put(int) for ByteBuffer

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Bulk put(int) for ByteBuffer

    Hi.

    [Solved]

    I am not quite sure this is the right sub-forum for my question but the other ones didnt seem to be correct either. If I am in fact at the wrong place I apologize in advance.

    Now to my little problem with a direct ByteBuffer.

    I am trying to put an array of int values into a ByteBuffer but the ByteBuffer does not offer a method to do so directly.
    My own performance tests have shown, that iterating through an int array and putting each int individually leads to drastic performance loss compared to using an IntBuffer and putting the entire array at once.
    But performance is very important in this specific part of my application and to keep the code clean I would prefer to only use ByteBuffers.

    I was trying to cheat a little and did the following:
    	public static ByteBuffer makeByteBuffer(int[] data) {
    		ByteBuffer buffer = ByteBuffer.allocateDirect(data.length * INTEGER_BYTE_SIZE).order(ByteOrder.nativeOrder());
    		IntBuffer buf = buffer.asIntBuffer();
    		buf.put(data);
    		buffer.flip();
    		return buffer;
    	}
    In comparison, this code works and will not throw an error but is siginificantly slower:
    	public static ByteBuffer makeByteBuffer(int[] data) {
    		ByteBuffer buffer = ByteBuffer.allocateDirect(data.length * INTEGER_BYTE_SIZE).order(ByteOrder.nativeOrder());
    		for (int i = 0; i < data.length; i++) {
    			buffer.putInt(data[i]);
    		}
    		buffer.flip();
    		return buffer;
    	}

    But this just got me a fatal error when trying to use the ByteBuffer:
    #
    # A fatal error has been detected by the Java Runtime Environment:
    #
    # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000669b09e5, pid=9132, tid=7664
    #
    # JRE version: 7.0_07-b11
    # Java VM: Java HotSpot(TM) 64-Bit Server VM (23.3-b01 mixed mode windows-amd64 compressed oops)
    # Problematic frame:
    # C [atio6axx.dll+0xac09e5] atiPS+0x840475
    #
    # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
    #
    # If you would like to submit a bug report, please visit:
    # ...
    # The crash happened outside the Java Virtual Machine in native code.
    # See problematic frame for where to report the bug.
    #
    So my question is, what exactly is the correct way of realising a bulk put method for ints on a ByteBuffer with good performance?
    Or is there any fast and efficient way to cast an IntBuffer into a ByteBuffer maybe?

    Thank you very much.
    Last edited by Cornix; July 21st, 2013 at 03:56 AM. Reason: Solved


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Bulk put(int) for ByteBuffer

    In your first example don't flip the byte buffer, flip the int buffer. Also, are you sure INTEGER_BYTE_SIZE is the correct value? It should be 4.

    // simple example
    int[] data = new int[10];
    for(int i = 0; i < data.length; ++i)
    {
    	data[i] = i;
    }
    ByteBuffer buf = ByteBuffer.allocateDirect(data.length * 4).order(ByteOrder.nativeOrder());
    IntBuffer ibuf = buf.asIntBuffer();
    ibuf.put(data);
    ibuf.flip();
    for(int i = 0; i < buf.limit(); ++i)
    {
    	System.out.println(buf.get());
    }

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Bulk put(int) for ByteBuffer

    Well this is funny; It works, thank you a lot!
    Though I dont understand what the difference is.

Similar Threads

  1. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  2. Empty ByteBuffer size?
    By squeakbox in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 13th, 2012, 06:00 PM
  3. Bulk write of big int[] and long[]
    By nwp in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: December 4th, 2011, 03:28 PM
  4. Replies: 4
    Last Post: November 14th, 2010, 11:44 AM
  5. Bulk operations on sets/ map problem
    By kyuss in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 14th, 2010, 12:48 PM