Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

ByteBuffer methods: flip() vs rewind()

 
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am aware that flip() set the current buffer position to 0 and set the limit to the previous buffer position whereas rewind() just set the current buffer position to 0.

In the following code, either I use rewind() or flip() i get the same result.



Could you provide me with a real example where the difference of these 2 methods really matters? Thanks in advance.
 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I got it. After having understood that the real use of a ByteBuffer is interacting with a FileChannel (or with a IO/Channel in general). After having read the channel into the buffer the method flip() restore the position to zero and set the limit to the latest position occupied. In that way the buffer will be ready to be read itself. While rewind() just set the position to zero for rereading after having already been read.

Bottom line: solved.
 
Marshal
Posts: 69756
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done and thank you for the explanation.
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic