Results 1 to 13 of 13
  1. #1
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default how to handle a java.nio.FloatBuffer?

    I have to put values in a FloatBuffer at different times. How can I ensure that I put the current value in the lowest free position, that is, the values be in a row, not spaced and of course not overwritten. I guess I have to use buffer.position() but how? I've never used buffers before.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: how to handle a java.nio.FloatBuffer?

    Can you post the code you are working with? Something that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default Re: how to handle a java.nio.FloatBuffer?

    Thanks for willing to help. No, I don't have problem, I just want to know the general way to do it when you have to add values to a buffer. I know you don't have to do any extra checks when you add them one after the other, as adding automatically increment the current position. But what if you add a value at some arbitrary time? Is it added at the correct position or you have to set position yourself (iterate and find the next free position)?

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: how to handle a java.nio.FloatBuffer?

    All objects are responsible for the consistency of their own internal state. It would be pretty ridiculous if FloatBuffer didn't keep track of its lowest free position for you. If you look at the documentation for FloatBuffer (Java Platform SE 7 ), you'll see that it updates the position every time you add a value.
    Get in the habit of using standard Java naming conventions!

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: how to handle a java.nio.FloatBuffer?

    Quote Originally Posted by mireazma View Post
    But what if you add a value at some arbitrary time? Is it added at the correct position or you have to set position yourself (iterate and find the next free position)?
    What would cause the position to change since the last time you added something?
    Get in the habit of using standard Java naming conventions!

  6. #6
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default Re: how to handle a java.nio.FloatBuffer?

    position(int) is the culprit. I found out that before using this rascal I must mark() the current position so later I reset() to here. But for fail-safe reasons, is there a "strong" way? Like using rewind() and iterate to find the lowest free position?

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: how to handle a java.nio.FloatBuffer?

    You never need to find the lowest free position. The position property defines the lowest free position.
    Get in the habit of using standard Java naming conventions!

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: how to handle a java.nio.FloatBuffer?

    Quote Originally Posted by mireazma View Post
    position(int) is the culprit. I found out that before using this rascal I must mark() the current position so later I reset() to here. But for fail-safe reasons, is there a "strong" way? Like using rewind() and iterate to find the lowest free position?
    I just read the API for the first time and tried some things. I don't understand what the issues are. You can write some numbers, wait a while, write some more. Then you can read them from the beginning. Why don't you submit a Short, Self Contained, Correct Example which describes the problem you are having.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default Re: how to handle a java.nio.FloatBuffer?

    Thanks all for the time. After some searching I found out that you should avoid position(int) as much as possible. If it's unavoidable, use mark(); eventually, if you're about to write and didn't follow the previous rules and you want to make sure you do it contiguously (you know you've messed around with position(int) while you were in writing mode some time before) for an assured, strong fail-safe:

    1. buffer.flip(); // put buffer in reading mode; now, position = 0, limit = last written position
    2. int p = ++buffer.limit(); // save limit position, well, the next one in fact
    3. buffer.reset(); // put buffer in writing mode; now, position = 0, limit = capacity
    4. buffer.position(p); // get the lowest free position.

    One probably could argue that, instead of all the code above, it's simpler to just use buffer.compact(); but you can't rely on it because it overwrites already read data and you may want to keep that available.

    Here's very well explained stuff about buffers:http://tutorials.jenkov.com/java-nio/buffers.html#clear
    Last edited by mireazma; 03-19-2014 at 11:16 AM.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: how to handle a java.nio.FloatBuffer?

    Buffers are the kind of API where you have to understand the intended usage before you understand the API itself. There are all these wonderful methods in them that you can totally misuse if you just don't know the problem they are designed to solve. It doesn't surprise me that you had to dig to get the truth out on the table; its hard to find good articles that are written from the perspective of the WHY and not the HOW. That article you link to is pretty good, but it still doesn't cut it for me.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    yuvijagdale is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: how to handle a java.nio.FloatBuffer?

    use java api for methods

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: how to handle a java.nio.FloatBuffer?

    Quote Originally Posted by yuvijagdale View Post
    use java api for methods
    Use shovels for digging.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: how to handle a java.nio.FloatBuffer?

    Quote Originally Posted by mireazma View Post
    One probably could argue that, instead of all the code above, it's simpler to just use buffer.compact(); but you can't rely on it because it overwrites already read data and you may want to keep that available.[/URL]
    That is one of the reasons why you would use #position(int). Set the position to the first element you want to retain as unread, and then call #compact().
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Obtaining a Java Handle
    By TaylorHubbard in forum Advanced Java
    Replies: 1
    Last Post: 03-14-2012, 09:36 AM
  2. how to handle calender date in java
    By reachsudha in forum Advanced Java
    Replies: 1
    Last Post: 05-19-2011, 12:19 PM
  3. FloatBuffer and 'put( float[] src )'
    By maxkiva in forum New To Java
    Replies: 4
    Last Post: 08-08-2010, 11:56 AM
  4. java java.nio.FloatBuffer
    By sardare in forum Advanced Java
    Replies: 2
    Last Post: 04-24-2009, 10:33 PM
  5. java.lang.NoClassDefFound --Any way to handle this
    By sunny_sykes in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2009, 08:29 AM

Posting Permissions

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